Algorithms · Floyd-Warshall Algorithm
Running Dijkstra and Bellman-Ford from Every Vertex (Complexity)
Overview
All-pairs shortest path (APSP) requires the shortest path between every pair of vertices, which can be obtained by running a single-source shortest path (SSSP) algorithm once from every vertex.
This approach is correct but computationally slow, which motivates a dynamic-programming method (Floyd-Warshall) for APSP.
Key concepts
- All-pairs shortest path (APSP): every vertex is treated as a source and every other vertex as a destination; the shortest path from every node to every other node must be found (e.g. from A to C, and for all such pairs).
- Relation to single-source shortest path (SSSP): APSP can be obtained by running an SSSP algorithm once from every vertex. This works, but it is computationally slow.
- Dijkstra is a greedy algorithm: it always takes the minimum and then computes from it. It cannot handle negative weight edges.
- Bellman-Ford must be used instead of Dijkstra for graphs with negative weights.
- Both resulting costs are too large, which motivates a dynamic-programming method (Floyd-Warshall) for APSP.

How it works
- Run an SSSP algorithm (Dijkstra or Bellman-Ford) with each vertex as source.
- Each run gives shortest distances from that source to all destinations.
- Total cost = (cost of one SSSP run) × V.
- Dijkstra cannot handle negative weight edges, so for graphs with negative weights Bellman-Ford must be used.
- Both resulting costs are too large, motivating dynamic programming (Floyd-Warshall) for APSP.
Formulas
- Dijkstra with binary min-heap (one source): — = number of edges, = number of vertices; each decrease-key costs , and decrease-key is done times.
- APSP by running Dijkstra from every vertex:
- Dense graph () with Dijkstra: ; for sparse graphs keep the form .
- Bellman-Ford (one source):
- APSP by running Bellman-Ford from every vertex:
- Dense graph () with Bellman-Ford:
Comparison
| Method for APSP | One-source cost | Total cost (all sources) | Dense graph cost | Handles negative edges? |
|---|---|---|---|---|
| Dijkstra (binary min-heap) from every vertex | No | |||
| Bellman-Ford from every vertex | Yes |
Revision summary
- Running SSSP from every vertex gives APSP, but it is computationally slow.
- Dijkstra with binary min-heap costs ; the decrease-key operation costs .
- APSP via Dijkstra costs ; for dense graphs .
- Dijkstra fails on negative weight edges, so Bellman-Ford is needed for such graphs.
- APSP via Bellman-Ford costs ; for dense graphs .
- Both and are too much; dynamic programming gives a better APSP method.
- The next step is to apply dynamic programming to APSP, which is better than implementing it through Bellman-Ford or Dijkstra.
Previous-year exam insights
- Floyd-Warshall computes all-pairs shortest paths in time using a DP over intermediate vertices, and it works correctly with negative edge weights (but not with negative-weight cycles).
- Dijkstra with a Fibonacci heap runs in per source, so APSP by running it from every vertex costs .
- Bellman-Ford detects a negative-weight cycle reachable from the source if any edge can still be relaxed after V-1 rounds; Floyd-Warshall detects one if any diagonal entry dist[i][i] becomes negative.
- For sparse graphs, running Dijkstra from every vertex () is asymptotically better than Floyd-Warshall (); Floyd-Warshall is preferable for dense graphs.
- Johnson's algorithm reweights edges with Bellman-Ford potentials to make all weights non-negative, then runs Dijkstra from every vertex, giving with a Fibonacci heap ( with a binary heap) for APSP with negative edges allowed.
- Floyd-Warshall's recurrence is dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) for k = 1..V, and it needs space.