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 the graph.

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[i][j] = $$\infty$$ else dist[i][j] = the length of the edge from i to j

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

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

The algorithm takes $$O(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.

Explanation
The Floyd-Warshall Algorithm is an example 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$$.