Improvement of Floyd-Warshall Algorithm
Understand how the Floyd-Warshall algorithm improves on Warshall’s method to compute all-pairs shortest paths efficiently. Learn the recursive dynamic programming approach and the algorithm's Java implementation. This lesson helps you grasp shortest path computations with negative cycles and compare variations for optimized performance.
Efficient shortest paths with restricted intermediate vertices
Our fast dynamic programming algorithm is still a factor of slower in the worst case than the standard implementation of Johnson’s algorithm. A different formulation of shortest paths that removes this logarithmic factor was proposed twice in 1962, first by Robert Floyd and later independently by Peter Ingerman, both slightly generalizing an algorithm of Stephen Warshall published earlier in the same year. In fact, Warshall’s algorithm was previously discovered by Bernard Roy in 1959, and the underlying recursion pattern was used by Stephen Kleene in 1951.
Warshall’s (and Roy’s and Kleene’s) insight was to use a different third parameter in the dynamic programming recurrence. Instead of considering paths with a limited number of edges, they considered paths that can pass through only certain vertices. Here, “pass-through” means “both enter and leave;” for example, the path starts at , passes through and , and ends at .
Number the vertices arbitrarily from to . For every pair of vertices and and every integer , we define a path as follows:
is the shortest path (if any) from to that passes through only vertices numbered at most .
In particular, is the true shortest path from to . Kleene and Roy and Warshall all observed that these paths have a simple recursive structure.
Properties of paths
- The path can’t pass through any intermediate vertices, so it must be the edge (if any) from to .
- For any integer , either passes through vertex or it doesn’t.
- If passes through vertex