1. Gyanastra
  2. Algorithms
  3. Floyd-Warshall Algorithm
  4. Running Dijkstra and Bellman-Ford from Every Vertex (Complexity)
Free lesson, no account needed 4 min read 2 practice questions after it

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.
Graph diagram: Running Dijkstra and Bellman-Ford from Every Vertex (Complexity)
Figure. Running Dijkstra and Bellman-Ford from Every Vertex (Complexity)

How it works

  1. Run an SSSP algorithm (Dijkstra or Bellman-Ford) with each vertex as source.
  2. Each run gives shortest distances from that source to all destinations.
  3. Total cost = (cost of one SSSP run) × V.
  4. Dijkstra cannot handle negative weight edges, so for graphs with negative weights Bellman-Ford must be used.
  5. Both resulting costs are too large, motivating dynamic programming (Floyd-Warshall) for APSP.

Formulas

  • Dijkstra with binary min-heap (one source): O(Elog⁡V)O(E \log V) — EE = number of edges, VV = number of vertices; each decrease-key costs log⁡V\log V, and decrease-key is done O(E)O(E) times.
  • APSP by running Dijkstra from every vertex: O(V⋅Elog⁡V)O(V \cdot E \log V)
  • Dense graph (E=O(V2)E = O(V^2)) with Dijkstra: O(V⋅V2log⁡V)=O(V3log⁡V)O(V \cdot V^2 \log V) = O(V^3 \log V); for sparse graphs keep the form O(VElog⁡V)O(V E \log V).
  • Bellman-Ford (one source): O(V⋅E)O(V \cdot E)
  • APSP by running Bellman-Ford from every vertex: O(V2⋅E)O(V^2 \cdot E)
  • Dense graph (E=O(V2)E = O(V^2)) with Bellman-Ford: O(V2⋅V2)=O(V4)O(V^2 \cdot V^2) = O(V^4)

Comparison

Method for APSPOne-source costTotal cost (all sources)Dense graph costHandles negative edges?
Dijkstra (binary min-heap) from every vertexO(Elog⁡V)O(E \log V)O(VElog⁡V)O(V E \log V)O(V3log⁡V)O(V^3 \log V)No
Bellman-Ford from every vertexO(VE)O(V E)O(V2E)O(V^2 E)O(V4)O(V^4)Yes

Revision summary

  • Running SSSP from every vertex gives APSP, but it is computationally slow.
  • Dijkstra with binary min-heap costs O(Elog⁡V)O(E \log V); the decrease-key operation costs log⁡V\log V.
  • APSP via Dijkstra costs O(VElog⁡V)O(V E \log V); for dense graphs O(V3log⁡V)O(V^3 \log V).
  • Dijkstra fails on negative weight edges, so Bellman-Ford is needed for such graphs.
  • APSP via Bellman-Ford costs O(V2E)O(V^2 E); for dense graphs O(V4)O(V^4).
  • Both O(V4)O(V^4) and O(V3log⁡V)O(V^3 \log V) 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 O(V3)O(V^{3}) 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 O(E+Vlog⁡V)O(E + V \log V) per source, so APSP by running it from every vertex costs O(VE+V2log⁡V)O(V E + V^{2} \log V).
  • 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 (O(VElog⁡V)O(V E \log V)) is asymptotically better than Floyd-Warshall (O(V3)O(V^{3})); 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 O(VE+V2log⁡V)O(V E + V^{2} \log V) with a Fibonacci heap (O(VElog⁡V)O(V E \log V) 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 O(V2)O(V^{2}) space.
Pages 1–2 of 2