0% found this document useful (0 votes)
38 views42 pages

Bellman-Ford Algorithm Explained

The document discusses the Bellman-Ford algorithm for finding the single-source shortest paths (SSSP) in weighted digraphs, emphasizing its ability to handle negative weight arcs but not negative weight cycles. It outlines the algorithm's structure, time complexity, and how it compares to Dijkstra's algorithm, which is faster for graphs without negative weights. Additionally, it touches on the All Pairs Shortest Path (APSP) problem and the time complexity of computing a distance matrix for all pairs of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views42 pages

Bellman-Ford Algorithm Explained

The document discusses the Bellman-Ford algorithm for finding the single-source shortest paths (SSSP) in weighted digraphs, emphasizing its ability to handle negative weight arcs but not negative weight cycles. It outlines the algorithm's structure, time complexity, and how it compares to Dijkstra's algorithm, which is faster for graphs without negative weights. Additionally, it touches on the All Pairs Shortest Path (APSP) problem and the time complexity of computing a distance matrix for all pairs of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

SSSP: Bellman-Ford Algorithm

algorithm Bellman-Ford( weighted digraph (G, c); node s )


array dist[n] = {1, 1, . . .}
dist[s] 0
for i from 0 to n 1 do
for x 2 V (G) do
for v 2 V (G) do
dist[v] min(dist[v], dist[x] + c(x, v))
end for
end for
end for
return dist
end
Time complexity – ⇥(n3 ); unlike the Dijkstra’s algorithm, it handles negative
weight arcs (but no negative weight cycles making the SSSP senseless).

34 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Bellman-Ford Algorithm

Slower than Dijkstra’s algorithm when all arcs are nonnegative.


Basic idea as in Dijkstra’s: to find the single-source shortest paths
(SSSP) under progressively relaxing restrictions.
• Dikstra’s: one node a time based on their current distance
estimate.
• Bellman-Ford: all nodes at “level” 0, 1, . . . , n 1 in turn.
• Level of a node v – the minimum possible number of arcs in a
minimum weight path to that node from the source s.

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

Proving Why Bellman-Ford Algorithm Works

Just as for Dijkstra’s, the update ensures dist[v] never increases.

Induction by the level i of the nodes:


• Base case: i = 0; the result is true due to initialisation:
dist[s] = 0; dist[v] = 1; v 2 V \s.
• Induction hypothesis: dist[v]; v 2 V , are true for i 1.
• Induction step for a node v at level i:
• Due to no negative weight cycles, a min-weight s-to-v path, ,
has i arcs.
• If y is the last node before v and 1 the subpath to y, then
dist[y]  | 1 | by the induction hypothesis.
• Thus by the update rule:

dist[v]  dist[y] + c(y, v)  | 1 | + c(y, v)  | |

as required at level i.
37 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Illustrating Bellman-Ford Algorithm

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

Illustrating Bellman-Ford Algorithm

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

Illustrating Bellman-Ford Algorithm

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

Illustrating Bellman-Ford Algorithm

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

Illustrating Bellman-Ford Algorithm

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

Illustrating Bellman-Ford Algorithm (Alternative Form)


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 = 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

Illustrating Bellman-Ford Algorithm (Alternative Form)

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

Illustrating Bellman-Ford Algorithm (Alternative Form)

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

Comments on Bellman-Ford Algorithm

• This (non-greedy) algorithm handles negative weight arcs, but


not negative weight cycles.
• Running time with the two innermost nested for-loops:
O(n3 ).
• Runs slower than the Dijkstra’s algorithm since considers all
nodes at “level” i = 0, 1, . . . , n 1, in turn.
• The alternative form where the two inner-most for-loops are
replaced with: for (u, v) 2 E(V ) runs in time O(nm).
• The outer for-loop (by i) in this case can be terminated after
no distance changes during the iteration (e.g., after i = 2 in
the example on Slide 39).
• Bellman-Ford algorithm can be modified to detect negative
weight cycle (see Textbook, Exercise 6.3.4)

40 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

All Pairs Shortest Path (APSP) Problem

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.

• The APSP complexity ⇥(n3 ) for the adjacency matrix version


of the Dijkstra’s SSSP algorithm: An,m = n2 .

• The APSP complexity ⇥(n2 m) for the Bellman-Ford SSSP


algorithm: An,m = mn.

41 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

All Pairs Shortest Path (APSP) Problem


Floyd’s algorithm – one of the known simpler algorithms for computing
the distance matrix (three nested for-loops; ⇥(n3 ) time complexity):

1 Number all nodes (say, from 0 to n 1).


2 At each step k, maintain the matrix of shortest distances from node
i to node j, not passing through nodes higher than k.
3 Update the matrix at each step to see whether the node k shortens
the current best distance.
An alternative to running the SSSP algorithm from each node.
• Better than the Dijkstra’s algorithm for dense graphs, probably not
for sparse ones.
• Unlike the Dijkstra’s algorithm, can handle negative costs.
• Based on Warshall’s algorithm (just tells whether there is a path from
node i to node j, not concerned with length).
42 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Floyd’s Algorithm

algorithm Floyd( weighted digraph (G, c) )


Initialisation: for u, v 2 V (G) do D[u, v] c(u, v) end for
for x 2 V (G) do
for u 2 V (G) do
for v 2 V (G) do
D[u, v] min{D[u, v], D[u, x] + D[x, v]}
end for
end for
end for

This algorithm is based on dynamic programming principles.


At the bottom of the outer for-x-loop, D[u, v] for each u, v 2 V (G) is
the length of the shortest path from u to v passing through intermediate
nodes x having been seen in that loop.
43 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Illustrating 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

Illustrating Floyd’s Algorithm: x = 0

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

Illustrating Floyd’s Algorithm: x = 1

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

Illustrating Floyd’s Algorithm: x = 2

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

Illustrating Floyd’s Algorithm: x = 3

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

Illustrating Floyd’s Algorithm: x = 4

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

Proving Why Floyd’s Algorithm Works

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

Proving Why Floyd’s Algorithm Works

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

Floyd’s Algorithm: Example 2

a d
3

7
3 5

2 c f g
6 9

8
4 1

b e
3

Computing all-pairs shortest paths

52 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Floyd’s Algorithm: Example 2 Initialisation

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

Floyd’s Algorithm: Example 2 x a

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

Floyd’s Algorithm: Example 2 x b

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

Floyd’s Algorithm: Example 2 x c

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

Floyd’s Algorithm: Example 2 x d

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

Floyd’s Algorithm: Example 2 x e

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

Floyd’s Algorithm: Example 2 x f

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

Computing Actual Shortest Paths

• In addition to knowing the shortest distances, the shortest


paths are often to be reconstructed.
• The Floyd’s algorithm can be enhanced to compute also the
predecessor matrix ⇧ = [⇡ij ]n,n
i,j=1,1 where vertex ⇡i,j precedes
vertex j on a shortest path from vertex i; 1  i, j  n.

Compute a sequence ⇧(0) , ⇧(1) , . . . ⇧(n) ,


(k)
where vertex ⇡i,j precedes the vertex j on a shortest path from
vertex i with all intermediate vertices in V(k) = {1, 2, . . . , k}.

For case of no intermediate vertices:



(0) NIL if i = j or c[i, j] = 1
⇡i,j =
i if i 6= j and c[i, j] < 1

60 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Computing Actual Shortest Paths

• In addition to knowing the shortest distances, the shortest


paths are often to be reconstructed.
• The Floyd’s algorithm can be enhanced to compute also the
predecessor matrix ⇧ = [⇡ij ]n,n
i,j=1,1 where vertex ⇡i,j precedes
vertex j on a shortest path from vertex i; 1  i, j  n.

Compute a sequence ⇧(0) , ⇧(1) , . . . ⇧(n) ,


(k)
where vertex ⇡i,j precedes the vertex j on a shortest path from
vertex i with all intermediate vertices in V(k) = {1, 2, . . . , k}.

For case of no intermediate vertices:



(0) NIL if i = j or c[i, j] = 1
⇡i,j =
i if i 6= j and c[i, j] < 1

60 / 69
Outline Shortest path Dijkstra Bellman-Ford All-pairs Floyd

Floyd’s Algorithm with Predecessors

algorithm FloydPred( weighted digraph (G, c) )

D c Create initial distance matrix from weights.


⇧ ⇧(0) Initialize predecessors from c as in Slide 60.

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

Illustrating Floyd’s Algorithm with Predecessors

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

Illustrating Floyd’s Algorithm with Predecessors: k = 1

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

Illustrating Floyd’s Algorithm with Predecessors: k = 2

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

Illustrating Floyd’s Algorithm with Predecessors: k = 3

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

Illustrating Floyd’s Algorithm with Predecessors: k = 4

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

Illustrating Floyd’s Algorithm with Predecessors: k = 5

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

Getting Shortest Paths from ⇧ Matrix

The recursive algorithm using the predecessor matrix ⇧ = ⇧(n) to


print the shortest path between vertices i and j:

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

Illustrating PrintPath Algorithm

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

PrintPath( ⇧(5) , 5, 3 ) PrintPath( ⇧(5) , 1, 2 )


! PrintPath( ⇧(5) , 5, ⇡5,3 = 4) ! PrintPath( ⇧(5) , 1, ⇡1,2 = 3)
! PrintPath( ⇧(5) , 5, ⇡5,4 = 5) ! PrintPath( ⇧(5) , 1, ⇡1,3 = 4)
print 5 ! PrintPath( ⇧(5) , 1, ⇡1,4 = 5)
print 4 ! PrintPath( ⇧(5) , 1, ⇡1,5 = 1)
print 3 print 1
print 5
print 4
print 3
print 2
69 / 69

You might also like