Improvement of Floyd-Warshall Algorithm
Explore the different techniques used to implement Floyd-Warshall algorithm variants efficiently.
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.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy