Floyd-Warshall's algorithm

The Floyd-Warshall Algorithm is an efficient way to find all-pairs shortest paths on a graph. That is, it is guaranteed to find the shortest path between every pair of vertices in a graph with no negative cycles. It still works, however, for graphs with negative edges.

This algorithm can also be used to detect the presence of negative cycles, if at the end of the algorithm, the distance from a vertex $$i$$ to itself is negative.

Algorithm
The Floyd-Warshall Algorithm is an application of Dynamic Programming. It maintains the following invariant:

"At the end of the $$r$$th iteration of the outer loop (i.e., when the iteration corresponding to $$k=r$$ is completed), for every pair of vertices $$u$$ and $$v$$, $$dist(u,v)$$ is the length of the shortest path from $$u$$ to $$v$$ such that all the intermediate vertices (i.e., all vertices in the path except $$u$$ and $$v$$) are less than or equal to $$r$$."

With this in mind, we can prove (and see) why the algorithm works, by induction:

When $$r=0$$ this is true because of the initialization&mdash;there cannot be any intermediate vertices, so $$dist(i,j)$$ is the length of the edge from $$i$$ to $$j$$ if it exists, and $$\infty$$ otherwise.

Suppose it is true when $$r=k-1$$. Then it is also true when $$r=k$$, because for any $$i$$ and $$j$$, the length of the shortest path from $$i$$ to $$j$$ with all intermediate vertices $$\le k$$ either does not involve the vertex $$k$$ at all (in which case it remains unchanged), or it does, in which case the shortest path from $$i$$ to $$j$$ via $$k$$ is clearly $$dist(i,k)+dist(k,j)$$.

Thus, it is true for all $$r$$. In particular, when $$r=N$$ (after the algorithm terminates), $$dist(i,j)$$ is the length of the shortest path from $$i$$ to $$j$$ using vertices $$\le N$$&mdash;all vertices&mdash;so it is the shortest path from $$i$$ to $$j$$.

The recurrence, where $$dist(k,i,j)$$ is the shortest distance between vertex $$i$$ and $$j$$, after using the vertices $$v_1, v_2, \ldots, v_k$$ as intermediate vertices in some way:
 * $$dist( 0, i, j ) =$$ initial graph conditions
 * $$dist( k, i, j ) = \min(dist( k - 1, i, k ) + dist( k - 1, k, j ), dist( k - 1, i, j ) )$$

Pseudocode
The pseudocode below assumes an input graph of N vertices.

for i = 1 to N   for j = 1 to N        if there is no edge from i to j            dist[1][i][j] = $$\infty$$ else dist[1][i][j] = the length of the edge from i to j

for k = 2 to N + 1 for i = 1 to N       for j = 1 to N            dist[k][i][j] = min(dist[k-1][i][j], dist[k-1][i][k] + dist[k-1][k][j])

This will give the shortest distances between any two nodes, from which shortest paths may be constructed.

The algorithm takes $$\theta(N^3)$$ time and $$O(N^2)$$ space, and has the distinct advantage of hiding a small constant in its behavior, since very little work is done in the innermost loop.

Implementation
C