Shortest Paths II: Bellman-Ford, Topological Sort, DAG Shortest Paths, Linear Programming, Difference Constraints
Shortest Paths II: Bellman-Ford, Topological Sort, DAG Shortest Paths, Linear Programming, Difference Constraints
Shortest Paths II: Bellman-Ford, Topological Sort, DAG Shortest Paths, Linear Programming, Difference Constraints
Negative-weight cycles
Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist.
Example:
<0
u
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)
then d[v] d[u] + w(u, v)
relaxation
step
Example of Bellman-Ford
A
4
A B C D E
0
E
3
L15.4
Example of Bellman-Ford
1
B
A
4
A B C D E
0
0 1
E
3
L15.5
Example of Bellman-Ford
1
B
A
4
A
0
0
0
1
1
C
4
L15.6
Example of Bellman-Ford
1
B
A
4
E
3
C
42
A
0
0
0
0
1
1
1
4
2
L15.7
Example of Bellman-Ford
1
B
A
4
E
3
C
2
A
0
0
0
0
1
1
1
4
2
L15.8
Example of Bellman-Ford
1
B
A
4
E
3
C
2
A
0
0
0
0
0
1
1
1
1
4
2
2
L15.9
Example of Bellman-Ford
1
B
A
4
E
3
C
2
A
0
0
0
0
0
0
1
1
1
1
1
4
2
2
2
1
1
L15.10
Example of Bellman-Ford
1
B
A
4
E
3
C
2
D
2
1
A
0
0
0
0
0
0
0
1
1
1
1
1
1
4
2
2
2
2
1
2
1
1
1
L15.11
Example of Bellman-Ford
1
B
A
4
E
3
C 5 D
2
2
1
Note: Values decrease
monotonically.
A
0
0
0
0
0
0
0
1
1
1
1
1
1
4
2
2
2
2
1
2
1
1
1
L15.12
Correctness
Theorem. If G = (V, E) contains no negativeweight cycles, then after the Bellman-Ford
algorithm executes, d[v] = d(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.
p: v0
v
v
k
Correctness (continued)
s
p: v0
v
v
k
Detection of negative-weight
cycles
Corollary. If a value d[v] fails to converge after
| V | 1 passes, there exists a negative-weight
cycle in G reachable from s.
L15.15
4
3
s 2
7
6
8
9
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
x b
maximizing
cT
x
L15.17
Linear-programming
algorithms
Algorithms for the general problem
Simplex methods practical, but worst-case
exponential time.
Ellipsoid algorithm polynomial time, but
slow in practice.
Interior-point methods polynomial time and
competes with simplex.
xj xi wij
vi
wij
vj
(The A
matrix has
dimensions
|E | |V |.)
L15.19
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
xk xk1 wk1, k
x1 xk wk1
0
weight of cycle
<0
Therefore, no
values for the xi
can satisfy the
constraints.
L15.20
0
s
Note:
No negative-weight
cycles introduced
shortest paths exist.
L15.21
Proof (continued)
Claim: The assignment xi = d(s, vi) solves the constraints.
Consider any constraint xj xi wij, and consider the
shortest paths from s to vj and vi:
d(s, vi)
vi
d(s, vj)
wij
vj
The triangle inequality gives us d(s,vj) d(s, vi) + wij.
Since xi = d(s, vi) and xj = d(s, vj), the constraint xj xi
wij is satisfied.
L15.22