โ† Wiki Index / Computation & Algorithms / Floyd-Warshall Algorithm
๐Ÿ’ป Computation & Algorithms

Floyd-Warshall Algorithm

The Floyd-Warshall algorithm is a cubic-time dynamic-programming algorithm that computes all-pairs shortest paths in a weighted directed graph by iteratively expanding the set of permitted intermediate vertices.

Floyd-Warshall algorithm (also known as the Royโ€“Warshallโ€“Floyd algorithm) is a classic dynamic programming method within computation and algorithms that solves the all-pairs shortest-path (APSP) problem on a weighted directed graph G = (V, E, w) in ฮ˜(|V|3) time and ฮ˜(|V|2) space. In a single execution of three nested loops over the vertex indices {1, 2, ..., |V|}, the algorithm computes the minimum-weight directed path distance ฮด(i, j) between every ordered pair of vertices (i, j) โˆˆ V ร— V, accommodating positive and negative edge weights provided the graph contains no negative-weight cycles.

Discovered independently across three mathematical domainsโ€”by Bernard Roy in 1959 for graph path connectivity, by Stephen Warshall in January 1962 for the Boolean transitive closure of binary relations, and by Robert W. Floyd in June 1962 as Algorithm 97 in Communications of the ACMโ€”the algorithm executes branch-free, stride-1 matrix operations without priority-queue pointer overhead. Consequently, the Floyd-Warshall algorithm remains a standard tool for dense graphs (where |E| = ฮ˜(|V|2)), all-pairs patristic distance matrices in bioinformatics, and Kleene closure computations over closed semirings.

Dynamic Programming Formulation Over Intermediate Vertex Prefixes

Subproblem Parameterization by Intermediate Vertex Set

Let the vertices of G = (V, E, w) be numbered V = {1, 2, ..., n}. Rather than inducting on edge count as the Bellman-Ford algorithm does, Floyd's recurrence restricts the intermediate vertices permitted along a path. An intermediate vertex of a simple path p = โŸจv0, v1, ..., vrโŸฉ is any interior vertex vm โˆˆ {v1, ..., vrโˆ’1} excluding the endpoints v0 and vr.

For any subset prefix Vk = {1, 2, ..., k} with 0 โ‰ค k โ‰ค n, define d(k)(i, j) as the minimum weight of any directed path from vertex i to vertex j whose intermediate vertices belong to {1, 2, ..., k}. At base case k = 0 (V0 = โˆ…, no intermediate vertices allowed), a path from i to j uses at most a single direct edge (i, j) โˆˆ E:

d(0)(i, j) = 0 if i = j
d(0)(i, j) = w(i, j) if i โ‰  j and (i, j) โˆˆ E
d(0)(i, j) = +โˆž if i โ‰  j and (i, j) โˆ‰ E

Optimal Substructure and the Cubic Recurrence

Assuming G contains no negative-weight cycles, every shortest path is simple and visits any intermediate vertex k at most once. When advancing from stage k โˆ’ 1 to stage k (for k = 1, 2, ..., n), any shortest path p from i to j using intermediate vertices in {1, ..., k} falls into one of two cases:

  1. Vertex k is not an intermediate vertex of p: All intermediate vertices of p belong to {1, ..., k โˆ’ 1}, so the path weight is d(kโˆ’1)(i, j).
  2. Vertex k is an intermediate vertex of p: Because p is simple, p visits k once and splits into a subpath p1 from i to k followed by a subpath p2 from k to j, where both subpaths use intermediate vertices only from {1, ..., k โˆ’ 1}.

Taking the minimum of these two cases yields the Floyd-Warshall recurrence equation:

d(k)(i, j) = min { d(kโˆ’1)(i, j), d(kโˆ’1)(i, k) + d(kโˆ’1)(k, j) } for k = 1, 2, ..., n

At k = n, the permitted intermediate set {1, ..., n} equals V, giving d(n)(i, j) = ฮด(i, j) for all (i, j) โˆˆ V ร— V.

Space Compression to ฮ˜(|V|ยฒ) and Negative-Cycle Diagnostics

During stage k, pivot row k and pivot column k remain invariant because d(k)(i, k) = min{d(kโˆ’1)(i, k), d(kโˆ’1)(i, k) + d(kโˆ’1)(k, k)} = d(kโˆ’1)(i, k) whenever d(kโˆ’1)(k, k) โ‰ฅ 0. Consequently, all updates execute in-place within a single n ร— n matrix D[i, j]:

For k = 1 to n:
For i = 1 to n:
For j = 1 to n:
D[i, j] โ† min ( D[i, j], D[i, k] + D[k, j] )

Placing the intermediate pivot k as the outermost loop is mandatory; nesting k innermost only inspects two-hop paths. A companion n ร— n successor table *Next*[i, j] (updated via *Next*[i, j] โ† *Next*[i, k] whenever D[i, k] + D[k, j] < D[i, j]) reconstructs the shortest path in O(length) hops, while negative diagonal entries (D[i, i] < 0) detect negative-weight cycles passing through vertex i.

Kleene Algebra, Transitive Closure, and Tropical Semirings

Warshall's Transitive Closure and Closed Semirings

Peter Ingerman (1962) and Alfred Aho, John Hopcroft, and Jeffrey Ullman (1974) showed that Warshall's transitive closure, Floyd's shortest paths, and Kleene's finite-automaton-to-regular-expression conversion are instances of Gaussโ€“Jordan elimination over a closed semiring (or Kleene algebra) (S, โŠ•, โŠ—, 0ฬ„, 1ฬ„):

A(k)[i, j] = A(kโˆ’1)[i, j] โŠ• ( A(kโˆ’1)[i, k] โŠ— (A(kโˆ’1)[k, k])* โŠ— A(kโˆ’1)[k, j] )

Substituting different algebraic semirings into the identical triple-loop template solves distinct problems across computational complexity theory:

Problem DomainUnderlying Set SSummary Operator โŠ•Extension Operator โŠ—Identities (0ฬ„, 1ฬ„)Kleene Star a*
Floyd-Warshall Shortest Pathsโ„ โˆช {+โˆž} (Tropical min-plus)min(a, b)a + b(+โˆž, 0)0 for a โ‰ฅ 0; โˆ’โˆž for a < 0
Warshall Transitive Closure{0, 1} (Boolean algebra)a โˆจ b (logical OR)a โˆง b (logical AND)(0, 1)1
Widest / Bottleneck Pathโ„โ‰ฅ0 โˆช {+โˆž} (Max-min)max(a, b)min(a, b)(0, +โˆž)+โˆž
Most Reliable Markov Path[0, 1] (Viterbi probability)max(a, b)a ร— b(0, 1)1
Kleene Automaton to RegexRegular expressions ฮฃ*R1 โˆช R2 (union)R1 ยท R2 (concatenation)(โˆ…, ฮต)R*

The APSP Conjecture, Tropical Semiring, and Scientific Applications

In the min-plus (tropical) semiring (โ„ โˆช {+โˆž}, min, +), the min operator lacks an additive inverse (subtraction), preventing direct application of Strassen-style fast divide-and-conquer algorithm matrix multiplication. Within fine-grained complexity theory, the All-Pairs Shortest Paths (APSP) Hypothesis conjectures that no algorithm solves general weighted APSP in truly subcubic O(n3โˆ’ฮต) time for any ฮต > 0; the fastest known upper bound is Ryan Williams's 2014 circuit-polynomial algorithm running in O(n3 / exp(ฮฉ((log n)1/2))) time.

In molecular phylogenetics and neighbor-joining, the Floyd-Warshall algorithm enforces metric triangle-inequality closure on incomplete evolutionary distance matrices derived from sequence alignment. In structural biology, it performs NMR nuclear Overhauser effect (NOE) distance-restraint bound smoothing prior to 3D coordinate embedding, and in network science it computes graph diameter, Wiener indices, and closeness centrality across proteinโ€“protein interaction networks.

Frequently Asked Questions

When is the Floyd-Warshall algorithm faster than running Dijkstra's algorithm from every vertex?

On a sparse graph (|E| = O(|V|)), running Dijkstra's algorithm |V| times takes O(|V|2 log |V| + |V|ยท|E|) = O(|V|2 log |V|) time, beating ฮ˜(|V|3). On a dense graph (|E| = ฮ˜(|V|2)), however, repeated Dijkstra performs O(|V|3) work with heavy heap-pointer overhead, whereas Floyd-Warshall's branchless inner loop vectorizes across SIMD lanes and GPUs, outperforming repeated Dijkstra on dense matrices.

How does the Floyd-Warshall algorithm detect negative-weight cycles?

Diagonal entries start at D[i, i] = 0. During execution, D[i, i] is updated to D[i, k] + D[k, i] whenever a closed walk from i through k and back to i has negative total weight. Thus, G has a negative-weight cycle if and only if D[i, i] < 0 for some vertex i upon termination.

References

  1. Roy, B. (1959). "Transitivitรฉ et connexitรฉ." Comptes Rendus Hebdomadaires des Sรฉances de l'Acadรฉmie des Sciences, 249, 216โ€“218. https://doi.org/10.1016/S0019-9958(59)90362-690362-6)
  2. Warshall, S. (1962). "A theorem on Boolean matrices." Journal of the ACM, 9(1), 11โ€“12. https://doi.org/10.1145/321105.321107
  3. Floyd, R. W. (1962). "Algorithm 97: Shortest path." Communications of the ACM, 5(6), 345. https://doi.org/10.1145/367766.368168
  4. Williams, R. (2018). "Faster all-pairs shortest paths via circuit complexity." SIAM Journal on Computing, 47(5), 1965โ€“1985. https://doi.org/10.1137/15M1024524
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision