Content deleted Content added
m =Related problems and algorithms= +wikilinks |
=Pseudocode= consolidated subroutines to one code, italic math |
||
Line 29:
==Pseudocode==
In the following algorithm,
1 '''for''' each vertex v in V[G]▼
2 '''do''' d[v] := infinite▼
3 previous[v] := 0▼
4 d[s] := 0▼
Dijkstra(''G'',''w'',''s'')▼
1 '''
2 '''
3
7 '''while''' Q is not an empty set
12 '''then''' d[v] = d[u] + w(u,v)
If we are only interested in a shortest path between
▲v = Extract-Min(Q) searches for the vertex v in the vertex set Q that has the least d[v] value. That vertex is removed from the set Q and then returned.
Now we can read the shortest path from ''s'' to ''t'' by iteration:▼
1 ''S'' = empty sequence▼
▲Dijkstra(G,w,s)
▲ 2 S := empty set
4 '''if''' ''u'' == ''s''▼
▲ 3 Q := set of all vertices
6 ''u'' = previous[''u'']▼
▲ 6 S := S union {u}
▲ 8 '''do''' Relax(u,v,w)
▲If we are only interested in a shortest path between vertexes s and t, we can terminate the search at line 5 if u == t.
▲Now we can read the shortest path from s to t by iteration:
▲ 1 S = empty sequence
▲ 2 u := t
▲ 3 S = u + S /* insert u to the beginning of S */
▲ 4 '''if''' u == s
▲ 6 u = previous[u]
7 goto 3
Now sequence ''S'' has the shortest path from ''s'' to ''t''.
== Running time==
|