Floyd–Warshall algorithm
   HOME

TheInfoList



OR:

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 R, 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 \Theta(, V, ^3) comparisons in a graph, even though there may be \Theta (, V, ^2) 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 G with vertices V numbered 1 through N. Further consider a function \mathrm(i,j,k) that returns the length of the shortest possible path (if one exists) from i to j 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 i to each j using ''any'' vertex in \. By definition, this is the value \mathrm(i,j,N), which we will find recursively. Observe that \mathrm(i,j,k) must be less than or equal to \mathrm(i,j,k-1): we have ''more'' flexibility if we are allowed to use the vertex k. If \mathrm(i,j,k) is in fact less than \mathrm(i,j,k-1), then there must be a path from i to j using the vertices \ that is shorter than any such path that does not use the vertex k. Since there are no negative cycles this path can be decomposed as: :(1) a path from i to k that uses the vertices \, followed by :(2) a path from k to j 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: : \mathrm(i,j,k) = :: \mathrm\Big(\mathrm(i,j,k-1), ::: \mathrm(i,k,k-1)+\mathrm(k,j,k-1)\Big). The base case is given by : \mathrm(i,j,0) = w(i,j), where w(i,j) denotes the weight of the edge from i to j if one exists and ∞ (infinity) otherwise. These formulas are the heart of the Floyd–Warshall algorithm. The algorithm works by first computing \mathrm(i,j,k) for all (i,j) pairs for k=0, then k=1, then k=2, and so on. This process continues until k=N, and we have found the shortest path for all (i,j) 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 i, j which form part of a negative cycle, because path-lengths from i to 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), including where i=j; * Initially, the length of the path (i,i) is zero; * A path ,k,\ldots,i/math> can only improve upon this if it has length less than zero, i.e. denotes a negative cycle; * Thus, after the algorithm, (i,i) will be negative if there exists a negative-length path from i back to 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. However, when a negative cycle is present, during the execution of the algorithm exponentially large numbers on the order of \Omega(6^n \cdot w_) can appear, where w_ is the largest absolute value edge weight in the graph. To avoid integer underflow problems, one should check for a negative cycle within the innermost for loop of the algorithm.


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, we can use the shortest-path tree, which can be calculated for each node in \Theta(, E, ) time using \Theta(, V, ) memory, and allows us to efficiently reconstruct a directed path between any two connected vertices.


Pseudocode

The array holds the penultimate vertex on the path from to (except in the case of , where it always contains even if there is no self-loop on ): let dist be a , V, \times , V, array of minimum distances initialized to \infty (infinity) let prev be a , V, \times , V, array of vertex indices initialized to null procedure ''FloydWarshallWithPathReconstruction''() is for each edge (u, v) do dist v] = w(u, v) ''// The weight of the edge (u, v)'' prev v] = u for each vertex v do dist v] = 0 prev v] = v for k from 1 to , V, do ''// standard Floyd-Warshall implementation'' for i from 1 to , V, for j from 1 to , V, if dist j] > dist k] + dist j] then dist j] = dist k] + dist j] prev j] = prev j] procedure ''Path''(u, v) is if prev v] = null then return [] path = [v] while ''u'' ≠ ''v'' do v = prev v] path.prepend(v) return path


Time complexity

Let n be , V, , the number of vertices. To find all n^2 of \mathrm(i,j,k) (for all i and j) from those of \mathrm(i,j,k-1) requires \Theta(n^2) operations. Since we begin with \mathrm(i,j,0) = \mathrm(i,j) and compute the sequence of n matrices \mathrm(i,j,1), \mathrm(i,j,2), \ldots, \mathrm(i,j,n), each having a cost of \Theta(n^2), the total
time complexity In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations ...
of the algorithm is n \cdot \Theta(n^2) = \Theta(n^3).


Applications and generalizations

The Floyd–Warshall algorithm can be used to solve the following problems, among others: * Shortest paths in directed graphs (Floyd's algorithm). *
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 directed graphs (Warshall's algorithm). In Warshall's original formulation of the algorithm, the graph is unweighted and represented by a Boolean
adjacency matrix In graph theory and computer science, an adjacency matrix is a square matrix used to represent a finite graph (discrete mathematics), graph. The elements of the matrix (mathematics), matrix indicate whether pairs of Vertex (graph theory), vertices ...
. Then the addition operation is replaced by
logical conjunction In logic, mathematics and linguistics, ''and'' (\wedge) is the Truth function, truth-functional operator of conjunction or logical conjunction. The logical connective of this operator is typically represented as \wedge or \& or K (prefix) or ...
(AND) and the minimum operation by
logical disjunction In logic, disjunction (also known as logical disjunction, logical or, logical addition, or inclusive disjunction) is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language sentence "it is ...
(OR). * Finding 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" ...
denoting the
regular language In theoretical computer science and formal language theory, a regular language (also called a rational language) is a formal language that can be defined by a regular expression, in the strict sense in theoretical computer science (as opposed to ...
accepted by a finite automaton (
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 ...
, a closely related generalization of the Floyd–Warshall algorithm) * Inversion of real
matrices Matrix (: matrices or matrixes) or MATRIX may refer to: Science and mathematics * Matrix (mathematics), a rectangular array of numbers, symbols or expressions * Matrix (logic), part of a formula in prenex normal form * Matrix (biology), the ...
( Gauss–Jordan algorithm) * 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. * Fast computation of Pathfinder networks. * Widest paths/Maximum bandwidth paths * Computing canonical form of difference bound matrices (DBMs) * Computing the similarity between graphs * Transitive closure in AND/OR/threshold graphs.


Implementations

Implementations are available for many
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s. * For C++, in th
boost::graph
library * For C#, a
QuikGraph
* For C#, a
QuickGraphPCL
(A fork of QuickGraph with better compatibility with projects using Portable Class Libraries.) * For
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, in th
Apache Commons Graph
library * For
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
, in the
Cytoscape Cytoscape is an Open-source software, open source bioinformatics software platform for Visualization (graphic), visualizing Metabolic network modelling, molecular interaction networks and integrating with gene expression profiles and other state da ...
library * For Julia, in th
Graphs.jl
package * For
MATLAB MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
, in th
Matlab_bgl
package * For
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
, in th
Graph
module * For Python, in the
SciPy SciPy (pronounced "sigh pie") is a free and open-source Python library used for scientific computing and technical computing. SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, fast Fourier ...
library (modul
scipy.sparse.csgraph
or NetworkX library * For R, in package
e1071
an

* For C, a pthreads
parallelized
implementation including a
SQLite SQLite ( "S-Q-L-ite", "sequel-ite") is a free and open-source relational database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it ...
interface to the data a
floydWarshall.h


Comparison with other shortest path algorithms

For graphs with non-negative edge weights,
Dijkstra's algorithm Dijkstra's algorithm ( ) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three ...
can be used to find all shortest paths from a ''single'' vertex with running time \Theta(, E, + , V, \log , V, ). Thus, running Dijkstra starting at ''each'' vertex takes time \Theta(, E, , V, + , V, ^2 \log , V, ). Since , E, = O(, V, ^2), this yields a worst-case running time of repeated Dijkstra of O(, V, ^3). While this matches the asymptotic worst-case running time of the Floyd-Warshall algorithm, the constants involved matter quite a lot. When a graph is dense (i.e., , E, \approx , V, ^2), the Floyd-Warshall algorithm tends to perform better in practice. When the graph is sparse (i.e., , E, is significantly smaller than , V, ^2), Dijkstra tends to dominate. 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).. 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.


References


External links


Interactive animation of the Floyd–Warshall algorithm


{{DEFAULTSORT:Floyd-Warshall algorithm Graph algorithms Routing algorithms Polynomial-time problems Articles with example pseudocode Dynamic programming Graph distance