Overview

9 Floyd-Warshall Algorithm

The chapter presents Floyd-Warshall as the dynamic programming solution for the all-pairs shortest path problem, especially useful when you need the exact minimum distance between every pair of nodes in a dense graph. Rather than repeatedly running a single-source algorithm from each vertex, it builds a distance matrix that starts with direct edge weights, zeros on the diagonal, and infinity for unreachable pairs. The central idea is simple but powerful: for each possible intermediate vertex, the algorithm checks whether routing through that vertex produces a shorter path than the best one known so far.

The text explains the algorithm step by step through matrix updates, showing how each phase expands the set of allowed intermediate nodes. The recurrence compares the current path from i to j against a detour through k, and the matrix is updated in place whenever the detour is cheaper. This layered process is what makes the method dynamic programming: each new matrix is built on the discoveries of the previous one, until all possible intermediates have been considered and the final table contains the optimal route cost for every source-destination pair.

It also emphasizes when Floyd-Warshall is and is not appropriate. The algorithm runs in O(V3) time and O(V2) space, so it is practical for bounded, dense graphs but inefficient for sparse or very large networks. Unlike Dijkstra’s algorithm, it can handle negative edge weights, and it can detect negative cycles by checking whether any diagonal entry becomes negative after completion. The chapter closes by highlighting real-world uses such as knowledge graphs, RAG systems, and transit analysis, where precomputing a global shortest-path table enables extremely fast O(1) lookup during query time.

The initial state of the network. The left panel shows the directed, weighted graph. The right panel shows the baseline distance matrix (A0). Existing direct paths are recorded with their cost, while missing connections are initialized to infinity (∞). The diagonal is zero, as the distance from any node to itself is zero.
Phase one of the algorithm (k=1). Node 1 is tested as an intermediate bridge for every pair of nodes. The algorithm discovers that routing a path from Node 4 to Node 2 via Node 1 costs 4 (2 + 2 = 4). Since 4 is less than the previous cost of infinity, a shorter route is found, and the matrix is overwritten in place to form A1
Phase two of the algorithm (k=2). Using Node 2 as the new bridge, the algorithm layers its discoveries strictly on top of the updated A1 matrix. Three new detours are found to be cheaper than their current paths, successfully connecting Node 1 to Node 4, Node 3 to Node 1, and Node 3 to Node 4.
The final phases of the algorithm. Testing Node 3 as a bridge (k=3) yields no new shortcuts, meaning A3 is an identical copy of A2. Finally, testing Node 4 (k=4) resolves the final infinite paths in the graph. The completed A4 matrix represents the absolute optimal shortest path between every single combination of nodes.

Summary

  • The Floyd-Warshall algorithm is a dynamic programming method used to solve the All-Pairs Shortest Path (APSP) problem on weighted graphs.
  • Unlike single-source algorithms like Dijkstra’s, Floyd-Warshall computes the optimal path distance between every possible pair of vertices in a single, unified execution.
  • The decision engine relies on a fundamental transition formula - Ak[i][j] = min(Ak-1[i][j], Ak-1[i][k] + Ak-1[k][j])
  • The algorithm works in phases by systematically testing every vertex in the graph as a potential intermediate “bridge” node (k) to discover shortcut routes.
  • It evaluates whether routing a path through an intermediate vertex (k) is cheaper than the currently known path between a starting vertex (i) and a destination vertex (j).
  • The algorithm executes three nested loops (bridge k, start i, destination j), resulting in a strict cubic time complexity of O(V3).
  • The runtime is unconditional with respect to edge count, Floyd-Warshall performs the exact same V3 operations on a graph with 10 edges as it does on a graph with 1,000 edges.
  • The space complexity is O(V2), as it only requires maintaining a 2D adjacency matrix representing the distances between all nodes.
  • Space efficiency can be optimized to update the V x V matrix completely in-place without storing historical iteration states in memory.
  • Floyd-Warshall strictly trades upfront compute time for downstream speed, producing a pre-computed global matrix that enables instantaneous O(1) lookup times.
  • The algorithm natively supports negative edge weights, unlike Dijkstra's algorithm which completely fails in the presence of negative costs.
  • It serves as an automated diagnostic tool for detecting negative weight cycles (loops whose total cost sums to less than zero).
  • A negative cycle is mathematically proven to exist if any diagonal element in the distance matrix becomes negative (D[i][i] < 0) after execution.
  • Floyd-Warshall is optimal for dense graphs (E ~ V2), where high connectivity justifies the O(V3) compute expenditure.
  • For sparse graphs (E << V2), running a Single-Source Shortest Path algorithm (like Dijkstra) repeatedly from each node is computationally far more efficient than Floyd-Warshall.
  • Systems use Floyd-Warshall to compute all-pairs transitive closure, pre-computing proximity matrices so RAG context engines can evaluate entity reachability in O(1) time.
  • Datacenter control planes periodically run Floyd-Warshall to generate pre-computed routing tables, giving servers instantaneous alternative paths if a hardware connection fails.
  • Transit agencies and navigation engines model intersections and stations as dense subgraphs to analyze multi-stop routes, identify systemic traffic bottlenecks, and perform instant real-time rerouting.
  1. Original Paper: https://dl.acm.org/doi/10.1145/367766.368168
  2. Code implementation: https://github.com/aniketwattamwar/Algorithms-Every-Programmer-Should-Know

FAQ

What problem does the Floyd-Warshall algorithm solve?It solves the all-pairs shortest path (APSP) problem, finding the shortest path between every pair of nodes in a graph.
How is Floyd-Warshall different from Dijkstra’s algorithm?Dijkstra’s algorithm is single-source, meaning it finds shortest paths from one starting node to all others. Floyd-Warshall computes shortest paths for all node pairs at once.
Why is Floyd-Warshall especially useful for dense graphs?Because it always performs a fixed O(V^3) set of updates, it fits dense graphs well where many connections exist and repeated single-source runs would be wasteful.
What is the core dynamic programming formula used by Floyd-Warshall?The update rule is: Ak[i][j] = min(Ak-1[i][j], Ak-1[i][k] + Ak-1[k][j]). It compares the current path with a detour through an intermediate node k.
What does the initial distance matrix contain?Direct edge weights where edges exist, 0 on the diagonal for paths from a node to itself, and infinity for pairs with no direct edge.
Can Floyd-Warshall handle negative edge weights?Yes. Unlike Dijkstra’s algorithm, Floyd-Warshall can handle negative edge weights correctly as long as the graph does not contain a negative weight cycle.
How can Floyd-Warshall detect a negative weight cycle?After the algorithm finishes, if any diagonal entry D[i][i] becomes less than 0, that indicates a negative cycle involving that vertex.
What are the time and space complexities of Floyd-Warshall?The time complexity is O(V^3) because it uses three nested loops. The space complexity is O(V^2) because it stores a V × V distance matrix.
Why does the algorithm update the matrix in place?In-place updates let each new phase build on the results of the previous one, reducing memory usage and supporting dynamic programming efficiently.
When is Floyd-Warshall a poor choice in practice?It is a poor choice for sparse or very large graphs, since O(V^3) computation and O(V^2) memory become expensive compared with single-source shortest path methods.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms Every Programmer Should Know ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Algorithms Every Programmer Should Know ebook for free