
ECS122A Handout May 26, 2009
Bellman-Ford(G, w, s)
1. Init-Single-Source(V, s)
2. for i= 1 to |V| − 1 do
3. for each edge (u, v)∈Edo
4. Relax(u, v, w)
5. endfor
6. endfor
7. for each edge (u, v)∈Edo /*check pass */
8. if d[v]> d[u] + w(u, v) then
9. return false
10. endif
11. endfor
12. return true
Init-Single-Source(V, s)
1. for each vertex v∈Vdo
2. d[v] = ∞
3. π[v] = nil
4. endfor
5. d[s] = 0
Relax(u, v, w)
1. if d[v]> d[u] + w(u, v) do
2. d[v] = d[u] + w(u, v)
3. π[v] = u
4. endif
Notes:
•d[v] = δ(s, v): the shortest-path weight from the source vertex sto v
•π[v]: the parent (predecessor) of v
•Bellman-Ford algorithm returns true if no negative-weight cycles reachable from source
vertex s,false otherwise.
•How quickly d[v] converges depends on order of relaxation (processing edges). But guar-
anteed to converge after |V| − 1 passes, assuming no negative-weight cycles.
1