Floyd–Warshall algorithm

[1][2] A single execution of the algorithm will find the lengths (summed weights) of shortest paths between all pairs of vertices.

Versions of the algorithm can also be used for finding the transitive closure of a relation

, or (in connection with the Schulze voting system) widest paths between all pairs of vertices in a weighted graph.

The Floyd–Warshall algorithm is an example of dynamic programming, and was published in its currently recognized form by Robert Floyd in 1962.

[3] However, it is essentially the same as algorithms previously published by Bernard Roy in 1959[4] and also by Stephen Warshall in 1962[5] for finding the transitive closure of a graph,[6] and is closely related to Kleene's algorithm (published in 1956) for converting a deterministic finite automaton into a regular expression, with the difference being the use of a min-plus semiring.

[7] The modern formulation of the algorithm as three nested for-loops was first described by Peter Ingerman, also in 1962.

[8] The Floyd–Warshall algorithm compares many possible paths through the graph between each pair of vertices.

that returns the length of the shortest possible path (if one exists) from

Now, given this function, our goal is to find the length of the shortest path from each

In other words, we have arrived at the recursive formula: The base case is given by where

Prior to the first recursion of the outer loop, labeled k = 0 above, the only known paths correspond to the single edges in the graph.

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 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.

which form part of a negative cycle, because path-lengths from

For numerically meaningful output, the Floyd–Warshall algorithm assumes that there are no negative cycles.

is the largest absolute value of a negative edge in the graph.

To avoid overflow/underflow 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.

memory, and allows us to efficiently reconstruct a directed path between any two connected vertices.

[9][12] The Floyd–Warshall algorithm can be used to solve the following problems, among others: Implementations are available for many programming languages.

For graphs with non-negative edge weights, Dijkstra's algorithm can be used to find all shortest paths from a single vertex with running time

Thus, running Dijkstra starting at each vertex takes time

, this yields a worst-case running time of repeated Dijkstra of

While this matches the asymptotic worst-case running time of the Floyd-Warshall algorithm, the constants involved matter quite a lot.

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).

[16][17] 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.

Graph of a strictly concave quadratic function with unique maximum.
Optimization computes maxima and minima.