Sub topic 6
Why V-1 Rounds Are Enough
This chapter assumes you already know how edge relaxation works and have seen the Bellman-Ford loop from the previous chapter. It answers one question: why does the outer loop run exactly V-1 times, and why is that always enough when no negative cycle is reachable?
The key fact: shortest paths are short
If no negative cycle is reachable from the source, then every reachable vertex has a shortest path that is simple. A simple path visits no vertex twice. The reason is that any cycle on a path has weight zero or more, so cutting it out never makes the path longer.
A simple path in a graph with V vertices visits at most V vertices. That means it uses at most V-1 edges. This bound is the whole basis for the loop count.
The invariant
Bellman-Ford keeps this promise after each round:
After round i, dist[v] is at most the weight of the shortest path from the source to v that uses at most i edges.
The proof is by induction on i.
- Base case (i = 0).
dist[source] = 0, which is the weight of the empty path. Every other vertex hasinf, and no path with zero edges reaches those vertices. - Inductive step. Take a shortest path to v with at most i edges, and let its last edge be (u, v, w). The part ending at u uses at most i-1 edges, so by the hypothesis
dist[u]is at most that part's weight after round i-1. During round i, Bellman-Ford relaxes (u, v, w), which setsdist[v]to at mostdist[u] + w. That value is no more than the full path's weight.
Every value in dist is also the weight of some real path, so dist never drops below the true shortest distance. Combining the invariant with the V-1 edge bound, after round V-1 each dist[v] equals the true shortest distance.
Seeing the worst case
Edge order affects how fast distances settle. It never affects correctness. In the worst case, each round moves correct information forward by only one edge. Listing a chain's edges in reverse order produces this case:
import math
def rounds_trace(n, edges, source):
dist = [math.inf] * n
dist[source] = 0
for r in range(1, n):
for u, v, w in edges:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
print(r, dist)
# Chain 0 -> 1 -> 2 -> 3, edges listed back to front
edges = [(2, 3, 1), (1, 2, 1), (0, 1, 1)]
rounds_trace(4, edges, 0)
# 1 [0, 1, inf, inf]
# 2 [0, 1, 2, inf]
# 3 [0, 1, 2, 3]Vertex 3 is three edges from the source, so it needs all V-1 = 3 rounds.
Common mistakes
| Mistake | Consequence |
|---|---|
| Running V rounds "to be safe" | Extra work, and it hides the purpose of the V-th pass as a detection check |
| Running fewer than V-1 rounds | Long shortest paths can end up with distances that are too high |
| Assuming the edge order changes the result | The order changes only how many rounds run before distances settle |
Exercise
A graph has 6 vertices, and no negative cycle is reachable from the source. The shortest path to vertex t uses 2 edges. Bellman-Ford's outer loop runs 5 rounds. After which round is dist[t] guaranteed to be correct, and why?
Show answer
After round 2. The invariant says that after round i, dist[t] is at most the weight of the best path using at most i edges. The true shortest path uses 2 edges, so dist[t] reaches the true distance by round 2 at the latest. Later rounds can't lower it further, because dist[t] never drops below the true shortest distance.
Exercise
Why can't we stop after V-2 rounds in general?
Show answer
A shortest simple path can use up to V-1 edges, as in a chain graph. In the worst edge order, each round fixes only one edge of the path, so the last vertex needs round V-1.