0% found this document useful (0 votes)
24 views26 pages

Bellman Ford & Floyd Warshall

The document discusses two algorithms for solving the single-source shortest path problem: Bellman-Ford and Dijkstra’s algorithm. It explains the workings of the Bellman-Ford algorithm, including its dynamic programming approach and limitations such as the count-to-infinity problem. Additionally, it covers the all-pairs shortest path problem and introduces the Floyd-Warshall algorithm as an efficient alternative.

Uploaded by

subhrand66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

Bellman Ford & Floyd Warshall

The document discusses two algorithms for solving the single-source shortest path problem: Bellman-Ford and Dijkstra’s algorithm. It explains the workings of the Bellman-Ford algorithm, including its dynamic programming approach and limitations such as the count-to-infinity problem. Additionally, it covers the all-pairs shortest path problem and introduces the Floyd-Warshall algorithm as an efficient alternative.

Uploaded by

subhrand66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Single-source shortest paths

• Two classic algorithms to solve single-source


shortest path problem
– Bellman-Ford algorithm
• A dynamic programming algorithm
• Works when some weights are negative
– Dijkstra’s algorithm
• A greedy algorithm
• Faster than Bellman-Ford
• Works when weights are all non-negative
Bellman-Ford algorithm
Observation:
• If there is a negative cycle, there is no solution
– Add this cycle again can always produces a less weight path
• If there is no negative cycle, a shortest path has at most |V|-1 edges

Idea:
• Solve it using dynamic programming
• For all the paths have at most 0 edge, find all the shortest paths
• For all the paths have at most 1 edge, find all the shortest paths
• …
• For all the paths have at most |V|-1 edge, find all the shortest paths
Bellman-Ford algorithm
Bellman-Ford(G, s)
for each v in G.V{ //Initialize 0-edge shortest paths
if(v==s) =0; else=//set the 0-edge shortest distance
from s to v
; //set the predecessor of v on the shortest path
} //bottom-up construct 0-to-(|V|-1)-edges shortest paths
Repeat |G.V|-1 times {
for each edge (u, v) in G.E{
if(+ ){
;
;
} //test negative cycle
}
for each edge (u, v) in G.E{
If (+ ) return false; // there is no solution
}
T(n)=O(VE)=O()
return true;
Bellman-Ford algorithm
e.g.
20
0 ∞ ∞
10 1
1 2 3

What is the 0-edge shortest path from 1 to 1?


<> with path weight 0
What is the 0-edge shortest path from 1 to 2?
<>with path weight

What is the 0-edge shortest path from 1 to 3?


<>with path weight
Bellman-Ford algorithm
e.g. ∞ > 0+20
20 𝑑1 , 3=20
0 ∞¿10 ∞¿ 20
1
10
2
1
3
∞ > 0+10
𝑑1 , 2=10
What is the at most 1-edge shortest path from 1 to 1? +1

<> with path weight 0 𝑑1 , 3 𝑢𝑛𝑐h𝑎𝑛𝑔𝑒𝑑


What is the at most 1-edge shortest path from 1 to 2?
<1, 2> with path weight 10

What is the at most 1-edge shortest path from 1 to 3?


<1, 3> with path weight 20

In Bellman-Ford, they are calculated by scan all edges once


Bellman-Ford algorithm
e.g. 0 +20=20
20 𝑑1 , 3 𝑢𝑛𝑐h𝑎𝑛𝑔𝑒𝑑
0 ¿10
10 ¿ 11
20

1
10
2
1
3
10=0+ 10
𝑑1 , 2 𝑢𝑛𝑐h𝑎𝑛𝑔𝑒𝑑
What is the at most 2-edges shortest path from 1 to 1? +1

<> with path weight 0 𝑑1 , 3=11


What is the at most 2-edges shortest path from 1 to 2?
<1, 2> with path weight 10

What is the at most 2-edges shortest path from 1 to 3?


<1, 2, 3> with path weight 11

In Bellman-Ford, they are calculated by scan all edges once


Bellman-Ford algorithm
All 0 edge shortest paths

5
∞ ∞
-2
2 3
6
-3
0
8
1 7
-4
7 2
4 5
∞ ∞
9
Bellman-Ford algorithm
Calculate all at most 1 edge
shortest paths 5
∞¿ 6 ∞/
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
∞// ∞/
9
Bellman-Ford algorithm
Calculate all at most 2 edges
shortest paths 5
6 ¿6 ∞ /11
/4
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
7 /7 ∞/2
9
Bellman-Ford algorithm
Calculate all at most 3 edges
shortest paths 5
6 ¿¿62 4 /4
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
7 /7 2 /2
9
Bellman-Ford algorithm
Calculate all at most 4 edges
shortest paths 5
2 ¿2 4 /4
-2
2 3
6
-3
0 /0
8
1 7
-4
7 2
4 5
7 /7 2 /-2
9
Bellman-Ford algorithm
Final result:
5
2 4
-2
2 3
6
-3
0
8
1 7
-4
7 2
4 5
7 -2
9
What is the shortest path 1, 4, 3, 2, 5
from 1 to 5?
What is the shortest path
What is weight of this path? -2 from 1 to 2, 3, and 4?
Count to infinity problem
• The Bellman–Ford algorithm does not prevent routing loops from happening and
suffers from the count-to-infinity problem.
• The core of the count-to-infinity problem is that if A tells B that it has a path
somewhere, there is no way for B to know if the path has B as a part of it. To see
the problem clearly, imagine a subnet connected like A–B–C–D–E–F, and let the
metric between the routers be "number of jumps".
• Now suppose that A is taken offline. In the vector-update-process B notices that the
route to A, which was distance 1, is down – B does not receive the vector update
from A. The problem is, B also gets an update from C, and C is still not aware of the
fact that A is down – so it tells B that A is only two jumps from C (C to B to A), which
is false. Since B doesn't know that the path from C to A is through itself (B), it
updates its table with the new value "B to A = 2 + 1".
• Later on, B forwards the update to C and due to the fact that A is reachable through
B (From C's point of view), C decides to update its table to "C to A = 3 + 1".
• This slowly propagates through the network until it reaches infinity (in which case
the algorithm corrects itself, due to the relaxation property of Bellman–Ford).
All-pairs shortest paths
• All-pairs shortest path problem: given a
weighted, directed graph G=(V, E), for every
pair of vertices, find a shortest path.
• If there are negative weights, run Bellman-
Ford algorithm |V| times
– T(n)=
• If there are no negative weights, run Dijkstra’s
algorithm |V| times
– T(n)=
All-pairs shortest paths
• There are other algorithms can do it more
efficient, such like Floyd-Warshall algorithm
• Floyd-Warshallalgorithm
– Negative weights may present, but no negative
cycle
– T(n)=
– A dynamic programming algorithm
All-pairs shortest paths
• Floyd-Warshall(G)
Construct the shortest path matrix when there is no
intermediate vertex, D(0);
for(i=1 to |G.V|){
//D(i) is the shortest path matrix when the
intermediate //vertices could be:
Compute D(i) from D(i-1);
}
All-pairs shortest paths
What are the weights of shortest paths with
no intermediate vertices, D(0)?

1 2
1 2 3 4
9
3 1
6 4 0∞ ∞ ∞
2
∞ 0 4
3 3 9 0 2
3 4 4 ∞ 6 ∞ 0
2
All-pairs shortest paths
D(0)
1 2 3 4
1 0∞ ∞∞
2
1 2 ∞0 4
3 3 2 0 9
2
3 4 ∞6 ∞0
6 4
What are the weights of shortest paths with
intermediate vertex 1?
3 D(1)
4
1 2 3 4
9 1 0 ∞ ∞ ∞
2
∞ 0 ∞ 4
3 3 2 0 9
4 ∞ 6 ∞ 0
All-pairs shortest paths
D(1)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2
∞ 0 ∞ 4
3 3 2 0 9
2
3 4 ∞ 6 ∞ 0
6 4
What are the weights of shortest paths with
intermediate vertices 1 and 2?
3 D(2)
4
1 2 3 4
9 1
∞ ∞ ∞
0
2
∞ 0 ∞ 4
3 3 2 0 6
4 ∞ 6 ∞ 0
All-pairs shortest paths
D(2)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2 ∞ 4
∞ 0
2
3 3 2 0 6
3
6 4
4 ∞ 6 ∞ 0
What are the weights of shortest paths with
intermediate vertices 1, 2 and 3?
3 4 D(3)
1 2 3 4
9 1 0∞ ∞ ∞
2
∞ 0 ∞ 4
3 3 2 0 6
4 ∞ 6 ∞ 0
All-pairs shortest paths
D(3)
1 2 3 4
1 0 ∞ ∞ ∞
1 2 2
∞ 0 ∞ 4
3 3 2 0 6
2
3 4 ∞ 6 ∞ 0
6 4
What are the weights of shortest paths with
intermediate vertices 1, 2, 3 and 4?
D(4)
3 4 1 2 3 4
1
9 0∞ ∞ ∞
2
∞ 0 ∞ 4
3 3 2 0 6
4 ∞ 6 ∞ 0
All-pairs shortest paths
Add predecessor information to reconstruct a shortest path
If updated the predecessor i-j in D(k) is the same as the predecessor k-j in D(k-1)
D(0) D(1)
1 2 3 4 1 2 3 4
1 0/n∞ /𝑛∞ /𝑛∞ /𝑛 1 0/n ∞ /𝑛∞ /𝑛∞ /𝑛
2 ∞ /𝑛 0/n ∞ /𝑛 4/2 2 ∞ /𝑛 0/n ∞ /𝑛 4/2
3 3/3 2/ 3 0/n 9/3 3 3/3 2/ 3 0/n 9/3
4 ∞ /𝑛 6/4 ∞ /𝑛 0/n 4 ∞ /𝑛 6/4 ∞ /𝑛 0/n
D(2) D(3)
1 2 3 4 1 2 3 4
1 ∞ /𝑛∞ /𝑛∞ /𝑛
0/n 1 ∞ /𝑛∞ /𝑛∞ /𝑛
0/n
2 ∞ /𝑛 0/n ∞ /𝑛 4/2 2 ∞ /𝑛 0/n ∞ /𝑛 4/2
3 3/3 2/ 3 0/n 6/2 3 3/3 2/ 3 0/n 6/2
4 ∞ /𝑛 6/4 ∞ /𝑛 0/n 4 ∞ /𝑛 6/4 ∞ /𝑛 0/n
All-pairs shortest paths

D(4)
1 2
1 2 3 4
1 0/n∞ /𝑛∞ /𝑛∞ /𝑛 3
2
2 ∞ /𝑛 0/n ∞ /𝑛 4/2 6 4
3 3/3 2/ 3 0/n 6/2
4 ∞ /𝑛 6/4 ∞ /𝑛 0/n
3 4

9
What is the shortest path 3, 2, 4
from 3 to 4?
What is weight of this path? 6
Representation of the Input
We assume that the input is represented by a
weight matrix W= (wij)i,j in E that is defined by

wij= 0 if i=j
wij= w(i,j) if ij and (i,j) in E
wij=  if ij and (i,j) not in E

24
The Floyd-Warshall Algorithm
Floyd-Warshall(W)
n = # of rows of W;
D(0) = W;
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)};
ef;
ef;
ef;
return D(n);

25
Time and Space Requirements
The running time is obviously O(n3). Since n=V,
hence O(V3)

However, in this version, the space requirements


are high. One can reduce the space from O(V3)
to O(V2) by using a single array d.

26

You might also like