In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, 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 Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
for finding
shortest paths
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between t ...
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.
Symbols
A
B
...
with positive or negative edge weights (but with no negative cycles).
[ See in particular Section 26.2, "The Floyd–Warshall algorithm", pp. 558–565 and Section 26.4, "A general framework for solving path problems in directed graphs", pp. 570–576.] 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
In mathematics, the transitive closure of a homogeneous binary relation on a set (mathematics), set is the smallest Relation (mathematics), relation on that contains and is Transitive relation, transitive. For finite sets, "smallest" can be ...
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 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 In theoretical computer science, in particular in formal language theory, Kleene's algorithm transforms a given nondeterministic finite automaton (NFA) into a regular expression.
Together with other conversion algorithms, it establishes the equival ...
(published in 1956) for converting a
deterministic finite automaton
In the theory of computation, a branch of theoretical computer science, a deterministic finite automaton (DFA)—also known as deterministic finite acceptor (DFA), deterministic finite-state machine (DFSM), or deterministic finite-state auto ...
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 match pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
, with the difference being the use of a min-plus
semiring
In abstract algebra, a semiring is an algebraic structure. Semirings are a generalization of rings, dropping the requirement that each element must have an additive inverse. At the same time, semirings are a generalization of bounded distribu ...
. 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 many possible paths through the graph between each pair of vertices. It is guaranteed to find all shortest paths and is able to do this with
comparisons in a graph,
even though there may be
edges in the graph. 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 length of 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 length of the shortest path from each
to each
using ''any'' vertex in
. By definition, this is the value
, which we will find
recursively.
Observe that
must be less than or equal to
: we have ''more'' flexibility if we are allowed to use the vertex
. If
is in fact less than
, then there must be a path from
to
using the vertices
that is shorter than any such path that does not use the vertex
. Since there are no negative cycles this path can be decomposed as:
:(1) a path from
to
that uses the vertices
, followed by
:(2) a path from
to
that uses the vertices
.
And of course, these must be a ''shortest'' such path (or several of them), otherwise we could further decrease the length. In other words, we have arrived at the recursive formula:
:
::
:::
.
The base case is given by
:
where
denotes the weight of the edge from
to
if one exists and ∞ (infinity) otherwise.
These formulas are the heart of the Floyd–Warshall algorithm. The algorithm works by first computing
for all
pairs for
, then
, 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.
Pseudocode
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. Some typefaces render it as a small line, slightly curved or straight, but inclined from the vertical; others give it the appearance of a miniature fille ...
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