The Floyd-Warshall algorithm computes all-pairs shortest graph path lengths in a finite weighted directed graph with no negative-length cycles in a graph. Initialize , use the arc cost for each existing directed edge, and use when no arc exists. For , update every ordered pair by the displayed rule. The storage is lengths and the running time is .
For correctness, let be the shortest simple graph path length whose intermediate vertices lie among the first . A shortest allowed graph path either avoids or splits at its unique occurrence of , giving the recurrence. Conversely, concatenate shortest paths through and remove any repeated-vertex sections; each such closed walk has nonnegative length, so removal cannot increase the length. This proves equality. The update can be performed in place because and the th row and column remain unchanged during stage .
If negative-length cycles in a graph are present, a shortest walk may not exist and the recurrence need not compute shortest simple graph paths. Two optimal subpaths can concatenate into a repeated-vertex walk with a negative cycle; deleting that cycle increases its length. A negative diagonal entry in the algorithm's final array detects this obstruction to finite shortest-walk distances.
Articles by others on the same topic
The Floyd-Warshall algorithm is a classic algorithm used in computer science and graph theory to find the shortest paths between all pairs of vertices in a weighted directed or undirected graph. It is particularly useful for graphs with positive or negative edge weights, although it cannot handle graphs with negative cycles. ### Key Features: 1. **Dynamic Programming Approach**: The algorithm uses a dynamic programming technique to iteratively improve the shortest path estimates between every pair of vertices.