The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.

Example:

Input:
       graph[][] = { {0,   5,  INF, 10},
                    {INF,  0,  3,  INF},
                    {INF, INF, 0,   1},
                    {INF, INF, INF, 0} }
which represents the following graph
             10
       (0)------->(3)
        |         /|\
      5 |          |
        |          | 1
       \|/         |
       (1)------->(2)
            3       
Note that the value of graph[i][j] is 0 if i is equal to j 
And graph[i][j] is INF (infinite) if there is no edge from vertex i to j.

Output:
Shortest distance matrix
      0      5      8      9
    INF      0      3      4
    INF    INF      0      1
    INF    INF    INF      0
[ad type=”banner”]

Floyd Warshall Algorithm
We initialize the solution matrix same as the input graph matrix as a first step. Then we update the solution matrix by considering all vertices as an intermediate vertex. The idea is to one by one pick all vertices and update 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 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.
  • 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].

The following figure is taken from the Cormen book. It shows the above optimal substructure property in the all-pairs shortest path problem.Floyd-Warshell Alogrithm

Following is C++ implementation of the Floyd Warshall algorithm.

[pastacode lang=”cpp” manual=”%2F%2F%20C%20Program%20for%20Floyd%20Warshall%20Algorithm%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20Number%20of%20vertices%20in%20the%20graph%0A%23define%20V%204%0A%20%0A%2F*%20Define%20Infinite%20as%20a%20large%20enough%20value.%20This%20value%20will%20be%20used%0A%20%20for%20vertices%20not%20connected%20to%20each%20other%20*%2F%0A%23define%20INF%2099999%0A%20%0A%2F%2F%20A%20function%20to%20print%20the%20solution%20matrix%0Avoid%20printSolution(int%20dist%5B%5D%5BV%5D)%3B%0A%20%0A%2F%2F%20Solves%20the%20all-pairs%20shortest%20path%20problem%20using%20Floyd%20Warshall%20algorithm%0Avoid%20floydWarshall%20(int%20graph%5B%5D%5BV%5D)%0A%7B%0A%20%20%20%20%2F*%20dist%5B%5D%5B%5D%20will%20be%20the%20output%20matrix%20that%20will%20finally%20have%20the%20shortest%20%0A%20%20%20%20%20%20distances%20between%20every%20pair%20of%20vertices%20*%2F%0A%20%20%20%20int%20dist%5BV%5D%5BV%5D%2C%20i%2C%20j%2C%20k%3B%0A%20%0A%20%20%20%20%2F*%20Initialize%20the%20solution%20matrix%20same%20as%20input%20graph%20matrix.%20Or%20%0A%20%20%20%20%20%20%20we%20can%20say%20the%20initial%20values%20of%20shortest%20distances%20are%20based%0A%20%20%20%20%20%20%20on%20shortest%20paths%20considering%20no%20intermediate%20vertex.%20*%2F%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20V%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bi%5D%5Bj%5D%20%3D%20graph%5Bi%5D%5Bj%5D%3B%0A%20%0A%20%20%20%20%2F*%20Add%20all%20vertices%20one%20by%20one%20to%20the%20set%20of%20intermediate%20vertices.%0A%20%20%20%20%20%20—%3E%20Before%20start%20of%20a%20iteration%2C%20we%20have%20shortest%20distances%20between%20all%0A%20%20%20%20%20%20pairs%20of%20vertices%20such%20that%20the%20shortest%20distances%20consider%20only%20the%0A%20%20%20%20%20%20vertices%20in%20set%20%7B0%2C%201%2C%202%2C%20..%20k-1%7D%20as%20intermediate%20vertices.%0A%20%20%20%20%20%20—-%3E%20After%20the%20end%20of%20a%20iteration%2C%20vertex%20no.%20k%20is%20added%20to%20the%20set%20of%0A%20%20%20%20%20%20intermediate%20vertices%20and%20the%20set%20becomes%20%7B0%2C%201%2C%202%2C%20..%20k%7D%20*%2F%0A%20%20%20%20for%20(k%20%3D%200%3B%20k%20%3C%20V%3B%20k%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Pick%20all%20vertices%20as%20source%20one%20by%20one%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Pick%20all%20vertices%20as%20destination%20for%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20above%20picked%20source%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(j%20%3D%200%3B%20j%20%3C%20V%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20vertex%20k%20is%20on%20the%20shortest%20path%20from%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20i%20to%20j%2C%20then%20update%20the%20value%20of%20dist%5Bi%5D%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist%5Bi%5D%5Bk%5D%20%2B%20dist%5Bk%5D%5Bj%5D%20%3C%20dist%5Bi%5D%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bi%5D%5Bj%5D%20%3D%20dist%5Bi%5D%5Bk%5D%20%2B%20dist%5Bk%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20shortest%20distance%20matrix%0A%20%20%20%20printSolution(dist)%3B%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20to%20print%20solution%20*%2F%0Avoid%20printSolution(int%20dist%5B%5D%5BV%5D)%0A%7B%0A%20%20%20%20printf%20(%22Following%20matrix%20shows%20the%20shortest%20distances%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20between%20every%20pair%20of%20vertices%20%5Cn%22)%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%200%3B%20j%20%3C%20V%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist%5Bi%5D%5Bj%5D%20%3D%3D%20INF)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%257s%22%2C%20%22INF%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf%20(%22%257d%22%2C%20dist%5Bi%5D%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Let%20us%20create%20the%20following%20weighted%20graph%0A%20%20%20%20%20%20%20%20%20%20%20%2010%0A%20%20%20%20%20%20%20(0)——-%3E(3)%0A%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%2F%7C%5C%0A%20%20%20%20%20%205%20%7C%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%7C%201%0A%20%20%20%20%20%20%20%5C%7C%2F%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20(1)——-%3E(2)%0A%20%20%20%20%20%20%20%20%20%20%20%203%20%20%20%20%20%20%20%20%20%20%20*%2F%0A%20%20%20%20int%20graph%5BV%5D%5BV%5D%20%3D%20%7B%20%7B0%2C%20%20%205%2C%20%20INF%2C%2010%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7BINF%2C%200%2C%20%20%203%2C%20INF%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7BINF%2C%20INF%2C%200%2C%20%20%201%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7BINF%2C%20INF%2C%20INF%2C%200%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20solution%0A%20%20%20%20floydWarshall(graph)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

Output :

Following matrix shows the shortest distances between every pair of vertices
      0      5      8      9
    INF      0      3      4
    INF    INF      0      1
    INF    INF    INF      0

Time Complexity: O(V^3)

The above program only prints the shortest distances. We can modify the solution to print the shortest paths also by storing the predecessor information in a separate 2D matrix.
Also, the value of INF can be taken as INT_MAX from limits.h to make sure that we handle maximum possible value. When we take INF as INT_MAX, we need to change the if condition in the above program to avoid arithmatic overflow.

#include <limits.h>

#define INF INT_MAX
..........................
if ( dist[i][k] != INF && 
     dist[k][j] != INF && 
     dist[i][k] + dist[k][j] < dist[i][j]
    )
 dist[i][j] = dist[i][k] + dist[k][j];
...........................
[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,