INITIALIZE-SINGLE-SOURCE(G, s) for each vertex v∈G.V v.d = ∞ v.π = NIL s.d = 0
RELAX(u, v, w) if v.d > u.d + w(u, v) v.d = u.d + w(u, v) v.π = u
BELLMAN-FORD(G, w, s) for i=1 to |G.V|-1 for each edge(u, v)∈G.E RELAX(u, v, w) for each edge(u, v)∈G.E if v.d > u.d + w(u, v) return FALSE return TRUE
DAG-SHORTEST-PATHS(G, w, s) topologically sort the vertices of G INITIALIZE-SINGLE-SOURCE(G, s) for each vertex u, taken in topologically sorted order for each vertex v∈G.Adj[u] RELAX(u, v, w)
Dijkstra算法
DIJKSTRA(G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) S = Empty Q = G.V while Q ≠ Empty u = EXTRACT-MIN(Q) S = S ∪ {u} for each vertex v∈G.Adj[u] RELAX(u, v, w)