~/ learn/ comp-372/ cards/ Bellman-Ford & DAG shortest paths
1 of 4

Type the BELLMAN-FORD pseudocode

Type the BELLMAN-FORD pseudocode

Answer

BELLMAN-FORD(G, w, s): INITIALIZE-SINGLE-SOURCE(G, s) for i = 1 to |G.V| - 1: for each edge (u, v) in G.E: RELAX(u, v, w) for each edge (u, v) in G.E: if v.d > u.d + w(u, v): return FALSE # negative cycle return TRUE

The |V|−1 passes settle every simple shortest path (at most |V|−1 edges). The final pass tests for a still-relaxable edge, which can only happen when a reachable negative-weight cycle exists.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/0f68ef17-ec20-42df-8554-d78c2a757f27/flashcard utf-8 LF