Given a weighted graph, return the shortest distances between every pair of nodes within the graph, where the graph can contain negative weights. It solves the all-pair shortest path problem.

Restrictions

Abstract

Operates by repeatedly lowering the distance between two nodes by checking if the shortest path between and involving an intermediate node is shorter. A good explanation is contained in this YouTube video. The -th instance of the outermost loop represents the algorithm has found shortest distances out of all possible paths involving nodes or less.

Efficiency

Time complexity:

Implementation

Transitive closure

Proof

Assuming input data complies with conditions for Floyd Warshall algorithm. Below is a proof by induction.

Assign integers starting from 1 onwards to represent each node of the graph. Nodes will subsequently referred to by , where x is the aforementioned integer.

Let distFW represent the by matrix outputted by the Floyd Warshall algorithm Such that distFW[i,j] represent the shortest distance from node to node found by the Floyd Warshall algorithm. Let distREAL be similarly defined, except representing the actual shortest distance between any two nodes.

distFW is to be initialized as the weight matrix of the graph to be solved.

Let be the statement that: after running the outer loop of the algorithm times, the algorithm will have found all shortest paths where the shortest path only consists of nodes within .

Prove

Algorithm runs 1 time, values of (the iterable of the outer for loop of Floyd Warshall) include only 1.

When :

If going from to via node (is ) is less than the current distance between and , the statement dist[i,1]+dist[1,j] < dist[i,j] will be true, and dist[i,j] will be updated to the new value of dist[i,1]+dist[1,j].

If going via is slower, the statement dist[i,1]+dist[1,j] < dist[i,j] will not be true, and thus dist[i,j] will not be updated.

Thus for all paths involving no or only intermediary nodes, the Floyd Warshall algorithm will always provide the correct distance of the correct shortest path.

Assume

It is assumed that, for all paths that only involves no intermidary nodes, or only intermediary nodes


Assuming all the correct pre-conditions exist for the Floyd Warshall All Pairs Shortest Path algorithm the following is a proof by induction. 

All the nodes in the graph are given consecutive and unique integer identifiers and a matrix of |V| by |V| is used to maintain the solution. 

After the initialisation when k=0 we have the dist[u,v] initialised as a simple adjacency matrix showing the direct edge weights between any pairs of nodes u and v. All other node pairs without direct edges are initialised with infinity. 

Base Case: When k=1 if going via node k=1 is less than the current distance between nodes numbered i and j, which is i1j will be reduce the distance by since the condition dist[i,1]+dist[1,j] < dist[i,j] will be true since nodes i and j do not have a direct edge connecting them, but are connected via node k=1. 

Inductive Step: Suppose for (k-1) passes all distances via nodes 1..(k-1) have been updated correctly for the outer loop of the triple nested loop in the FW algorithm and that dist[i,j] contains the shortest distance from node I to node j where the intermediate vertices lie within {1……(k-1)} of the graph. 

On the kth iteration of the outer triple nested loop of the FW algorithm there are two possibilities. 

  • Case 1: dist[i,j] will remain unchanged as going via node k if a path exists is not cheaper, so the cheaper distance is retained 

  • Case 2: dist[i,j] will reduce in distance as going via node k is cheaper ik, kj than what was already explored with i(k-1),(k-1)

As k is incremented from 1 to |V| we have then relaxed all the edges from i to j via node k where a path exists. We explored all the ik, kj paths in the graph, so FW gives the correct answer for the whole graph G.