Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
 A weighted, directed graph is a collection
vertices connected by weighted edges (where
the weight is some real number).
◦ One of the most common examples of a graph in the
real world is a road map.
 Each location is a vertex and each road connecting locations
is an edge.
 We can think of the distance traveled on a road from one
location to another as the weight of that edge.
ABC PQR
XYZ
3.5 4
1.5
1.7
2.5
ABC PQR XYZ
ABC 0 1.7 3.5
PQR 1.5 0 ∞
XYZ 4 2.5 0
Storing a Weighted, Directed
Graph
 Adjacency Matrix:
◦ Let D be an edge-weighted graph in adjacency-
matrix form
• D(i,j) is the weight of edge (i, j), or  if there is no
such edge.
• Update matrix D, with the shortest path
through immediate vertices.
0 1 2 3
0 0 6 5 ∞
1 ∞ 0 4 3
2 ∞ ∞ 0 2
3 ∞ ∞ ∞ 0
2
1
3
0
6
5
4
3
2
D =
Floyd-Warshall Algorithm
 Given a weighted graph, we want to
know the shortest path from one
vertex in the graph to another.
◦ The Floyd-Warshall algorithm determines
the shortest path between all pairs of
vertices in a graph.
◦ What is the difference between Floyd-
Warshall and Dijkstra’s??
Floyd-Warshall Algorithm
If V is the number of vertices, Dijkstra’s
runs in O(V2)
◦ We could just call Dijkstra |V| times, passing
a different source vertex each time.
◦ O(V  V2) = O(V3)
◦ (Which is the same runtime as the Floyd-
Warshall Algorithm)
 BUT, Dijkstra’s doesn’t work with
negative-weight edges.
Floyd Warshall Algorithm
 Let’s go over the premise of how Floyd-Warshall
algorithm works…
◦ Let the vertices in a graph be numbered from 1 … n.
◦ Consider the subset {1,2,…, k} of these n vertices.
◦ Imagine finding the shortest path from vertex i to vertex j
that uses vertices in the set {1,2,…,k} only.
◦ There are two situations:
1) k is an intermediate vertex on the shortest path.
2) k is not an intermediate vertex on the shortest path.
j
k
i
Floyd Warshall Algorithm
Consider the following directed weighted
graph-
Using Floyd Warshall Algorithm, find the shortest path distance between
every pair of vertices.
Floyd Warshall Algorithm
Solution-
Step-01:
•Remove all the self loops and parallel edges (keeping the lowest
weight edge) from the graph.
•In the given graph, there are neither self edges nor parallel edges.
Step-02:
• Write the initial distance matrix.
• It represents the distance between every pair of vertices in the form of
given weights.
• For diagonal elements (representing self-loops), distance value = 0.
• For vertices having a direct edge between them, distance value =
weight of that edge.
• For vertices having no direct edge between them, distance value = ∞.
Initial distance matrix for the given
graph is,
Floyd Warshall Algorithm
Step-03:
Using Floyd Warshall Algorithm, write the
following 4 matrices-
D0(2,3) = 1
Check via intermediate vertex 1 D0(2,1) + D0(1,3) = ∞+ ∞= ∞
D0(2,1) + D0(1,3) > D0(2,3) So No Change.
D0(2,4) = ∞
Check via intermediate vertex 1 D0(2,1) + D0(1,4) = ∞+ 1= ∞
D0(2,1) + D0(1,4) = D0(2,4) So No Change.
D0(3,2) = ∞
Check via intermediate vertex 1 D0(3,1) + D0(1,2) = 4+ 8= 12
D0(3,1) + D0(1,2) < D0(3,2) So Change D0(3,2) to 12
D0(3,4) = ∞
Check via intermediate vertex 1 D0(3,1) + D0(1,4) = 4+1= 5
D0(3,1) + D0(1,4) < D0(3,4) So Change D0(3,4) to 5
D0(4,2) = 2
Check via intermediate vertex 1 D0(4,1) + D0(1,2) = ∞ + 8= ∞
D0(4,1) + D0(1,2) > D0(4,2) So No Change
D0(4,3) = 9
Check via intermediate vertex 1 D0(4,1) + D0(1,3) = ∞ + ∞ = ∞
D0(4,1) + D0(1,3) > D0(4,2) So No Change
0 ∞ 1
∞ 9 0
4 0 5
𝐷1 =
1 2 3 4
1
2
3
4
8
∞ 0 1 ∞
12
2
0 ∞ 1
∞ 9 0
4 0 5
𝐷1 =
1 2 3 4
1
2
3
4
8
2
12
0 1 ∞
∞ 0
Step-03:
Using Floyd Warshall Algorithm, write the
following 4 matrices-
D1(1,3) = ∞
Check via intermediate vertex 2 D1(1,2) + D1(2,3) = 8+ 1= 9
D1(1,2) + D1(2,3) < D1(1,3) So Change D1(1,3) to 9
D1(1,4) = 1
Check via intermediate vertex 2 D1(1,2) + D1(2,4) = ∞+ ∞= ∞
D0(2,1) + D0(1,4)> D1(1,4) So No Change.
D1(3,1) = 4
Check via intermediate vertex 2 D1(3,2) + D1(2,1) = 12+ ∞ = ∞
D1(3,2) + D1(2,1) > D1(3,1) So No Change
D1(3,4) = 5
Check via intermediate vertex 2 D1(3,2) + D1(2,4) = 12+∞= ∞
D1(3,2) + D1(2,4) > D1(3,4) = 5 So No change
D1(4,1) = ∞
Check via intermediate vertex 1 D1(4,2) + D1(1,2) = 2 + 8= 10
D1(4,2) + D1(1,2) < D1(4,1) So No Change
D1(4,3) = 9
Check via intermediate vertex 2 D1(4,2) + D1(2,3) = 2 + 1= 3
D1(4,2) + D1(2,3) < D1(4,3) So Change D1(4,3) to 3
0 9 1
∞ 3 0
4 0 5
𝐷2 =
1 2 3 4
1
2
3
4
8
∞ 0 1 ∞
12
2
0 ∞ 1
∞ 9 0
4 0 5
𝐷1 =
1 2 3 4
1
2
3
4
8
∞ 0 1 ∞
12
2
0 1
∞ 0
𝐷2 =
1 2 3 4
1
2
3
4
8
∞ 0 ∞
2
9
3
0
1
4 5
12 0
Step-03:
Using Floyd Warshall Algorithm, write the
following 4 matrices-
D2(1,2) = 8
Check via intermediate vertex 3 D2(1,3) + D2(3,2) = 9 + 12= 21
D2(1,2) + D2(2,3) > D2(1,3) So No Change
D2(1,4) = 1
Check via intermediate vertex 3 D2(1,3) + D2(3,4) = 9 + 5 = 14
D2(1,3) + D2(3,4)) > D2(1,4) So No Change
D2(2,1) = ∞
Check via intermediate vertex 3 D2(2,3) + D2(3,1) = 1 + 4= 5
D2(2,3) + D2(3,1) < D2(2,1) So Change D2(2,1) to 5
D2(2,4) = ∞
Check via intermediate vertex 3 D2(2,3) + D2(3,4) = 1 + 5 = 6
D2(2,3) + D2(3,4) < D2(2,4) So Change D2(2,4) to 6
D2(4,1) = ∞
Check via intermediate vertex 3 D2(4,3) + D2(3,1) = 3 + 4= 7
D2(4,3) + D2(3,1) < D2(4,1) So Change D2(4,1) to 7
D2(4,2) = 2
Check via intermediate vertex 3 D2(4,3) + D2(3,2) = 3 + 12 = 15
D2(4,3) + D2(3,2) > D2(4,2) So No Change
0 9 1
∞ 3 0
4 0 5
𝐷2 =
1 2 3 4
1
2
3
4
8
∞ 0 1 ∞
12
2
0 9 1
7 3 0
4 0 5
𝐷3 =
1 2 3 4
1
2
3
4
8
5 0 1 6
12
2
4 0
𝐷3 =
1 2 3 4
1
2
3
4
5 0 1
12
1
0
5
6
0 9
8 1
7 3
2 0
Step-03:
Using Floyd Warshall Algorithm, write the
following 4 matrices-
D3(1,2) = 8
Check via intermediate vertex 4 D3(1,4) + D3(4,2) = 1 + 2= 3
D3(1,4) + D3(4,2) < D3(1,2) So Change D3(1,2) to 3
D3(1,3) = 9
Check via intermediate vertex 4 D3(1,4) + D3(4,3) = 1 + 3= 4
D3(1,4) + D3(4,3) < D3(1,3) So Change D3(1,3) to 4
D3(2,1) = 5
Check via intermediate vertex 4 D3(2,4) + D3(4,1) = 6 + 7= 13
D3(2,4) + D3(4,1) > D3(2,1) So No Change
D3(2,3) = 1
Check via intermediate vertex 4 D3(2,4) + D3(4,3) = 6 + 3= 9
D3(2,4) + D3(4,3) < D3(2,3) So No Change
D3(3,1) = 4
Check via intermediate vertex 4 D3(3,4) + D3(4,1) = 5 + 7= 12
D3(3,4) + D3(4,1) > D3(3,1) So No Change
D3(3,2) = 12
Check via intermediate vertex 4 D3(3,4) + D3(4,2) = 5 + 2= 7
D3(3,4) + D3(4,2) < D3(3,2) So Change D3(3,2) to 7
D3(3,1) = 4
Check via intermediate vertex 4 D3(3,4) + D3(4,1) = 5 + 7= 12
D3(3,4) + D3(4,1) > D3(3,1) So No Change
0 9 1
7 3 0
4 0 5
𝐷3 =
1 2 3 4
1
2
3
4
8
5 0 1 6
12
2
0 4 1
7 3 0
4 0 5
𝐷4 =
1 2 3 4
1
2
3
4
3
5 0 1 6
7
2
The last matrix D4 represents the
shortest path distance between every
pair of vertices.
Floyd Warshall Algorithm -
Example
Consider Vertex 3:
Nothing changes.
Consider Vertex 2:
D(1,3) = D(1,2) + D(2,3)
Consider Vertex 1:
D(3,2) = D(3,1) + D(1,2)
Original weights.
Floyd Warshall Algorithm
 Looking at this example, we can come up
with the following algorithm:
◦ Let D store the matrix with the initial graph edge
information initially, and update D with the
calculated shortest paths.
For k=1 to n {
For i=1 to n {
For j=1 to n
D[i,j] = min(D[i,j],D[i,k]+D[k,j])
}
}
◦ The final D matrix will store all the shortest
paths.

All Pair shortest Path.pptx

  • 1.
  • 2.
    Floyd-Warshall Algorithm  Aweighted, directed graph is a collection vertices connected by weighted edges (where the weight is some real number). ◦ One of the most common examples of a graph in the real world is a road map.  Each location is a vertex and each road connecting locations is an edge.  We can think of the distance traveled on a road from one location to another as the weight of that edge. ABC PQR XYZ 3.5 4 1.5 1.7 2.5 ABC PQR XYZ ABC 0 1.7 3.5 PQR 1.5 0 ∞ XYZ 4 2.5 0
  • 3.
    Storing a Weighted,Directed Graph  Adjacency Matrix: ◦ Let D be an edge-weighted graph in adjacency- matrix form • D(i,j) is the weight of edge (i, j), or  if there is no such edge. • Update matrix D, with the shortest path through immediate vertices. 0 1 2 3 0 0 6 5 ∞ 1 ∞ 0 4 3 2 ∞ ∞ 0 2 3 ∞ ∞ ∞ 0 2 1 3 0 6 5 4 3 2 D =
  • 4.
    Floyd-Warshall Algorithm  Givena weighted graph, we want to know the shortest path from one vertex in the graph to another. ◦ The Floyd-Warshall algorithm determines the shortest path between all pairs of vertices in a graph. ◦ What is the difference between Floyd- Warshall and Dijkstra’s??
  • 5.
    Floyd-Warshall Algorithm If Vis the number of vertices, Dijkstra’s runs in O(V2) ◦ We could just call Dijkstra |V| times, passing a different source vertex each time. ◦ O(V  V2) = O(V3) ◦ (Which is the same runtime as the Floyd- Warshall Algorithm)  BUT, Dijkstra’s doesn’t work with negative-weight edges.
  • 6.
    Floyd Warshall Algorithm Let’s go over the premise of how Floyd-Warshall algorithm works… ◦ Let the vertices in a graph be numbered from 1 … n. ◦ Consider the subset {1,2,…, k} of these n vertices. ◦ Imagine finding the shortest path from vertex i to vertex j that uses vertices in the set {1,2,…,k} only. ◦ There are two situations: 1) k is an intermediate vertex on the shortest path. 2) k is not an intermediate vertex on the shortest path. j k i
  • 7.
    Floyd Warshall Algorithm Considerthe following directed weighted graph- Using Floyd Warshall Algorithm, find the shortest path distance between every pair of vertices.
  • 8.
    Floyd Warshall Algorithm Solution- Step-01: •Removeall the self loops and parallel edges (keeping the lowest weight edge) from the graph. •In the given graph, there are neither self edges nor parallel edges. Step-02: • Write the initial distance matrix. • It represents the distance between every pair of vertices in the form of given weights. • For diagonal elements (representing self-loops), distance value = 0. • For vertices having a direct edge between them, distance value = weight of that edge. • For vertices having no direct edge between them, distance value = ∞.
  • 9.
    Initial distance matrixfor the given graph is, Floyd Warshall Algorithm
  • 11.
    Step-03: Using Floyd WarshallAlgorithm, write the following 4 matrices- D0(2,3) = 1 Check via intermediate vertex 1 D0(2,1) + D0(1,3) = ∞+ ∞= ∞ D0(2,1) + D0(1,3) > D0(2,3) So No Change. D0(2,4) = ∞ Check via intermediate vertex 1 D0(2,1) + D0(1,4) = ∞+ 1= ∞ D0(2,1) + D0(1,4) = D0(2,4) So No Change. D0(3,2) = ∞ Check via intermediate vertex 1 D0(3,1) + D0(1,2) = 4+ 8= 12 D0(3,1) + D0(1,2) < D0(3,2) So Change D0(3,2) to 12 D0(3,4) = ∞ Check via intermediate vertex 1 D0(3,1) + D0(1,4) = 4+1= 5 D0(3,1) + D0(1,4) < D0(3,4) So Change D0(3,4) to 5 D0(4,2) = 2 Check via intermediate vertex 1 D0(4,1) + D0(1,2) = ∞ + 8= ∞ D0(4,1) + D0(1,2) > D0(4,2) So No Change
  • 12.
    D0(4,3) = 9 Checkvia intermediate vertex 1 D0(4,1) + D0(1,3) = ∞ + ∞ = ∞ D0(4,1) + D0(1,3) > D0(4,2) So No Change 0 ∞ 1 ∞ 9 0 4 0 5 𝐷1 = 1 2 3 4 1 2 3 4 8 ∞ 0 1 ∞ 12 2
  • 13.
    0 ∞ 1 ∞9 0 4 0 5 𝐷1 = 1 2 3 4 1 2 3 4 8 2 12 0 1 ∞ ∞ 0
  • 14.
    Step-03: Using Floyd WarshallAlgorithm, write the following 4 matrices- D1(1,3) = ∞ Check via intermediate vertex 2 D1(1,2) + D1(2,3) = 8+ 1= 9 D1(1,2) + D1(2,3) < D1(1,3) So Change D1(1,3) to 9 D1(1,4) = 1 Check via intermediate vertex 2 D1(1,2) + D1(2,4) = ∞+ ∞= ∞ D0(2,1) + D0(1,4)> D1(1,4) So No Change. D1(3,1) = 4 Check via intermediate vertex 2 D1(3,2) + D1(2,1) = 12+ ∞ = ∞ D1(3,2) + D1(2,1) > D1(3,1) So No Change D1(3,4) = 5 Check via intermediate vertex 2 D1(3,2) + D1(2,4) = 12+∞= ∞ D1(3,2) + D1(2,4) > D1(3,4) = 5 So No change D1(4,1) = ∞ Check via intermediate vertex 1 D1(4,2) + D1(1,2) = 2 + 8= 10 D1(4,2) + D1(1,2) < D1(4,1) So No Change
  • 15.
    D1(4,3) = 9 Checkvia intermediate vertex 2 D1(4,2) + D1(2,3) = 2 + 1= 3 D1(4,2) + D1(2,3) < D1(4,3) So Change D1(4,3) to 3 0 9 1 ∞ 3 0 4 0 5 𝐷2 = 1 2 3 4 1 2 3 4 8 ∞ 0 1 ∞ 12 2 0 ∞ 1 ∞ 9 0 4 0 5 𝐷1 = 1 2 3 4 1 2 3 4 8 ∞ 0 1 ∞ 12 2
  • 16.
    0 1 ∞ 0 𝐷2= 1 2 3 4 1 2 3 4 8 ∞ 0 ∞ 2 9 3 0 1 4 5 12 0
  • 17.
    Step-03: Using Floyd WarshallAlgorithm, write the following 4 matrices- D2(1,2) = 8 Check via intermediate vertex 3 D2(1,3) + D2(3,2) = 9 + 12= 21 D2(1,2) + D2(2,3) > D2(1,3) So No Change D2(1,4) = 1 Check via intermediate vertex 3 D2(1,3) + D2(3,4) = 9 + 5 = 14 D2(1,3) + D2(3,4)) > D2(1,4) So No Change D2(2,1) = ∞ Check via intermediate vertex 3 D2(2,3) + D2(3,1) = 1 + 4= 5 D2(2,3) + D2(3,1) < D2(2,1) So Change D2(2,1) to 5 D2(2,4) = ∞ Check via intermediate vertex 3 D2(2,3) + D2(3,4) = 1 + 5 = 6 D2(2,3) + D2(3,4) < D2(2,4) So Change D2(2,4) to 6 D2(4,1) = ∞ Check via intermediate vertex 3 D2(4,3) + D2(3,1) = 3 + 4= 7 D2(4,3) + D2(3,1) < D2(4,1) So Change D2(4,1) to 7
  • 18.
    D2(4,2) = 2 Checkvia intermediate vertex 3 D2(4,3) + D2(3,2) = 3 + 12 = 15 D2(4,3) + D2(3,2) > D2(4,2) So No Change 0 9 1 ∞ 3 0 4 0 5 𝐷2 = 1 2 3 4 1 2 3 4 8 ∞ 0 1 ∞ 12 2 0 9 1 7 3 0 4 0 5 𝐷3 = 1 2 3 4 1 2 3 4 8 5 0 1 6 12 2
  • 19.
    4 0 𝐷3 = 12 3 4 1 2 3 4 5 0 1 12 1 0 5 6 0 9 8 1 7 3 2 0
  • 20.
    Step-03: Using Floyd WarshallAlgorithm, write the following 4 matrices- D3(1,2) = 8 Check via intermediate vertex 4 D3(1,4) + D3(4,2) = 1 + 2= 3 D3(1,4) + D3(4,2) < D3(1,2) So Change D3(1,2) to 3 D3(1,3) = 9 Check via intermediate vertex 4 D3(1,4) + D3(4,3) = 1 + 3= 4 D3(1,4) + D3(4,3) < D3(1,3) So Change D3(1,3) to 4 D3(2,1) = 5 Check via intermediate vertex 4 D3(2,4) + D3(4,1) = 6 + 7= 13 D3(2,4) + D3(4,1) > D3(2,1) So No Change D3(2,3) = 1 Check via intermediate vertex 4 D3(2,4) + D3(4,3) = 6 + 3= 9 D3(2,4) + D3(4,3) < D3(2,3) So No Change D3(3,1) = 4 Check via intermediate vertex 4 D3(3,4) + D3(4,1) = 5 + 7= 12 D3(3,4) + D3(4,1) > D3(3,1) So No Change
  • 21.
    D3(3,2) = 12 Checkvia intermediate vertex 4 D3(3,4) + D3(4,2) = 5 + 2= 7 D3(3,4) + D3(4,2) < D3(3,2) So Change D3(3,2) to 7 D3(3,1) = 4 Check via intermediate vertex 4 D3(3,4) + D3(4,1) = 5 + 7= 12 D3(3,4) + D3(4,1) > D3(3,1) So No Change 0 9 1 7 3 0 4 0 5 𝐷3 = 1 2 3 4 1 2 3 4 8 5 0 1 6 12 2 0 4 1 7 3 0 4 0 5 𝐷4 = 1 2 3 4 1 2 3 4 3 5 0 1 6 7 2 The last matrix D4 represents the shortest path distance between every pair of vertices.
  • 22.
    Floyd Warshall Algorithm- Example Consider Vertex 3: Nothing changes. Consider Vertex 2: D(1,3) = D(1,2) + D(2,3) Consider Vertex 1: D(3,2) = D(3,1) + D(1,2) Original weights.
  • 23.
    Floyd Warshall Algorithm Looking at this example, we can come up with the following algorithm: ◦ Let D store the matrix with the initial graph edge information initially, and update D with the calculated shortest paths. For k=1 to n { For i=1 to n { For j=1 to n D[i,j] = min(D[i,j],D[i,k]+D[k,j]) } } ◦ The final D matrix will store all the shortest paths.