Data structure Graph | ||
![]() | ||
Class All-pairs shortest path problem (for weighted graphs) Worst-case performance Θ ( | V | 3 ) {displaystyle Theta (|V|^{3})} Best-case performance Θ ( | V | 3 ) {displaystyle Theta (|V|^{3})} Average performance Θ ( | V | 3 ) {displaystyle Theta (|V|^{3})} Worst-case space complexity Θ ( | V | 2 ) {displaystyle Theta (|V|^{2})} |
In computer science, the Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles). A single execution of the algorithm will find the lengths (summed weights) of the shortest paths between all pairs of vertices. Although it does not return details of the paths themselves, it is possible to reconstruct the paths with simple modifications to the algorithm. Versions of the algorithm can also be used for finding the transitive closure of a relation
Contents
History and naming
The Floyd–Warshall algorithm is an example of dynamic programming, and was published in its currently recognized form by Robert Floyd in 1962. However, it is essentially the same as algorithms previously published by Bernard Roy in 1959 and also by Stephen Warshall in 1962 for finding the transitive closure of a graph, and is closely related to Kleene's algorithm (published in 1956) for converting a deterministic finite automaton into a regular expression. The modern formulation of the algorithm as three nested for-loops was first described by Peter Ingerman, also in 1962.
The algorithm is also known as Floyd's algorithm, the Roy–Warshall algorithm, the Roy–Floyd algorithm, or the WFI algorithm.
Algorithm
The Floyd–Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able to do this with
Consider a graph
For each of these pairs of vertices, the true shortest path could be either
(1) a path that only uses vertices in the setor
(2) a path that goes fromWe know that the best path from
If
and the recursive case is
This formula is the heart of the Floyd–Warshall algorithm. The algorithm works by first computing
Example
The algorithm above is executed on the graph on the left below:
Prior to the first iteration of the outer loop, labeled
The distance matrix at each iteration of
Behavior with negative cycles
A negative cycle is a cycle whose edges sum to a negative value. There is no shortest path between any pair of vertices
Hence, to detect negative cycles using the Floyd–Warshall algorithm, one can inspect the diagonal of the path matrix, and the presence of a negative number indicates that the graph contains at least one negative cycle. To avoid numerical problems one should check for negative numbers on the diagonal of the path matrix within the inner for loop of the algorithm. Obviously, in an undirected graph a negative edge creates a negative cycle (i.e., a closed walk) involving its incident vertices. Considering all edges of the above example graph as undirected, e.g. the vertex sequence 4 – 2 – 4 is a cycle with weight sum −2.
Path reconstruction
The Floyd–Warshall algorithm typically only provides the lengths of the paths between all pairs of vertices. With simple modifications, it is possible to create a method to reconstruct the actual path between any two endpoint vertices. While one may be inclined to store the actual path from each vertex to each other vertex, this is not necessary, and in fact, is very costly in terms of memory. Instead, the shortest-path tree can be calculated for each node in
Analysis
Let
Applications and generalizations
The Floyd–Warshall algorithm can be used to solve the following problems, among others:
Implementations
Implementations are available for many programming languages.
Comparison with other shortest path algorithms
The Floyd–Warshall algorithm is a good choice for computing paths between all pairs of vertices in dense graphs, in which most or all pairs of vertices are connected by edges. For sparse graphs with non-negative edge weights, a better choice is to use Dijkstra's algorithm from each possible starting vertex, since the running time of repeated Dijkstra (
There are also known algorithms using fast matrix multiplication to speed up all-pairs shortest path computation in dense graphs, but these typically make extra assumptions on the edge weights (such as requiring them to be small integers). In addition, because of the high constant factors in their running time, they would only provide a speedup over the Floyd–Warshall algorithm for very large graphs.