Lecture 17: Shortest Paths III: Bellman-Ford
Lecture 17: Shortest Paths III: Bellman-Ford
• Bellman-Ford Algorithm
– Analysis
– Correctness
Recall:
-ve
u v
1
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011
d [v] ← ∞
Initialize: for v ∈ V :
Π [v] ← NIL
d[S] ← 0
Main: repeat
select edge (u, v) [somehow]
if d[v] > d[u] + w(u, v) :
“Relax” edge (u, v) d[v] ← d[u] + w(u, v)
π[v] ← u
until you can’t relax any more edges or you’re tired or . . .
Complexity:
Termination: Algorithm will continually relax edges when there are negative cycles
present.
-1
1 -4
u v
0 1 3 4
1 2 1
0 2
d[u] -1 1
-2 0
etc
2
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011
v0 v1 v2 v3 v4 v5 v6
4 8 10 12 13 14
13
ORDER
10 11 12
(v0, v1)
11
T(n) = 3 + 2T(n-2) (v1, v2)
4 6 8 9 10
all of v2, vn
T(n) = θ(2n/2) (v0, v2)
all of v2, vn
Figure 3: Algorithm could take exponential time. The outgoing edges from v0 and v1
have weight 4, the outgoing edges from v2 and v3 have weight 2, the outgoing edges
from v4 and v5 have weight 1.
5-Minute 6.006
Figure 4 is what I want you to remember from 6.006 five years after you graduate!
Bellman-Ford(G,W,s)
Initialize ()
for i = 1 to |V | − 1
for each edge (u, v) ∈ E:
Relax(u, v)
for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
then report a negative-weight cycle exists
Theorem:
If G = (V, E) contains no negative weight cycles, then after Bellman-Ford executes
d[v] = δ(s, v) for all v ∈ V .
3
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011
Proof:
Let v ∈ V be any vertex. Consider path p = hv0 , v1 , . . . , vk i from v0 = s to vk = v
that is a shortest path with minimum number of edges. No negative weight cycles
=⇒ p is simple =⇒ k ≤ |V | − 1.
Corollary
If a value d[v] fails to converge after |V | − 1 passes, there exists a negative-weight
cycle reachable from s.
Proof:
After |V | − 1 passes, if we find an edge that can be relaxed, it means that the current
shortest path from s to some vertex is not simple and vertices are repeated. Since this
cyclic path has less weight than any simple path the cycle has to be a negative-weight
cycle.
4
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011
1
∞ -1 -1
B 1 B 1
-1 2 -1 2
3 3
0 4 7 0 4 7
A 2 E ∞ A 2 E ∞ 1
3 1 3 1 1
5 8 5 8
4 2 -3 4 2 -3
6 6
C D C D
5 5 1 -2
4 2 ∞ ∞ 2 ∞ 2 3
2 3
Figure 5: The numbers in circles indicate the order in which the δ values are computed.
v
v1 vk
δ (s, vi) =
S δ (s, vi-1) + w (vi-1,vi)
p: v0 v2
5
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.