Bellman-Ford Algorithm Explained
Bellman-Ford Algorithm Explained
34 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Bellman-Ford Algorithm
Theorem 6.9
If a graph G contains no negative weight cycles, then after the ith
iteration of the outer for-loop, the element dist[v] contains the
minimum weight of a path to v for all nodes v with level at most i.
36 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
as required at level i.
37 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
a b c d e
2 0 0 1 1 1 1
a
2 1 0 3 1 1 1
-2
2 0 0 1 3 5
1 d 3 0 0 1 2 0
-1 2 4 0 0 1 2 1
4 -3
c
6
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
3
a b c d e
2 0 0 1 1 1 1
a
2 1 0 3 1 1 1
-2
2 0 0 1 3 5
1 1 d 3 0 0 1 2 0
-1 2 4 0 0 1 2 1
-1 4 -3
c
6
1
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
0
a b c d e
2 0 0 1 1 1 1
a
2 1 0 3 1 1 1
-2
2 0 0 1 3 5
1 3 d 3 0 0 1 2 0
-1 2 4 0 0 1 2 1
-1 4 -3
c
6
5
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
0
a b c d e
2 0 0 1 1 1 1
a
2 1 0 3 1 1 1
-2
2 0 0 1 3 5
1 2 d 3 0 0 1 2 0
-1 2 4 0 0 1 2 1
-1 4 -3
c
6
-1
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 b i dist[x]
0
a b c d e
2 0 0 1 1 1 1
a
2 1 0 3 1 1 1
-2
2 0 0 1 3 5
1 2 d 3 0 0 1 2 0
-1 2 4 0 0 1 2 1
-1 4 -3
c
6
-1
e
38 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Iteration i = 0
x, v Distance d[v] min{d[v], d[x] + c(x, v)} a b c d e
0 1 1 1 1
a, b d[b] min{1, 0 + 3} = 3 0 3 1 1 1
a, c d[c] min{1, 0 1} = 1 0 3 1 1 1
b, a d[a] min{0, 3 + 2} = 0 0 3 1 1 1
b, d d[d] min{1, 3 + 2} = 5 0 3 1 5 1
c, b d[b] min{3, 1 + 1} = 0 0 0 1 5 1
c, d d[d] min{5, 1 + 4} = 3 0 0 1 3 1
c, e d[e] min{1, 1 + 6} = 5 0 0 1 3 5
d, b d[b] min{0, 3 2} = 0 0 0 1 3 5
d, c d[c] min{ 1, 3 + 2} = 1 0 0 1 3 5
d, e d[e] min{5, 3 3} = 0 0 0 1 3 0
39 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Arc (x, v): a,b a,c b,a b,d c,b c,d c,e d,b d,c d,e
c(x, v): 3 1 2 2 1 4 6 2 2 3
Iteration i = 1
x, v Distance d[v] min{d[v], d[x] + c(x, v)} a b c d e
0 0 1 3 0
a, b d[b] min{0, 0 + 3} = 0 0 0 1 3 0
a, c d[c] min{ 1, 0 1} = 1 0 0 1 3 0
b, a d[a] min{0, 0 + 2} = 0 0 0 1 3 0
b, d d[d] min{3, 0 + 2} = 2 0 0 1 2 0
c, b d[b] min{0, 1 + 1} = 0 0 0 1 2 0
c, d d[d] min{2, 1 + 4} = 2 0 0 1 2 0
c, e d[e] min{0, 1 + 6} = 0 0 0 1 2 0
d, b d[b] min{0, 2 2} = 0 0 0 1 2 0
d, c d[c] min{ 1, 2 + 2} = 1 0 0 1 2 0
d, e d[e] min{0, 2 3} = 1 0 0 1 2 1
39 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Arc (x, v): a,b a,c b,a b,d c,b c,d c,e d,b d,c d,e
c(x, v): 3 1 2 2 1 4 6 2 2 3
Iteration i = 2..4
x, v Distance d[v] min{d[v], d[x] + c(x, v)} a b c d e
0 0 1 2 1
a, b d[b] min{0, 0 + 3} = 0 0 0 1 2 1
a, c d[c] min{ 1, 0 1} = 1 0 0 1 2 1
b, a d[a] min{0, 0 + 2} = 0 0 0 1 2 1
b, d d[d] min{2, 0 + 2} = 2 0 0 1 2 1
c, b d[b] min{0, 1 + 1} = 0 0 0 1 2 1
c, d d[d] min{2, 1 + 4} = 2 0 0 1 2 1
c, e d[e] min{ 1, 1 + 6} = 1 0 0 1 2 1
d, b d[b] min{0, 3 2} = 0 0 0 1 2 1
d, c d[c] min{ 1, 3 + 2} = 1 0 0 1 2 1
d, e d[e] min{ 1, 3 3} = 1 0 0 1 2 1
39 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
40 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Given a weighted digraph (G, c), determine for each pair of nodes
u, v 2 V (G) (the length of) a minimum weight path from u to v.
⇥ ⇤
Convenient output: a distance matrix D = D[u, v] u,v2V (G)
• Time complexity ⇥(nAn,m ) of computing the matrix D by
finding the single-source shortest paths (SSSP) from each
node as the source in turn.
• An=|V (G)|,m=|E(G)| – the complexity of the SSSP algorithm.
41 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Floyd’s Algorithm
3 1 2 0 1 2 3 4
3
0 0 3 1 1 1
2 1 6 2 0 1 2 1 7
6 7
0 2 2 6 1 1 0 4 6 7
-2 6 7
3 4 1 2 2 0 3 5
1 4 1 1 1 1 0
2 3
-1
Adjacency/cost matrix c[u, v]
4 -3 0
2 0
6 0
4 0
44 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 2 0 1 2 3 4
3
0 0 3 1 1 1
2 1 6 2 0 1 2 1 7
6 7
0 2 2 6 1 1 0 4 6 7
-2 6 7
3 4 1 2 2 0 3 5
1 4 1 1 1 1 0
2 3
-1
Distance matrix D0 [u, v]
-3 D0 [1, 2] = min{1, 2c[1,0] 1c[0,1] } = 1
2 4
0
6 0
4 0
45 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 2 0 1 2 3 4
3
0 0 3 1 5 1
2 1 6 2 0 1 2 1 7
6 7
0 2 2 6 3 1 0 3 6 7
-2 6 7
3 4 0 2 1 0 3 5
1 4 1 1 1 1 0
2 3
-1
Distance matrix D1 [u, v]
-3 D1 [0, 3] = min{1, 3D0 [0,1] + 2D0 [1,3] } = 5
2 4
D1 [2, 3] = min{4, 1D0 [2,1] + 2D0 [1,3] } = 3
6 D1 [3, 2] = min{2, 2D0 [3,1] + 1D0 [1,2] } = 1
4 0
46 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 0 1 2 3 4
1 2 3
0 0 0 1 2 5
2 1 6 2 0 1 2 7 7
6 7
0 2 6 3 1 0 3 6 7
2 -2 6 7
3 4 0 2 1 0 3 5
1 4 1 1 1 1 0
2 3
-1
Distance matrix D2 [u, v]
-3 D2 [0, 1] = min{3, 1D1 [0,2] + 1D1 [2,1] } = 0
2 4 D2 [0, 3] = min{5, 1D1 [0,2] + 3D1 [2,3] } = 2
6 D2 [0, 4] = min{1, 1D1 [0,2] + 6D1 [2,4] } = 5
4 D2 [1, 4] = min{1, 1D1 [1,2] + 6D1 [2,4] } = 7
47 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 2 0 1 2 3 4
3
0 0 0 1 2 1
2 1 6 2 0 1 2 1 7
6 7
0 2 2 6 3 1 0 3 0 7
-2 6 7
3 4 0 2 1 0 3 5
1 4 1 1 1 1 0
2 3
-1
Distance matrix D3 [u, v]
-3 D3 [0, 4] = min{5, 2D2 [0,3] 3D2 [3,4] } = 1
2 4
D3 [1, 4] = min{7, 2D1 [1,3] 3D1 [3,4] } = 1
6 D3 [2, 4] = min{6, 3D1 [2,3] 3D1 [3,4] } = 0
4 0
48 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
3 1 2 0 1 2 3 4
3
0 0 0 1 2 1
2 1 6 2 0 1 2 1 7
6 7
0 2 2 6 3 1 0 3 0 7
-2 6 7
3 4 0 2 1 0 3 5
1 4 1 1 1 1 0
2 3
-1
Final distance matrix D ⌘ D4 [u, v]
4 -3 0
2 0
6 0
4 0
49 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Theorem 6.12: At the bottom of the outer for-loop, for all nodes u and v,
D[u, v] contains the minimum length of all paths from u to v that are
restricted to using only intermediate nodes that have been seen in the
outer for-loop.
When algorithm terminates, all nodes have been seen and D[u, v] is the length
of the shortest u-to-v path.
Notation: Sk – the set of nodes seen after k passes through this loop; Sk -path
– one with all intermediate nodes in Sk ; Dk – the corresponding value of D.
Induction on the outer for-loop:
• Base case: k = 0; S0 = ;, and the result holds.
• Induction hypothesis: It holds after k 0 times through the loop.
• Inductive step: To show that Dk+1 [u, v] after k + 1 passes
through this loop is the minimum length of an u-to-v Sk+1 -path.
50 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Inductive step: S
Suppose that x is the last node seen in the loop, so Sk+1 = Sk {x}.
• Fix an arbitrary pair of nodes u, v 2 V (G) and let L be the
min-length of an u-to-v Sk+1 -path, so that obviously
L Dk+1 [u, v].
• To show that also Dk+1 [u, v] L, choose an u-to-v Sk+1 -path of
length L. If x 2
/ , the result follows from the induction hypothesis.
• If x 2 , let 1 and 2 be, respectively, the u-to-x and x-to-v
subpaths. Then 1 and 2 are Sk -paths and by the inductive
hypothesis,
L | 1| + | 2| Dk [u, x] + Dk [x, v] Dk+1 [u, v]
Non-negativity of the weights is not used in the proof, and Floyd’s algorithm
works for negative weights (but negative weight cycles should not be present).
51 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
a d
3
7
3 5
2 c f g
6 9
8
4 1
b e
3
52 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
Initialisation: c(u,v)]
z2 }| 3{
a 0 2 3 3 1 1 1
b6 2 0 4 1 3 1 1 7
6 7
c6 3 4 0 5 1 6 1 7
⇥ ⇤ 6 7
D[u, v] d6
6 3 1 5 0 1 7 1 7
7
u,v2V (G)
e6 1 3 1 1 0 8 1 7
6 7
f4 1 1 6 7 8 0 9 5
g 1 1 1 1 1 9 0
a b c d e f g
for x 2 V = {a, b, c, d, e, f, g} do
for u 2 V = {a, b, c, d, e, f, g} do
for v 2 V = {a, b, c, d, e, f, g} do
D[u, v] min {D[u, v], D[u, x] + D[x, v]}
end for
end for
end for
53 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g 3
2 a b c d e f
a 0 2 3 3 1 1 1
f f b6
6 2 0 4 5 3 1 1 7 7
6 6 1 7
e e
c
6 3 4 0 5 1 7
d6
6 3 5 5 0 1 7 1 7 7
e6 1 3 1 1 0 8 1 7
d d
6 7
f4 1 1 6 7 8 0 9 5
g 1 1 1 1 1 9 0
c c | {z }
D[u, v] min {D[u, v], D[u, a] + D[a, v]} ;
b b (u, v) 2 V 2
E.g.,
a a
D[b, d] min{D[b, d], D[b, a] + D[a, d]}
= min{1, 2 + 3} = 5
a
54 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g 3
2 a b c d e f
a 0 2 3 3 5 1 1
f f b6
6 2 0 4 5 3 1 1 7 7
6 6 1 7
e e
c
6 3 4 0 5 1 7
d6
6 3 5 5 0 8 7 1 7 7
e6 5 3 1 8 0 8 1 7
d d
6 7
f4 1 1 6 7 8 0 9 5
g 1 1 1 1 1 9 0
c c | {z }
D[u, v] min {D[u, v], D[u, b] + D[b, v]} ;
b b (u, v) 2 V 2
E.g.,
a a
D[a, e] min{D[a, e], D[a, b] + D[b, e]}
= min{1, 2 + 3} = 5
b
55 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g 3
2 a b c d e f
a 0 2 3 3 4 9 1
f f b6
6 2 0 4 5 3 10 1 7 7
6 1 6 1 7
e e
c
6 3 4 0 5 7
d6
6 3 5 5 0 6 7 1 7 7
e6 4 3 1 6 0 7 1 7
d d
6 7
f4 9 10 6 7 7 0 9 5
g 1 1 1 1 1 9 0
c c | {z }
D[u, v] min {D[u, v], D[u, c] + D[c, v]} ;
b b (u, v) 2 V 2
E.g.,
a a
D[a, f ] min{D[a, f ], D[a, c] + D[c, f ]}
= min{1, 3 + 6} = 9
c
56 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g 3
2 a b c d e f
a 0 2 3 3 4 9 1
f f b6
6 2 0 4 5 3 10 1 7 7
6 6 1 7
e e
c
6 3 4 0 5 1 7
d6
6 3 5 5 0 8 7 1 7 7
e6 4 3 1 8 0 7 1 7
d d
6 7
f4 9 10 6 7 7 0 9 5
g 1 1 1 1 1 9 0
c c | {z }
D[u, v] min {D[u, v], D[u, d] + D[d, v]} ;
b b (u, v) 2 V 2
E.g.,
a a
D[a, f ] min{D[a, f ], D[a, d] + D[d, f ]}
= min{9, 3 + 7} = 9
d
57 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g 3
2 a b c d e f
a 0 2 3 3 4 9 1
f f b6
6 2 0 4 5 3 10 1 7 7
6 5 1 6 1 7
e e
c
6 3 4 0 7
d6
6 3 5 5 0 8 7 1 7 7
e6 4 3 1 8 0 7 1 7
d d
6 7
f4 9 10 6 7 7 0 9 5
g 1 1 1 1 1 9 0
c c | {z }
D[u, v] min {D[u, v], D[u, e] + D[e, v]} ;
b b (u, v) 2 V 2
E.g.,
a a
D[b, f ] min{D[b, f ], D[b, e] + D[e, f ]}
= min{9, 3 + 7} = 9
e
58 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
g g g 3
2 a b c d e f
a 0 2 3 3 4 9 18
f f b66 2 0 4 5 3 10 19 7 7
6 1 6 15 7
e e
c
6 3 4 0 5 7
d66 3 5 5 0 8 7 16 7 7
e6 4 3 1 8 0 7 16 7
d d
6 7
f4 9 10 6 7 7 0 9 5
g 18 19 15 16 16 9 0
c c | {z }
D[u, v] min {D[u, v], D[u, f ] + D[f, v]} ;
b b (u, v) 2 V 2
E.g.,
a a
D[a, g] min{D[a, g], D[a, f ] + D[f, g]}
= min{1, 9 + 9} = 18
f
59 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
60 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
60 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
for k from 1 to n do
for i from 1 to n do
for j from 1 to n do
if D[i, j] > D[i, k] + D[k, j] then
D[i, j] D[i, k] + D[k, j]; ⇧[i, j] ⇧[k, j]
end if
end for
end for
end for
61 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 3
2 1 0 3 8 1 4
26 1 0 1 1 7 7
6 7
3 4 D (0)
= 36
6 1 4 0 1 1 7
7
7 1 44 2 1 5 0 1 5
8
1 3 5 1 1 1 6 0
2
1 2 3 4 5
2 3
4 5 1 NIL 1 1 NIL 1
26 NIL NIL NIL 2 2 7
6 7
⇧(0) = 36
6 NIL 3 NIL NIL NIL 7
7
5 4 44 4 NIL 4 NIL NIL 5
6 5 NIL NIL NIL 5 NIL
62 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 3
2 1 0 3 8 1 4
26 1 0 1 1 7 7
6 7
3 4 D (1)
= 36
6 1 4 0 1 1 7
7
7 1 44 2 5 5 0 2 5
8
1 3 5 1 1 1 6 0
2
1 2 3 4 5
2 3
4 5 1 NIL 1 1 NIL 1
26 NIL NIL NIL 2 2 7
6 7
⇧(1) = 36
6 NIL 3 NIL NIL NIL 7
7
5 4 44 4 1 4 NIL 1 5
6 5 NIL NIL NIL 5 NIL
63 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 3
2 1 0 3 8 4 4
26 1 0 1 1 7 7
6 7
3 4 D (2)
= 36
6 1 4 0 5 11 7
7
7 1 44 2 5 5 0 2 5
8
1 3 5 1 1 1 6 0
2
1 2 3 4 5
2 3
4 5 1 NIL 1 1 2 1
26 NIL NIL NIL 2 2 7
6 7
⇧(2) = 36
6 NIL 3 NIL 2 2 7
7
5 4 44 4 1 4 NIL 1 5
6 5 NIL NIL NIL 5 NIL
64 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
1 2 3 4 5
2 3
2 1 0 3 8 4 4
26 1 0 1 1 7 7
6 7
3 4 D (3)
= 36
6 1 4 0 5 11 7
7
7 1 44 2 1 5 0 2 5
8
1 3 5 1 1 1 6 0
2
1 2 3 4 5
2 3
4 5 1 NIL 1 1 2 1
26 NIL NIL NIL 2 2 7
6 7
⇧(3) = 36
6 NIL 3 NIL 2 2 7
7
5 4 44 4 3 4 NIL 1 5
6 5 NIL NIL NIL 5 NIL
65 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
21 2 3 4 5
3
2 1 0 3 1 4 4
26 3 0 4 1 1 7
6 7
3 4 D (4)
= 36
67 4 0 5 3 7
7
7 1 44 2 1 5 0 2 5
8
1 3 5 8 5 1 6 0
2
1 2 3 4 5
2 3
4 5 1 NIL 1 4 2 1
26 4 NIL 4 2 1 7
6 7
⇧(4) = 36
6 4 3 NIL 2 1 7
7
5 4 44 4 3 4 NIL 1 5
6 5 4 3 4 5 NIL
66 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
21 2 3 4 5
3
2 1 0 1 3 2 4
26 3 0 4 1 1 7
6 7
3 4 D (5)
= 36
67 4 0 5 3 7
7
7 1 44 2 1 5 0 2 5
8
1 3 5 8 5 1 6 0
2
1 2 3 4 5
2 3
4 5 1 NIL 3 4 5 1
26 4 NIL 4 2 1 7
6 7
⇧(5) = 36
6 4 3 NIL 2 1 7
7
5 4 44 4 3 4 NIL 1 5
6 5 4 3 4 5 NIL
67 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
algorithm PrintPath( ⇧, i, j )
if i = j then print i
else
if ⇡i,j = NIL then print “no path from i to j”
else
PrintPath( ⇧, i, ⇡i,j )
print j
end if
end if
68 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd
2 1 2 3 4 5 3
1 NIL 3 4 5 1
26 4 NIL 4 2 1 7
6 7
⇧(5) = 36
6 4 3 NIL 2 1 7
7
44 4 3 4 NIL 1 5
5 4 3 4 5 NIL