Lecture 30: Bellman-Ford Algorithm

advertisement
CSE 431/531: Analysis of Algorithms
Spring 2016
Lecture 30: Bellman-Ford Algorithm
Apr 1, 2016
Lecturer: Shi Li
• Given: G = (V, E), directed, w : E → R, may be negative, and a source vertex s ∈ V
• Output: shortest path from s to v, for every v ∈ V
s
s
6
7
8
a
-3
b
a
-10
-2
8
b
-3
-4
-10
c
7
d
c
-2
d
Figure 30.1: Left side: a graph with negative edge weights. Right side: shortest path tree; the distance
from s to a, b, c, d is respectively, 7, 2, −3, 4.
• Dijkstra’s Algorithm fails on the above example: it will think the distance from s to b is 6
Idea for Bellman-Ford Algorithm
• Maintain: f (v), v ∈ V : the length of some path from s to v
• Repeatedly improve f (v)’s
• Terminate the algorithm if f (v)’s can not be improved any more.
Update rule for an edge (u, v) ∈ E:
• If f (u) + w(u, v) < f (v) then f (v) ← f (u) + w(u, v)
Algorithm to implement the algorithm:
Let f (s) ← 0 and f (v) ← ∞ for every v ∈ V \ {s}
Repeat:
if there exists (u, v) ∈ E satisfying f (u) + w(u, v) < f (v) then
f (v) ← f (u) + w(u, v)
else break the loop
If there are negative cycles, the algorithm may run forever: consider a graph with two vertices s and t. There
is an edge (s, t) of weight 2 and an edge (t, s) of weight −3. Then the algorithm will keep updating f (s) and
f (t): f (t) ← 2, f (s) ← −1, f (t) ← 1, f (s) ← −2, f (t) ← 0, f (s) ← −3, · · · .
Exercise 30.1 If there are no negative cycles in graph G, then the algorithm will terminate.
30-1
30-2
Lecture 30: Bellman-Ford Algorithm
Bellman-Ford Algorithm
f (s) ← 0 and f (v) ← ∞ for every v ∈ V \ {s}
for i ← 1 to n
updated ← false
for each (u, v) ∈ E
if f (u) + w(u, v) < f (v)
f (v) ← f (u) + w(u, v)
π[v] ← u;
updated ← true
if not updated, then break
if updated, then output “negative cycle exists”
Notice that f (v) is either ∞, or the length of some path from s to v.
Lemma 30.2 After i-th iteration in the Bellman-Ford Algorithm, f (v) is at most the length of the shortest
path from s to v that uses at most i edges.
Proof: Proof by induction. The statement is clearly true for i = 0. Now suppose the statement is true
for i − 1 and we shall prove that it holds for i. Consider a vertex v and let P be the shortest from s to
v that uses at most i edges. If P contains at most i − 1 edges, then by induction hypothesis, f (v) is at
most the length of P after iteration i − 1; thus it is at most the length of P after iteration i since f (v)
can only decrease. If P contains i edges, then let u be the last vertex before v in path P and let P 0 be
the segment of P from s to u. Then, by induction hypothesis, after iteration i − 1, f (u) is at most the
length of P 0 . When we check the edge (u, v) in iteration i, f (u) is still at most the length of P 0 since
f (u) can only decrease as the algorithm proceeds. Thus, after checking the edge (u, v), we shall have
f (v) ≤ f (u) + w(u, v) = length of P 0 + w(u, v) = length of P .
Download