# lecture12_bellman_ford_floyd_marshall

```More on Dynamic Programming
Bellman-Ford
Floyd-Warshall
Wednesday, July 30th
1
Outline For Today
1. Bellman-Ford’s Single-Source Shortest Paths
2. Floyd-Warshall’s All-Pairs Shortest Paths
2
Recipe of a DP Algorithm (1)
1: Identify small # of subproblems
Ex 1: Find opt independent set in Gi: only n subproblems.
Ex 2: Find optimal alignment of Xi and Yj: only mn
subproblems
3
Recipe of a DP Algorithm (2)
Step 2: quickly + correctly solve “larger” subproblems
given solutions to smaller ones, usually via a recursive
formula:
Ex 1: Linear Ind. Set: A[i] = max {wi + A[i-2], A[i-1]}
A[i-1][j-1] + αij
Ex 2: Alignment: A[i][j] = min
A[i-1][j] + δ
A[i][j-1] + δ
4
Recipe of a DP Algorithm (3)
3: After solving all subproblems, can quickly compute
final solution
Ex 1: Linear Independent Set: return A[n]
Ex 2: Alignment: return A[m][n]
5
Outline For Today
1. Bellman-Ford’s Single-Source Shortest Paths
2. Floyd-Warshall’s All-Pairs Shortest Paths
6
Recap: Single-Source Shortest Paths (SSSP)
 Input: Directed (simple) graph G(V, E), source s, and weights
ce on edges.
 Output: ∀v ∈V, shortest s ⤳ v path.
 Dijkstra’s Algorithm: O(mlogn); only if ce ≥ 0
7
Pros/Cons of Dijkstra’s Algorithm
Pros: O(mlogn) super fast & simple algorithm
Cons:
1. Works only if ce ≥ 0
 Sometimes need negative weights, e.g. (finance)
2. Not very distributed/parallelizable:
 Looks very “serial”
 Need to store large parts of graph in-memory.
 Therefore not very scalable in very large graphs.
Bellman-Ford addresses both of these drawbacks
(Last lecture will see parallel version of BF)
8
Preliminary: Negative Weight Cycles
Question: How to define shortest paths when G has
negative weights cycles?
G
4
x
s
y
-6
-5
z
3
t
9
Possible Shortest s⤳v Path Definition 1
Shortest path from s to v with cycles allowed.
Problem: Can loop forever in a negative cycle.
So there is no “shortest path”!
G
4
x
s
y
-6
-5
z
3
t
10
Possible Shortest s⤳v Path Definition 2
Shortest path from s to v, cycles NOT allowed.
Problem: Now well-defined. But NP-hard. Don’t
expect a “fast” algorithm solving it exactly.
G
4
x
s
y
-6
-5
z
3
t
11
Solution: Assume No Negative-Weight Cycles
Q: Now, can shortest paths contain cycles?
A: No. Assume P is shortest path from s to v,
with a cycle.
P= s ⤳ x ⤳ x ⤳ v. Then since x ⤳ x is a cycle,
and we assumed no negative weight cycles, we
could get P`= s ⤳ x ⤳ v, and get a shorter path.
12
Upshot: Bellman-Ford’s Properties
Note: Bellman-Ford will be able to detect if there
is a negative weight cycle!
G(V, E),
weights
∃ negativeweight cycle
Bellman-Ford
Shortest Paths
Both outputs computed in reasonable
amount of time.
13
Challenge of A DP Approach
Need to identify some sub-problems.
 Linear IS:
 Line graph was naturally ordered from left to right.
 Subproblems could be defined as prefix graphs.
 Sequence Alignment:
 X, Y strands were naturally ordered strings.
 Subproblems could be defined as prefix strings.
**Shortest Paths’ Input G Has No Natural Ordering**
14
High-level Idea Of Subproblems
But the Output Is Paths & Paths Are Sequential!
Trick: Impose an Ordering Not On G but on Paths.
Larger Paths Will Be Derived By Appending New
Edges To The Ends Of Smaller (Shorter) Paths.
15
Subproblems
Input: G(V, E) no negative cycles, s, ce arbitrary weights.
Output: ∀v, global shortest paths from s to v.
Q: Max possible # hops (or # edges) on the
shortest paths?
A: n-1 (**since there are no negative cycles**)
P(v, i) = Shortest path from s to v with at
most i edges (& no cycles).
16
Example
Let P(v, i) be the shortest s-v path with ≤ i edges.
x
1
s
-1
w
2
1
z
3
-6
t
2
a
1
b
0
c
Q: P(t,1)?
A: Does not exist (assume such paths have ∞ weights.)
17
Example
Let P(v, i) be the shortest s-v path with ≤ i edges.
x
1
s
-1
w
2
1
z
3
-6
t
2
a
1
b
0
c
Q: P(t,2)?
A: s->x->t with weight 2.
18
Example
Let P(v, i) be the shortest s-v path with ≤ i edges.
x
1
s
-1
w
2
1
z
3
-6
t
2
a
1
b
0
c
Q: P(t,3)?
A: s->w->z->t with weight -1.
19
Example
Let P(v, i) be the shortest s-v path with ≤ i edges.
x
1
s
-1
w
2
1
z
3
-6
t
2
a
1
b
0
c
Q: P(t,4)?
A: s->w->z->t with weight -1.
20
Solving P(v,i) In Terms of “Smaller” Subproblems
Let P=P(v, i) be the shortest s-v path with ≤ i edges
Note: For some v, an s-v path with ≤ i edges may not
exist. Assume v has such a path.
A Claim that does not require a proof:
|P| ≤ i-1 OR |P| = i
21
Case 1: |P=P(v, i)| ≤ i-1
s
v
Q: What can we assert about P(v,
i-1)?
A: P(v, i-1) = P(v, i) (by contradiction)
(P(v, i) is also the shortest s⤳v path with at most i-1
edges)
22
Case 2: |P=P(v, i)| = i
s
u
v
P`=>|s⤳u| = i-1
Q: What can we assert about P`?
Claim: P` = P(u,
i-1)
(P` is shortest s⤳u path in with ≤ i-1 edges)
23
Proof that P`=P(u, i-1)
Assume ∃a better s⤳u path Q with ≤ i-1 edges
s
u
v
Q
Q had ≤ i-1 edges, then Q ∪ (u,v) has ≤ i edges.
cost(Q) < cost(P`), cost(Q ∪ (u,v)) < cost(P).
Q.E.D
24
Summary of the 2 Cases
Case 1: |P(v, i)| ≤ i-1 => P(v,
i-1)
= P(v,
s
v
Case 2: |P(v, i)| = i => P` = P(u,
s
i)
u
i-1)
v
P`=>|s⤳u| = i-1
25
Our Subproblems Agains
∀ v, and for i={1, …, n}
P(v, i): shortest s ⤳ v path with ≤ i edges (or null)
L(v, i): w(P(v, i)) (and +∞ for null paths)
L(v, i-1)
L(v, i) = min
minu: ∃(u,v)∈E : L(u, i-1) + c(u,v)
26
Bellman-Ford Algorithm
L(v, i): w(P(v, i))
Let A be an nxn 2D array.
A[i][v] = shortest path to vertex v with ≤ i edges.
procedure Bellman-Ford(G(V,E), weights C):
Base Cases: A[0][s] =
27
Bellman-Ford Algorithm
L(v, i): w(P(v, i))
Let A be an nxn 2D array.
A[i][v] = shortest path to vertex v with ≤ i edges.
procedure Bellman-Ford(G(V,E), weights C):
Base Cases: A[0][s] = 0
A[0][j] =
28
Bellman-Ford Algorithm
L(v, i): w(P(v, i))
Let A be an nxn 2D array.
A[i][v] = shortest path to vertex v with ≤ i edges.
procedure Bellman-Ford(G(V,E), weights C):
Base Cases: A[0][s] = 0
A[0][j] = +∞ where j ≠ s
for i = 1, …, n-1:
for v ∈ V:
A[i][v] = min {A[i-1][v]
min(u,v)∈E A[i-1][u]+c(u,v)
29
Correctness of BF
By induction on i and correctness of the
recurrence for L(v, i) (exercise)
30
Runtime of BF
# entries in A is n2.
Q: How much time for computing each A[i][v]?
A: in-deg(v)
For each i, total work for all A[i][v] entries is:
å in - deg(v)
vÎV
Total Runtime: O(nm)
…
for i = 1, …, n-1:
for v ∈ V:
A[i][v] = min {A[i-1][v]
min(u,v)∈E A[i-1][u]+c(u,v)
31
Runtime Optimization: Stopping Early
Suppose for some i ≤ n:
A[i][v] = A[i-1][v] ∀ v.
Q: What does this mean?
A: Values will not change in any later iteration
=> We can stop!
…
Values only depend on the
previous iteration!
for i = 1, …, n-1:
for v ∈ V:
A[i][v] = min {A[i-1][v]
min(u,v)∈E A[i-1][u]+c(u,v)
32
Negative Cycle Checking
Consider any graph G(V, E) with arbitrary edge weights.
=>There may be negative cycles.
Claim: If BF stabilizes at some iteration i > 0, then
G has no negative cycles.
(i.e., negative cycles implies BF never stabilizes!)
33
How To Check For Cycles If Claim is True
Run BF just one extra iteration!
Check if A[n][v] = A[n-1][v] for all v.
If so, no negative cycles, o.w. there is a negative
cycle.
Running n iterations is the general form of BF:
G(V, E),
weights
∃ negativeweight cycle
Bellman-Ford
Shortest Paths
34
Proof of Claim:
BF Stabilizes => G has no negative cycles
Assume BF has stabilized in iteration i.
Notation: d(v) = A[i][v] = A[i-1][v] (by above assumption)
A[i][v] = min {A[i-1][v]
min(u,v)∈E A[i-1][u] + c(u,v)
d(v) = min {d(v)
min(u,v)∈E d(u) + c(u,v)
d(v) ≤ d(u) + c(u,v)
Let’s argue that every cycle C has non-negative
weight...
35
Proof of Claim (continued)
d(v) ≤ d(u) + c(u,v)
Fix a cycle C:
y
z
x
t
36
Proof of Claim (continued)
d(v) ≤ d(u) + c(u,v) => d(v) – d(u) ≤ c(u,v)
Fix a cycle C:
y
z
x
t
37
Proof of Claim (continued)
d(v) ≤ d(u) + c(u,v) => d(v) – d(u) ≤ c(u,v)
Fix a cycle C:
y
z
x
t
d(x) – d(t) ≤ c(t,x)
38
Proof of Claim (continued)
d(v) ≤ d(u) + c(u,v) => d(v) – d(u) ≤ c(u,v)
Fix a cycle C:
y
z
x
t
d(x) – d(t) ≤ c(t,x)
d(y) – d(x) ≤ c(x,y)
39
Proof of Claim (continued)
d(v) ≤ d(u) + c(u,v) => d(v) – d(u) ≤ c(u,v)
Fix a cycle C:
y
z
x
t
d(x) – d(t) ≤ c(t,x)
d(y) – d(x) ≤ c(x,y)
d(z) – d(y) ≤ c(y,z)
40
Proof of Claim (continued)
d(v) ≤ d(u) + c(u,v) => d(v) – d(u) ≤ c(u,v)
Fix a cycle C:
y
z
x
t
d(x) – d(t) ≤ c(t,x)
d(y) – d(x) ≤ c(x,y)
d(z) – d(y) ≤ c(y,z)
d(t) – d(z) ≤ c(z,t)
0 ≤ w(C)
Same algebra and result
for any cycle (exercise).
Q.E.D. 41
Space Optimization (1)
Only need A[i-1][v]’s to compute A[i][v]s.
 Only need O(n) space; i.e., O(1) per vertex.
Q: By throwing things out, what do we lose in general?
A: Reconstruction of the actual paths.
But with only O(n) more space, we can actually
…
reconstruct the paths!
for i = 1, …, n-1:
for v ∈ V:
A[i][v] = min {A[i-1][v]
min(u,v)∈E A[i-1][u]+c(u,v)
42
Space Optimization (1)
Fix: Each v stores a predecessor pointer (initially null)
Whenever A[i][v] is updated to A[i-1][u]+c(u,v), we set
the Pred[v] to u.
Claim: At termination, tracing pointers back from v
yields the shortest s-v path.
…
(Details in the book, by induction on i)
for i = 1, …, n-1:
for v ∈ V:
A[i][v] = min {A[i-1][v]
min(u,v)∈E A[i-1][u]+c(u,v)
43
Summary of BF
Runtime: O(nm), not as fast as Dijkstra’s O(mlogn).
But works with negative weight edges.
And is distributable/parallelizable.
Will see its distributed version last lecture of class.
44
Outline For Today
1. Bellman-Ford’s Single-Source Shortest Paths
2. Floyd-Warshall’s All-Pairs Shortest Paths
45
All-Pairs Shortest Paths (APSP)
Input: Directed G(V, E), arbitrary edge weights, no neg. cycles.
Output: ∀ u, v, d(u, v): shortest (u, v) path in G.
(no fixed source s)
Q: What’s a poly-time algorithm to solve APSP?
A: for each s, run BF from s => O(n2m)
Better Algorithm: Floyd-Warshall
46
Floyd-Warshall Idea
Linear IS: input graph naturally ordered sequentially
Seq. Alignment: strings naturally ordered sequentially
Bellman-Ford: output paths naturally ordered
sequentially
FW imposes sequentiality on the vertices
 order vertices from 1 to n
 only use the first i vertices in each subproblem
(Same idea works for SSSP, but less efficient than BF.)
47
Floyd-Warshall Subproblems
V= {1, …, n}, ordered completely arbitrarily
Vk = {1, …, k}
Original Problem: ∀(u, v) shortest u, v path.
We need to define the subproblems.
Subproblem P(i, j, k) = shortest i, j path that uses
only Vk as intermediate nodes (excluding i and j).
48
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
1
6
0
7
Q: P(6,4,1)?
A: null (weight of +∞)
49
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
1
6
0
7
Q: P(2,4,1)?
A: 2->1->4 (weight of 2)
50
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
1
6
0
7
Q: P(2,6,0)?
A: 2-6 (weight of 2)
(no intermediate nodes needed)
51
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
1
6
0
7
Q: P(2,6,1)?
A: 2-6 (still weight of 2)
(without intermediate nodes)
52
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
1
6
0
7
Q: P(2,6,2)?
A: 2-6 (still weight of 2)
(without intermediate nodes)
53
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
1
6
0
7
Q: P(2,6,3)?
A: 2->3->6 (weight of 0)
(now with intermediate node 3)
54
Floyd-Warshall Subproblems
1
1
2
1
4
5
-1
2
3
2
6
7
1
0
Final shortest i⤳j path is P(i, j, n)
when we’re allowed to use any vertices
as intermediate nodes.
55
Claim That Doesn’t Require A Proof
Fix source i, and destination j
k ∉ P(i, j, k) OR k∈P(i, j, k)
i
j
(either one of the intermediate vertices is k or it’s not)
56
Case 1: k ∉ P(i, j, k)
Then all internal nodes are from 1,…,k-1.
Q: What can we assert about P(i, j, k)?
i
j
all intermediate vertices are from Vk-1
A: P(i, j, k) = P(i, j, k-1)
57
Case 2: k ∈ P(i, j, k)
Q: What can we assert about P1 and P2?
i
P1
k
j
P2
one (and only one) of the int. nodes is k. (why only one?)
A1: P1 & P2 only contain int. nodes 1,…,k-1
A2: P1 = P(i,k,k-1) & P2 = P(k,j,k-1)
58
Summary of the 2 Cases
Case 1: k ∉ P(i, j, k) => P(i, j, k) = P(i, j, k-1)
i
j
Case 2: k ∈ P(i, j, k) => P1 = P(i,k,k-1) & P2 = P(k,j,k-1)
i
P1
k
j
P2
59
Recurrence for Larger Subproblems
∀ i, j, k and where i,j,k={1, …, n}
P(i, j, k): shortest i ⤳ j path with all intermediate
nodes from Vk={1,…,k} (or null)
L(i, j, k): w(P(i, j, k)) (and +∞ for null paths)
L(i, j, k) = min
L(i, j, k-1)
With appropriate
base cases.
L(i, k, k-1) + L(k, j, k-1)
60
Floyd-Warshall Algorithm
Let A be an nxnxn 3D array.
A[i][j][k] = shortest i⤳j path with Vk as intermediate nodes
procedure Floyd-Warshall(G(V,E), weights C):
Base Cases: A[i][i][0]
61
Floyd-Warshall Algorithm
Let A be an nxnxn 3D array.
A[i][j][k] = shortest i⤳j path with Vk as intermediate nodes
procedure Floyd-Warshall(G(V,E), weights C):
Base Cases: A[i][i][0] = 0
A[i][j][0] =
62
Floyd-Warshall Algorithm
Let A be an nxnxn 3D array.
A[i][j][k] = shortest i⤳j path with Vk as intermediate nodes
procedure Floyd-Warshall(G(V,E), weights C):
Base Cases: A[i][i][0] = 0
A[i][j][0] = Ci,j if (i,j) ∈ E
+∞ if (i,j) ∉ E
for k = 1, …, n:
for i = 1, …, n:
for j = 1, …, n:
A[i][j][k] = min {A[i][j][k-1],
A[i][k][k-1] + A[k][j][k-1]}
63
Correctness & Runtime
Correctness: induction on i,j,k & correctness of recurrence
Runtime: O(n3) (b/c n3 subproblems, O(1) for each one)
procedure Floyd-Warshall(G(V,E), weights C):
Base Cases: A[i][i][0] = 0
A[i][j][0] = Ci,j if (i,j) ∈ E
+∞ if (i,j) ∉ E
for k = 1, …, n:
for i = 1, …, n:
for j = 1, …, n:
A[i][j][k] = min {A[i][j][k-1],
64
A[i][k][k-1] + A[k][j][k-1]}
Detecting Negative Cycles
Just check the A[i][i][n] for each i!
Let C be a negative cycle with |C| = l
=>for any vertex j on C, A[j][j][l] ≤ 0
=> therefore A[j][j][n] will be negative
65
Path Reconstruction
Similar to BF but keep successors instead
of predecessors.
(exercise)
66
Dijkstra, BF, FW
Dijkstra
BF
FW
Single-Source
/All Pairs
SingleSource
SingleSource
All Pairs
Run-time
O(mlog(n))
O(mn)
O(n3)
Negative Edges
No
Yes
Yes
Negative Cycles No
No, but
No, but
can detect can detect
67
Will see a parallel version of BF in last
lecture.
Next Week: Intractability, P vs NP &
What to Do for NP-hard Problems?
68
```