Beruflich Dokumente
Kultur Dokumente
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Lecture 16
Recall:
path p = < v0 , v1 , . . . , vk >
(v1 , vi+1 ) E
w(p) =
k1
0i<k
w(vi , vi+1 )
i0
Shortest path weight from u to v is (u, v). (u, v) is if v is unreachable from u, undened
if there is a negative cycle on some path from u to v.
-ve
u
Main:
for v V :
d [v]
[v]
NIL
d[S] 0
repeat
select edge (u, v) [somehow]
if d[v] > d[u] + w(u, v) :
Lecture 16
Complexity:
Termination: Algorithm will continually relax edges when there are negative cycles present.
u
0
0
-1
-2
d[u]
-1
-4
3
etc
2
1
0
v
1
v0
T(n) = 3 + 2T(n-2)
T(n) = (2 )
n/2
v1
v2
v3
v4
v5
v6
10
12
13
10
11
14
13
12
11
10
ORDER
(v0, v1)
(v1, v2)
all of v2, vn
(v0, v2)
all of v2, vn
Lecture 16
5-Minute 6.006
Heres what I want you to remember from 6.006 ve years after you graduate
Exponential Bad
Polynomial Good
if C2 > 1, trouble!
Divide & Explode
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
-1
-1
B
-1
0
4
4 2
2
3
5
-1
-3
3
5
End of pass 1
1
1
-3
1 -2
2
Figure 5: The numbers in circles indicate the order in which the values are computed
Lecture 16
Theorem:
If G = (V, E) contains no negative weight cycles, then after Bellman-Ford executes d[v] =
(u, v) for all vV .
Proof:
vV be any vertex. Consider path p from s to v that is a shortest path with minimum
number of edges.
v
v1
vk
(s, vi) =
(s, vi-1) + w (vi-1,vi)
S
p:
v0
v2
Corollary
If a value d[v] fails to converge after | V | 1 passes, there exists a negative-weight cycle
reachable from s.