You are on page 1of 23

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 1.5


ABC PQR
ABC 0 1.7 3.5 1.7

3.5
PQR 1.5 0 ∞ 4 2.5

XYZ 4 2.5 0 XYZ


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
1
6 3
0 0 6 5 ∞ 0 4 3
D=
1 ∞ 0 4 3 5
2
2

2 ∞ ∞ 0 2
3 ∞ ∞ ∞ 0
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.

i j
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 = ∞.
Floyd Warshall Algorithm
Initial distance matrix for the given graph
is,
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
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

1 2 3 4
1 0 8 ∞ 1
  = 2 ∞ 0 1 ∞
3 4 12 0 5
4 ∞ 2 9 0
1 2 3 4
1 0 8 ∞ 1

  = 2 ∞ 0 1 ∞

3 4 12 0 5

4 ∞ 2 9 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) = ∞
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

1 2 3 4 1 2 3 4
1 0 8 ∞ 1 1 0 8 9 1
  = 2 ∞ 0 1 ∞   = 2 ∞ 0 1 ∞
3 4 12 0 5 3 4 12 0 5
4 ∞ 2 9 0 4 ∞ 2 3 0
1 2 3 4
1 0 8 1
9

  = 2 ∞ 0 1 ∞

3 4 12 00 5

4 ∞ 2 3 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) = ∞
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

1 2 3 4
1 2 3 4
1 0 8 9 1 1 0 8 9 1
  = 2 ∞ 0 1 ∞
  = 2 5 0 1 6
3 4 12 0 5
3 4 12 0 5
4 ∞ 2 3 0
4 7 2 3 0
1 2 3 4
1
0 8 9 1

  = 2 5 0 1 6

3 4 12 0 5

4 7 2 3 00
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

1 2 3 4 1 2 3 4
1 1 0 3 4 1
0 8 9 1
  = 2 5 0 1 6   = 2 5 0 1 6
3 4 12 0 5 3 4 7 0 5

4 7 2 3 0 4 7 2 3 0

The last matrix D4 represents the


shortest path distance between every
pair of vertices.
Floyd Warshall Algorithm - Example

Original weights.

Consider Vertex 1:
D(3,2) = D(3,1) + D(1,2)

Consider Vertex 2:
D(1,3) = D(1,2) + D(2,3)

Consider Vertex 3:
Nothing changes.
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.

You might also like