Alg Lec25 Spt2 Print
Alg Lec25 Spt2 Print
Alg Lec25 Spt2 Print
The Bellman-Ford algorithm solves the single-source shortest paths problem. Unlike Dijkstras algorithm, it allows edges of negative length. The bad news is that it runs slower. Like Dijkstras algorithm, the Bellman-Ford algorithm uses the technique of relaxation, progressively decreasing estimates v. d of the distance from s until the estimates are precise. The algorithm returns true iff the graph contains no negative cycles that are reachable from the source.
for each edge (u, v) E Relax(u, v, w) if v. d > u. d + w(u, v) return false /* i.e., (u, v) can still be relaxed */
The running time is O(V E ). COMP3600/6466: Lecture 25 2012 1 COMP3600/6466: Lecture 25 2012 2
COMP3600/6466: Lecture 25 2012 3
When (s, v1) is relaxed in the 1st phase, v1. d is set to (s, v1) if it isnt already. When (v1, v2) is relaxed in the 2nd phase, v2. d is set to (s, v2) if it isnt already.
When (vk1, vk ) is relaxed in the kth phase, vk . d is set to (s, vk ) if it isnt already.
So v. d = (s, v) for all v after |V | 1 phases, and no edges are still relaxable. COMP3600/6466: Lecture 25 2012 4
Summing this inequality over i = 1, . . . , k, we nd a contradiction since the sum of w(vi1, vi) is negative by assumption. Therefore, some edge of the negative cycle is still relaxable.
for i = 1, . . . , k.
DAG Shortest Paths(G, w, s) 1 s. d = 0 2 s. = NIL 3 for all v V {s} 4 v. d = 5 v. = NIL 6 for each vertex u in topological order 7 for v G. Adj [u] 8 Relax(u, v, w)
The time complexity is O(V + E ). 5 COMP3600/6466: Lecture 25 2012 6
Note that fewer than |V | 1 phases may not be enough. COMP3600/6466: Lecture 25 2012
Proof of correctness. Consider any shortest path s v1 v2 vk1 vk . The algorithm relaxes the edges from left to right.
When (s, v1) is relaxed, v1. d is set to (s, v1) if it isnt already. When (v1, v2) is relaxed, v2. d is set to (s, v2) if it isnt already.
COMP3600/6466: Lecture 25
2012
COMP3600/6466: Lecture 25
2012
T3 must be done at least 15 minutes after T7 T2 must be done before T9 T2 must be done at least 5 minutes before T4 T5 must be done at most 10 minutes after T1
and so on. We wish to know if this is possible and if so nd a schedule. If Ti is scheduled at time xi, the above constraints can be written as:
In general, we have real variables x1, x2, . . . , xn, and some number of constraints of the form x j xi bk . We will dene a weighted graph G = (V , E , w), called the constraint graph. There are n + 1 vertices V = {v0, v1, v2, . . . , vn}. There is an edge (v0, vi) of length 0 for i = 1, 2, . . . , n. For each constraint x j xi bk , there is an edge (vi, v j ) with length bk . Interesting fact: If the constraint graph has a negative cycle, there is no solution. Otherwise, an example of a solution is
x7 x3 15 x2 x9 0 x2 x4 5 x5 x1 10
COMP3600/6466: Lecture 25 2012 9
COMP3600/6466: Lecture 25
2012
10
x1 x2 0, x1 x5 1, x2 x5 1, x3 x1 5, x4 x1 4, x4 x3 1, x5 x3 3, x5 x4 3.
COMP3600/6466: Lecture 25
2012
11
COMP3600/6466: Lecture 25
2012
12