Sub topic 14
Choosing an Algorithm: Bellman-Ford, Floyd-Warshall, or Dijkstra
This chapter assumes you know Big-O notation and have followed the earlier chapters on Bellman-Ford and Floyd-Warshall. Some familiarity with Dijkstra's algorithm helps. Three properties of the problem decide which algorithm fits best:
- Query pattern: you need distances from one source, or between every pair of vertices.
- Edge weights: some edges are negative, or all are non-negative.
- Density: the edge count E is close to V (sparse) or close to V² (dense).
The Cost Table
| Algorithm | Problem | Negative weights | Time | Extra space |
|---|---|---|---|---|
| Dijkstra (binary heap) | Single-source | No | O((V + E) log V) | O(V) |
| Bellman-Ford | Single-source | Yes, detects cycles | O(V · E) | O(V) |
| Floyd-Warshall | All-pairs | Yes, detects cycles | O(V³) | O(V²) |
| Dijkstra run from every vertex | All-pairs | No | O(V (V + E) log V) | O(V²) for output |
| Bellman-Ford run from every vertex | All-pairs | Yes | O(V² · E) | O(V²) for output |
Don't treat the last row as a real option. On a dense graph, E ≈ V², so repeated Bellman-Ford costs O(V⁴). Floyd-Warshall gives the same answers in O(V³).
Putting Numbers on It
Here are rough operation counts for V = 1,000, with log V ≈ 10:
| Graph | Bellman-Ford (one source) | Dijkstra (one source) | Floyd-Warshall (all pairs) | Dijkstra × V (all pairs) |
|---|---|---|---|---|
| Sparse, E = 5,000 | 5 × 10⁶ | 6 × 10⁴ | 10⁹ | 6 × 10⁷ |
| Dense, E = 10⁶ | 10⁹ | 10⁷ | 10⁹ | 10¹⁰ |
Two patterns stand out:
- Sparse all-pairs: repeated Dijkstra beats Floyd-Warshall by more than an order of magnitude. If the graph has negative edges, Johnson's algorithm (next chapter) first removes them, so you still get this speed.
- Dense all-pairs: Floyd-Warshall wins. It also has three tight loops, no heap, and memory access that suits the cache, so its constant factor is small.
A Decision Procedure
The same rules as a function:
def choose_algorithm(n, m, all_pairs, has_negative_edge):
# n vertices, m edges
dense = m >= n * n // 4
if not all_pairs:
return "bellman_ford" if has_negative_edge else "dijkstra"
if dense or n <= 500:
return "floyd_warshall"
return "johnson" if has_negative_edge else "dijkstra_from_every_vertex"
print(choose_algorithm(1000, 5000, False, True)) # bellman_ford
print(choose_algorithm(1000, 1_000_000, True, True)) # floyd_warshall
print(choose_algorithm(10000, 40000, True, True)) # johnsonCommon Mistakes
- Running Dijkstra with a negative edge. Dijkstra never revisits a vertex once it is finalized, so it can return a wrong distance without any error. Only use it after you have checked that every weight is ≥ 0.
- Ignoring memory. Floyd-Warshall stores V² distances, plus V² next-hop entries if you need paths. At V = 50,000 the distance matrix alone holds 2.5 × 10⁹ entries, which is too much for most machines before you even count running time.
- Choosing Bellman-Ford only for cycle detection. If you only need to know whether a negative cycle exists anywhere in the graph, add a virtual source with zero-weight edges to every vertex and run Bellman-Ford once. That costs O(V · E). Running Floyd-Warshall for the same question costs O(V³).
Exercise
A road network has 200,000 intersections and 500,000 one-way roads, and every travel time is positive. You need the fastest route from one depot to every intersection. Which algorithm should you use, and roughly how much faster is it than Bellman-Ford?
Show answer
Use Dijkstra, because all weights are non-negative and you need distances from a single source. Dijkstra costs about (V + E) log V ≈ 700,000 × 18 ≈ 1.3 × 10⁷ operations. Bellman-Ford costs up to V · E = 10¹¹. Dijkstra is roughly 7,000 times faster in the worst case.
Exercise
A graph has 400 vertices and about 150,000 edges, and some of the weights are negative. You need distances between every pair. Which algorithm fits, and why not Bellman-Ford from every vertex?
Show answer
Use Floyd-Warshall. The graph is dense, and Floyd-Warshall needs 400³ = 6.4 × 10⁷ steps. Running Bellman-Ford from every vertex needs V² · E = 160,000 × 150,000 = 2.4 × 10¹⁰ steps, which is several hundred times more work.