All Pairs Shortest
Paths Problem
Floyd Warshall
Algorithm
Introduction
•The Floyd-Warshall algorithm
is a dynamic programming
algorithm used to find the
shortest path between all pairs
of vertices in a weighted graph
(directed or undirected).
•The core idea of the Floyd-
Warshall algorithm is to
iteratively improve the shortest
path between any two vertices by
considering an intermediate
vertex.
•This concept is based on the
principle of optimality in
dynamic programming, where the
solution to a problem can be built
from optimal solutions of sub-
Shortest Path Means?
B 5 D
Path 1: A->B->D =7
Path 2: A->C->D =7 3
Path 3: A->B->C->D =6
2 1
Path 3 is the Shortest path A C
4
(B & C are intermediate
nodes here)
Motive
• Dijkstra's algorithm solves single-source shortest path
problems but is inefficient for all-pairs shortest paths.
Floyd-Warshall efficiently finds shortest paths
between all pairs of vertices in one go.
• The Floyd-Warshall algorithm was designed to avoid
repetitive calculations by using a dynamic
programming approach.
Used in:
• Network routing for fast data transmission.
• Game development for pathfinding in games.
• used in Google Maps, GPS systems.
Algorithm
Step 1: Construct an adjacency matrix A, where A[i, j]
represents the edge cost.
Set A[i, j] = ∞ if no direct path exists
If i = j (same vertex), set A[i, i] = 0.
Step 2: For each vertex k as an intermediate, update A[i,
j] for all pairs (i, j) using the formula:
A[i,j]=min(A[i,j],A[i,k]+A[k,j])
Keep the first row and column unchanged.
Step 3: Repeat Step 2 for all vertices until all possible
intermediate paths are checked.
Step 4: The final matrix contains the shortest paths
between all pairs of vertices.
Algorithm - Pseudo Code
let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)
for each edge (u, v) do
dist[u][v] = w(u, v) // The weight of the edge (u, v)
for each vertex v do
dist[v][v] = 0
for k from 1 to |V|
for i from 1 to |V|
for j from 1 to |V|
if dist[i][j] > dist[i][k] + dist[k][j]
dist[i][j] = dist[i][k] + dist[k][j]
end if
Example
No Edge Between Vertices: set A[i,j]=∞ (infinity)
Self-Loop :set A[i,i]=0
Edge Between Vertices: set D[i,j] to the weight of that edge w[i,j].
Considering Vertex 1 as intermediate vertex
Keep the 1st row ,1st column and diagonal elements unchanged Why?
If D[i,k]+D[k,j]<D[i,j],
then update: D[i,j]=D[i,k]+D[k,j]
A1[2, 3]
A0[2, 1]+A0[1, 3]<A0[2, 3]
0 +∞ < ∞ (Condition not Satisfied ❌)
A1[2,4]
A0[2, 1]+A0[1, 4]<A0[2, 4]
0 + 5 < 4 (Condition not
Satisfied ❌)
A1[3,2]
A0[3, 1]+A0[1, 2]<A0[3, 2]
∞ + 3 < 1 (Condition not
Satisfied ❌)
A [3,4]
1
A0[3, 1]+A0[1, 4]<A0[3, 4]
∞ + 5 < ∞ (Condition not
Satisfied ❌)
A1[4,2]
A0[4, 1]+A0[1, 2]<A0[4, 2]
∞ + 3 < ∞ (Condition not
Satisfied ❌)
A1[4,3]
A0[4, 1]+A0[1, 3]<A0[4, 3]
∞ +∞ < 2(Condition not
Satisfied ❌)
A1[1, 3]
A0[1, 2]+A0[2, 3]<A0[1, 3]
3 +∞ < ∞ (Condition not 2 as intermediate
Satisfied ❌) vertex
A1[1,4]
A0[1, 2]+A0[2, 4]<A0[1, 4]
3 + 4 < 5 (Condition not
Satisfied ❌)
A1[3,1]
A0[3, 2]+A0[2, 1]<A0[3, 1]
1 + 2 < ∞ (Condition Satisfied
✅)
{A0[3, 1]=1+2}
A1[3,4]
A0[3, 2]+A0[2, 4]<A0[3, 4]
1 + 4 < ∞ (Condition Satisfied ✅)
{A1[3,4]=1+4}
A1[4,1]
A0[4, 2]+A0[2, 1]<A0[4, 1]
∞ + 2 < ∞ (Condition not
Satisfied
A 1
[4,3] ❌)
A0[4, 2]+A0[2, 3]<A0[4, 3]
∞ +∞ < 2(Condition not
Satisfied ❌)
3 as intermediate vertex
4 as intermediate vertex
Final Adjacency Matrix
Complexity:
Time complexity of the Floyd-Warshall
algorithm is O(n³)
The algorithm has three nested loops:
[Link] loop for selecting an intermediate vertex (k) →
runs n times.
[Link] loop for selecting a source vertex (i) → runs n
times.
[Link] loop for selecting a destination vertex (j) →
runs n times.
The algorithm uses a distance matrix of size n ×
n, so the space complexity is O(n²).
Conclusion
The Floyd-Warshall algorithm is a method used to
find the shortest paths between all pairs of
nodes in a graph. It works by checking if going
through an intermediate node gives a shorter path
and updates the distances step by step.
It works for both directed and undirected graphs with positive or
negative edge weights (but no negative cycles).
Unlike Dijkstra’s algorithm, it finds shortest paths between all
node pairs in a single execution.
Overall, the Floyd-Warshall algorithm is widely used in network
routing, urban planning, and shortest path computations in
various fields.