In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, the Floyd–Warshall algorithm (also known as Floyd's algorithm, the Roy–Warshall algorithm, the Roy–Floyd algorithm, or the WFI algorithm) is an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
for finding
shortest paths in a directed
weighted graph
This is a glossary of graph theory. Graph theory is the study of graphs, systems of nodes or vertices connected in pairs by lines or edges
Edge or EDGE may refer to:
Technology Computing
* Edge computing, a network load-balancing system
* ...
with positive or negative edge weights (but with no negative cycles). 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
, or (in connection with the
Schulze voting system)
widest paths between all pairs of vertices in a weighted graph.
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
Bernard Roy (; 15 March 1934 – 28 October 2017) was an emeritus professor at the Université Paris-Dauphine. In 1974 he founded the "Laboratoire d'Analyse et de Modélisation des Systèmes pour l'Aide à la Décision" ( Lamsade). He was Presiden ...
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
A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
. The modern formulation of the algorithm as three nested for-loops was first described by Peter Ingerman, also in 1962.
Algorithm
The Floyd–Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able to do this with
comparisons in a graph, even though there may be up to
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
with vertices
numbered 1 through
. Further consider a function
that returns the shortest possible path (if one exists) from
to
using vertices only from the set
as intermediate points along the way. Now, given this function, our goal is to find the shortest path from each
to each
using ''any'' vertex in
.
For each of these pairs of vertices, the
could be either
:(1) a path that does not go through
(only uses vertices in the set
.)
or
:(2) a path that does go through
(from
to
and then from
to
, both only using intermediate vertices in
)
We know that the best path from
to
that only uses vertices
through
is defined by
, and it is clear that if there was a better path from
to
to
, then the length of this path would be the concatenation of the shortest path from
to
(only using intermediate vertices in
) and the shortest path from
to
(only using intermediate vertices in
).
If
is the weight of the edge between vertices
and
, we can define
in terms of the following
recursive formula: the base case is
:
and the recursive case is
:
::
:::
.
This formula is the heart of the Floyd–Warshall algorithm. The algorithm works by first computing
for all
pairs for
, then
, and so on. This process continues until
, and we have found the shortest path for all
pairs using any intermediate vertices. Pseudocode for this basic version follows:
let dist be a , V, × , V, array of minimum distances initialized to ∞ (infinity)
for each edge (''u'', ''v'') do
dist
'u''''v''] ← w(''u'', ''v'') ''// The weight of the edge (''u'', ''v'')''
for each vertex ''v'' do
dist
'v''''v''] ← 0
for ''k'' from 1 to , V,
for ''i'' from 1 to , V,
for ''j'' from 1 to , V,
if dist
'i''''j''] > dist
'i''''k''] + dist
'k''''j'']
dist
'i''''j''] ← dist
'i''''k''] + dist
'k''''j'']
end if
Example
The algorithm above is executed on the graph on the left below:

Prior to the first recursion of the outer loop, labeled above, the only known paths correspond to the single edges in the graph. At , paths that go through the vertex 1 are found: in particular, the path
,1,3is found, replacing the path
,3which has fewer edges but is longer (in terms of weight). At , paths going through the vertices are found. The red and blue boxes show how the path
,2,1,3
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
is assembled from the two known paths
,2and
,1,3encountered in previous iterations, with 2 in the intersection. The path
,2,3is not considered, because
,1,3is the shortest path encountered so far from 2 to 3. At , paths going through the vertices are found. Finally, at , all shortest paths are found.
The distance matrix at each iteration of , with the updated distances in bold, will be:
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
,
which form part of a negative cycle, because path-lengths from
to
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
, including where
;
* Initially, the length of the path
is zero;
* A path