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).[1][2] A single execution of the algorithm will find the lengths (summed weights) of 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 R{\displaystyle R}, or (in connection with the Schulze voting system) widest paths between all pairs of vertices in a weighted graph.

The Floyd–Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able to do this with Θ(|V|3){\displaystyle \Theta (|V|^{3})} comparisons in a graph. This is remarkable considering that there may be up to Ω(|V|2){\displaystyle \Omega (|V|^{2})} edges in the graph, and every combination of edges is tested. It does so by incrementally improving an estimate on the shortest path between two vertices, until the estimate is optimal.

Consider a graph G{\displaystyle G} with vertices V{\displaystyle V} numbered 1 through N{\displaystyle N}. Further consider a function shortestPath(i,j,k){\displaystyle \mathrm {shortestPath} (i,j,k)} that returns the shortest possible path from i{\displaystyle i} to j{\displaystyle j} using vertices only from the set {1,2,…,k}{\displaystyle \{1,2,\ldots ,k\}}as intermediate points along the way. Now, given this function, our goal is to find the shortest path from each i{\displaystyle i} to each j{\displaystyle j} using only vertices in {1,2,…,N}{\displaystyle \{1,2,\ldots ,N\}}.

For each of these pairs of vertices, the shortestPath(i,j,k){\displaystyle \mathrm {shortestPath} (i,j,k)} could be either

(1) a path that doesn't go through k{\displaystyle k} (only uses vertices in the set {1,…,k−1}{\displaystyle \{1,\ldots ,k-1\}}.)

or

(2) a path that does go through k{\displaystyle k} (from i{\displaystyle i} to k{\displaystyle k} and then from k{\displaystyle k} to j{\displaystyle j}, both only using intermediate vertices in {1,…,k−1}{\displaystyle \{1,\ldots ,k-1\}})

We know that the best path from i{\displaystyle i} to j{\displaystyle j} that only uses vertices 1{\displaystyle 1} through k−1{\displaystyle k-1} is defined by shortestPath(i,j,k−1){\displaystyle \mathrm {shortestPath} (i,j,k-1)}, and it is clear that if there were a better path from i{\displaystyle i} to k{\displaystyle k} to j{\displaystyle j}, then the length of this path would be the concatenation of the shortest path from i{\displaystyle i} to k{\displaystyle k} (only using intermediate vertices in {1,…,k−1}{\displaystyle \{1,\ldots ,k-1\}}) and the shortest path from k{\displaystyle k} to j{\displaystyle j} (only using intermediate vertices in {1,…,k−1}{\displaystyle \{1,\ldots ,k-1\}}).

If w(i,j){\displaystyle w(i,j)} is the weight of the edge between vertices i{\displaystyle i} and j{\displaystyle j}, we can define shortestPath(i,j,k){\displaystyle \mathrm {shortestPath} (i,j,k)} in terms of the following recursive formula: the base case is

This formula is the heart of the Floyd–Warshall algorithm. The algorithm works by first computing shortestPath(i,j,k){\displaystyle \mathrm {shortestPath} (i,j,k)} for all (i,j){\displaystyle (i,j)} pairs for
k=1{\displaystyle k=1}, then k=2{\displaystyle k=2}, etc. This process continues until k=N{\displaystyle k=N}, and we have found the shortest path for all (i,j){\displaystyle (i,j)} pairs using any intermediate vertices. Pseudocode for this basic version follows:

Prior to the first iteration of the outer loop, labeled k = 0 above, the only known paths correspond to the single edges in the graph. At k = 1, paths that go through the vertex 1 are found: in particular, the path [2,1,3] is found, replacing the path [2,3] which has fewer edges but is longer (in terms of weight). At k = 2, paths going through the vertices {1,2} are found. The red and blue boxes show how the path [4,2,1,3] is assembled from the two known paths [4,2] and [2,1,3] encountered in previous iterations, with 2 in the intersection. The path [4,2,3] is not considered, because [2,1,3] is the shortest path encountered so far from 2 to 3. At k = 3, paths going through the vertices {1,2,3} are found. Finally, at k = 4, all shortest paths are found.

The distance matrix at each iteration of k, with the updated distances in bold, will be:

A negative cycle is a cycle whose edges sum to a negative value. There is no shortest path between any pair of vertices i{\displaystyle i}, j{\displaystyle j} which form part of a negative cycle, because path-lengths from i{\displaystyle i} to j{\displaystyle j} can be arbitrarily small (negative). For numerically meaningful output, the Floyd–Warshall algorithm assumes that there are no negative cycles. Nevertheless, if there are negative cycles, the Floyd–Warshall algorithm can be used to detect them. The intuition is as follows:

The Floyd–Warshall algorithm iteratively revises path lengths between all pairs of vertices (i,j){\displaystyle (i,j)}, including where i=j{\displaystyle i=j};

Initially, the length of the path (i,i){\displaystyle (i,i)} is zero;

A path [i,k,…,i]{\displaystyle [i,k,\ldots ,i]} can only improve upon this if it has length less than zero, i.e. denotes a negative cycle;

Thus, after the algorithm, (i,i){\displaystyle (i,i)} will be negative if there exists a negative-length path from i{\displaystyle i} back to i{\displaystyle i}.

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.[9] 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.[10] 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.

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 Θ(|E|){\displaystyle \Theta (|E|)} time using Θ(|V|){\displaystyle \Theta (|V|)} memory to store each tree which allows us to efficiently reconstruct a path from any two connected vertices.

The Floyd–Warshall algorithm can be used to solve the following problems, among others:

Shortest paths in directed graphs (Floyd's algorithm).

Transitive closure of directed graphs (Warshall's algorithm). In Warshall's original formulation of the algorithm, the graph is unweighted and represented by a Boolean adjacency matrix. Then the addition operation is replaced by logical conjunction (AND) and the minimum operation by logical disjunction (OR).

Optimal routing. In this application one is interested in finding the path with the maximum flow between two vertices. This means that, rather than taking minima as in the pseudocode above, one instead takes maxima. The edge weights represent fixed constraints on flow. Path weights represent bottlenecks; so the addition operation above is replaced by the minimum operation.

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 (O(|E||V|+|V|2log⁡|V|){\displaystyle O(|E||V|+|V|^{2}\log |V|)} using Fibonacci heaps) is better than the O(|V|3){\displaystyle O(|V|^{3})} running time of the Floyd–Warshall algorithm when |E|{\displaystyle |E|} is significantly smaller than |V|2{\displaystyle |V|^{2}}. For sparse graphs with negative edges but no negative cycles, Johnson's algorithm can be used, with the same asymptotic running time as the repeated Dijkstra approach.

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).[13][14] 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.