Floyd Warshall Algorithm
Floyd Warshall Algorithm
Initialize the solution matrix same as the input graph matrix as a first step.
Then update the solution matrix by considering all vertices as an intermediate
vertex.
The idea is to pick all vertices one by one and updates all shortest paths which
include the picked vertex as an intermediate vertex in the shortest path.
When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. K-1} as intermediate vertices.
For every pair (i, j) of the source and destination vertices respectively, there are
two possible cases.
K is not an intermediate vertex in shortest path from i to j. We keep the value of
dist[i][j] as it is.
1. K is an intermediate vertex in shortest path from i to j. We update the value of
dist[i][j] as dist[i][k] + dist[k][j], if dist[i][j] > dist[i][k] + dist[k][j]
Pseudo-Code of Floyd Warshall Algorithm :
For k = 0 to n – 1
For i = 0 to n – 1
For j = 0 to n – 1
Distance[i, j] = min(Distance[i, j], Distance[i, k] + Distance[k, j])