Given a set of vertices V in a weighted graph where its edge weights w(u, v) can be negative, find the shortest path weights d(s, v) from every source s for all vertices v present in the graph. If the graph contains a negative-weight cycle, report it.

For example, consider the following graph:

Floyd Warshall Algorithm

The adjacency matrix containing the shortest distance is:
 
  0  -1  -2   0
  4   0   2   4
  5   1   0   2
  3  -1   1   0
 
The shortest path from:
 
• vertex 0 to vertex 1 is [0 —> 2 —> 3 —> 1]
• vertex 0 to vertex 2 is [0 —> 2]
• vertex 0 to vertex 3 is [0 —> 2 —> 3]
• vertex 1 to vertex 0 is [1 —> 0]
• vertex 1 to vertex 2 is [1 —> 0 —> 2]
• vertex 1 to vertex 3 is [1 —> 0 —> 2 —> 3]
• vertex 2 to vertex 0 is [2 —> 3 —> 1 —> 0]
• vertex 2 to vertex 1 is [2 —> 3 —> 1]
• vertex 2 to vertex 3 is [2 —> 3]
• vertex 3 to vertex 0 is [3 —> 1 —> 0]
• vertex 3 to vertex 1 is [3 —> 1]
• vertex 3 to vertex 2 is [3 —> 1 —> 0 —> 2]

We have already covered single-source the shortest paths in separate posts. We have seen that:

Here, V is the total number of vertices and E is the total number of edges in the graph.

 
This post will introduce All-Pairs Shortest Paths that return the shortest paths between every pair of vertices in the graph containing negative edge weights.

Practice this problem

If the graph contains only positive edge weights, a simple solution would be to run Dijkstra’s algorithm V times. The time complexity of this solution would be O(V × (E + V × log(V))), i.e., O(V × E + V2.log(V)).

If the graph contains negative edge weights, we can run Bellman–Ford once from each vertex to find all-pairs shortest paths. The time complexity of this approach will be O(V2 × E). If the graph is dense, i.e., E = V2, then the time complexity becomes O(V4).
 

Floyd–Warshall algorithm is an algorithm for finding the shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). It does so by comparing all possible paths through the graph between each pair of vertices and that too with O(V3) comparisons in a graph.

Following is the pseudocode for Floyd Warshall, as given on Wikipedia. The implementation takes a graph, represented by an adjacency matrix, and fills dist[] with shortest path (the least-cost) information:

let dist be V × V matrix of minimum distances initialized to INFINITY
for each vertex v
  dist[v][v] = 0
for each edge (u, v)
  dist[u][v] = weight(u, v)
 
for k from 0 to |V| – 1
  for i from 0 to |V| – 1
    for j from 0 to |V| – 1
      if (dist[i][k] + dist[k][j]) is less than dist[i][j] then
        dist[i][j] = dist[i][k] + dist[k][j]

Above pseudocode picks up a vertex k between 0 and V-1, one at a time, and include that vertex as an intermediate vertex in the shortest path between every pair of edges i—>j in the graph. It updates the cost matrix whenever a more straightforward path from i to j through vertex k is found. Since, for a given k, we have already considered vertices 0…k-1 as intermediate vertices, this approach works.

 
Let’s consider the above graph,

Before the first iteration of the outer loop for k, the only known paths correspond to the single edges in the graph.

 
Floyd Warshall

At k = 0, paths that go through the vertex 0 are found: in particular, the path [1, 0, 2] is found, replacing the path [1, 2] which has fewer edges but is costly.
 
Floyd Warshall Algorithm: Step 0

At k = 1, paths going through the vertices {0, 1} are found. The following figure shows how the path [3, 1, 0, 2] is assembled from the two known paths [3, 1] and [1, 0, 2] encountered in previous iterations, with 1 in the intersection. The path [3, 1, 2] is not considered because [1, 0, 2] is the shortest path encountered so far from 1 to 2.
 
Floyd Warshall Algorithm: Step 1

At k = 2, paths going through the vertices {0, 1, 2} are found.
 
Floyd Warshall Algorithm: Step 2

Finally, at k = 3, all shortest paths are found.
 
Floyd Warshall Algorithm: Step 3

The Floyd–Warshall algorithm is simple to code and really efficient traditionally. It can also be used to find the Transitive Closure of a graph and detect negative-weight cycles in the graph.

To detect negative cycles using the Floyd–Warshall algorithm, check the distance matrix’s diagonal for a negative number as it indicates that the graph contains at least one negative cycle.

How does this works?

The Floyd–Warshall algorithm iteratively revises path lengths between all pairs of vertices (i, j), including where i = j. Initially, the size of the path (i, i) is zero. A path [i, k…i] can only improve upon this if it has a length less than zero, i.e., denotes a negative cycle. Thus, after the algorithm, (i, i) will be negative if there exists a negative-length path from i back to i.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
The shortest path from 0 —> 1 is [0, 2, 3, 1]
The shortest path from 0 —> 2 is [0, 2]
The shortest path from 0 —> 3 is [0, 2, 3]
The shortest path from 1 —> 0 is [1, 0]
The shortest path from 1 —> 2 is [1, 0, 2]
The shortest path from 1 —> 3 is [1, 0, 2, 3]
The shortest path from 2 —> 0 is [2, 3, 1, 0]
The shortest path from 2 —> 1 is [2, 3, 1]
The shortest path from 2 —> 3 is [2, 3]
The shortest path from 3 —> 0 is [3, 1, 0]
The shortest path from 3 —> 1 is [3, 1]
The shortest path from 3 —> 2 is [3, 1, 0, 2]

The time complexity of the Floyd–Warshall algorithm is O(V3), where V is the total number of vertices in the graph.

 
Johnson’s algorithm can also be used to find the shortest paths between all pairs of vertices in a sparse, weighted, directed graph. It allows some edge weights to be negative numbers, but no negative-weight cycles may exist. It uses the Bellman–Ford algorithm to transform the input graph such that it removes all negative weights from the graph and run Dijkstra’s algorithm on the transformed graph.

 
References:

https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
MIT: Shortest Paths III: All-pairs Shortest Paths, Matrix Multiplication, Floyd–Warshall, Johnson