In a directed graph with non-negative edge weights I can easily find shortest path from u to v using dijkstra's. But is there any simple tweak to Dijkstra's so that I can find shortest path from u to v through a given vertex w. Or any other algorithm suggestions?
Asked
Active
Viewed 5,898 times
7
-
Is it possible to find that a path exists from u to v that has w in it, not necessarily the shortest path. Can it be done in poly(n) time, where n is the number of vertices in the graph. – Rahul Kadukar Feb 28 '14 at 09:50
-
For an arbitrary graph, this problem is NP-hard. See http://cstheory.stackexchange.com/questions/25077/shortest-path-hitting-a-given-vertex for more details. – Zach Langley Dec 30 '15 at 02:33
5 Answers
9
Find the shortest path from u to w, then the shortest path from w to v.

Beta
- 96,650
- 16
- 149
- 150
-
1This is not necessarily the shortest path. It's not hard to find a counter-example where the two shortest paths share an edge. In such a case, concatenating the paths does not give you a valid path. This problem is NP-hard. – Zach Langley Dec 30 '15 at 02:32
-
-
1@Beta A path cannot repeat vertices. If the shortest path from u to w contains the vertex z and the shortest path to w to v also contains z, z will be listed twice in the concatenated list of vertices, so the list of vertices does not form a path. It turns out that the simpler problem of determining if there exists *any* path from u to v through w is NP-hard for arbitrary directed graphs. (See http://cstheory.stackexchange.com/questions/25077/shortest-path-hitting-a-given-vertex for more details.) – Zach Langley Jan 04 '16 at 00:50
-
@ZachLangley: Who says a path cannot repeat vertices? Do you think the OP had that condition in mind? – Beta Jan 04 '16 at 01:55
-
2@Beta It's not clear from the question what the OP had in mind, but from every definition I've seen, paths do not repeat vertices. Usually if vertex repetition is allowed, it is called a *walk*. – Zach Langley Jan 04 '16 at 16:51
-
1
5
- Find shortest path from u to w
- Find shortest path from w to v
Then u->w->v is the shortest path.
You can do it by running Dijkstra for two times, but you can also apply the Floyd-Warshall algorithm.

Mu Qiao
- 6,941
- 1
- 31
- 34
3
This problem is NP-hard, so finding a polynomial time solution is unlikely. See this cstheory post for more details.

Community
- 1
- 1

Zach Langley
- 6,776
- 1
- 26
- 25
1
Using the following approach we could run the algorithm just once:
set v_visisted = false
Start from w and find shortest path to u
if v was visited during shortest path search to u, set v_visted = true
If v is part of shortest path from w->u then
exit with result ( # the path would be u->v->w->v )
else
if v_visited= true then we already know values for w->v. We have a solution.
else save path from w->v and switch u to source and find shortest path to v.
Note that running the shortest path from u to v is effectively continuing the algo's first run. Therefore, we are running the algo just once, by tracking if we visited 'v'.

grdvnl
- 636
- 6
- 9
0
Looks like finding u to w and then finding w to v, concatenating both results. Would it work?

heltonbiker
- 26,657
- 28
- 137
- 252
-
no it wouldnt. some vertices can apperear multiple times in the shortest path – Christo S. Christov Dec 11 '17 at 11:58