Professional Documents
Culture Documents
Lec18 PDF
Lec18 PDF
6.046J/18.401J LECTURE 18
Shortest Paths II Bellman-Ford algorithm Linear programming and difference constraints VLSI layout compaction
Negative-weight cycles
Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist. Example: <0 u u v v
L18.2
Negative-weight cycles
Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist. Example: <0 u u v v
Bellman-Ford algorithm: Finds all shortest-path lengths from a source s V to all v V or determines that a negative-weight cycle exists.
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.3
Bellman-Ford algorithm
d [ s] 0 for each v V {s} do d[v]
initialization
for i 1 to | V | 1 do for each edge (u, v) E do if d[v] > d[u] + w(u, v) relaxation then d[v] d[u] + w(u, v) step for each edge (u, v) E do if d[v] > d[u] + w(u, v) then report that a negative-weight cycle exists At the end, d[v] = (s, v), if no negative-weight cycles. Time = O(VE).
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.4
Example of Bellman-Ford
B B
3 4 1 5 2 2
A A
E E
3
C C
D D
L18.5
Example of Bellman-Ford
B B
3 4 1 5 2 2
0 A A
E E
3
C C
D D
Initialization.
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.6
Example of Bellman-Ford
B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C
D D
Example of Bellman-Ford
B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C
D D
L18.8
Example of Bellman-Ford
B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C
D D
L18.9
Example of Bellman-Ford
B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C
D D
L18.10
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C
D D
L18.11
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C 4
D D
L18.12
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C 4
D D
L18.13
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C 2 4
D D
L18.14
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C 2
D D
L18.15
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
E E
C C 2
D D
End of pass 1.
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.16
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
C C 2
D D
L18.17
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
3
C C 2
D D
L18.18
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
3
C C 2
D D 1
L18.19
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
3
C C 2
D D 1
L18.20
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
3
C C 2
D D 1
L18.21
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
3
C C 2
D D 1
L18.22
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
1 E E
3
C C 2
D D 1
L18.23
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
2 3
1 E E
C C 2
D D 2 1
L18.24
Example of Bellman-Ford
1 B B
7 3 1
0 A A
1
4 5
2
8
1 5
2 3
1 E E
C C 2
D D 2
Correctness
Theorem. If G = (V, E) contains no negativeweight cycles, then after the Bellman-Ford algorithm executes, d[v] = (s, v) for all v V.
L18.26
Correctness
Theorem. If G = (V, E) contains no negativeweight cycles, then after the Bellman-Ford algorithm executes, d[v] = (s, v) for all v V.
Proof. Let v V be any vertex, and consider a shortest path p from s to v with the minimum number of edges.
s p: v v00
v v11
v v22
v v33
v vkk
Since p is a shortest path, we have (s, vi) = (s, vi1) + w(vi1, vi) .
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.27
Correctness (continued)
s p: v v00 v v11 v v22 v v33
v v vkk
Initially, d[v0] = 0 = (s, v0), and d[v0] is unchanged by subsequent relaxations (because of the lemma from Lecture 14 that d[v] (s, v)). After 1 pass through E, we have d[v1] = (s, v1). After 2 passes through E, we have d[v2] = (s, v2). M After k passes through E, we have d[vk] = (s, vk). Since G contains no negative-weight cycles, p is simple. Longest simple path has | V | 1 edges.
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.28
L18.29
Linear programming
Let A be an mn matrix, b be an m-vector, and c be an n-vector. Find an n-vector x that maximizes cTx subject to Ax b, or determine that no such solution exists. n m . A
November 16, 2005
x b
maximizing cT
x
L18.30
Linear-programming algorithms
Algorithms for the general problem Simplex methods practical, but worst-case exponential time. Interior-point methods polynomial time and competes with simplex.
L18.31
Linear-programming algorithms
Algorithms for the general problem Simplex methods practical, but worst-case exponential time. Interior-point methods polynomial time and competes with simplex. Feasibility problem: No optimization criterion. Just find x such that Ax b. In general, just as hard as ordinary LP.
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.32
L18.33
L18.34
v vii
wij
v vjj
Unsatisfiable constraints
Theorem. If the constraint graph contains a negative-weight cycle, then the system of differences is unsatisfiable.
L18.36
Unsatisfiable constraints
Theorem. If the constraint graph contains a negative-weight cycle, then the system of differences is unsatisfiable.
Proof. Suppose that the negative-weight cycle is v1 v2 L vk v1. Then, we have x2 x1 x3 x2 w12 w23
M
L18.37
Unsatisfiable constraints
Theorem. If the constraint graph contains a negative-weight cycle, then the system of differences is unsatisfiable.
Proof. Suppose that the negative-weight cycle is v1 v2 L vk v1. Then, we have x2 x1 x3 x2 w12 w23
M
L18.39
v v99 v v33
L18.40
0 s
v v99 v v33
L18.41
Proof (continued)
Claim: The assignment xi = (s, vi) solves the constraints. Consider any constraint xj xi wij, and consider the shortest paths from s to vj and vi:
s s
v vii
wij
v vjj
The triangle inequality gives us (s,vj) (s, vi) + wij. Since xi = (s, vi) and xj = (s, vj), the constraint xj xi wij is satisfied.
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.42
x1
x2
x2 x1 d 1 + Bellman-Ford minimizes maxi{xi} mini{xi}, which compacts the layout in the x-dimension. Constraint:
November 16, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L18.45