Sub topic 6
Why V-1 Rounds Are Enough
The Bellman-Ford loop runs n - 1 times. That number comes from a proof about what each round guarantees, and this chapter walks through it. The proof also explains when you need fewer rounds, when you need every one of them, and why the guarantee fails once a negative cycle is present.
This chapter assumes you already know edge relaxation, the basic Bellman-Ford loop from the previous chapter, and proof by induction.
The Claim
Let δ(s, v) be the true shortest-path distance from source s to vertex v. Assume no negative cycle is reachable from s. After V - 1 rounds, where each round relaxes every edge once, dist[v] = δ(s, v) for every vertex v.
The proof combines three facts:
dist[v]never drops belowδ(s, v).- Some shortest path to each reachable vertex uses at most
V - 1edges. - Round
ifixes every vertex whose shortest path usesiedges.
Fact 1: Distances Never Undershoot
Each time Bellman-Ford sets dist[v] = dist[u] + w, the new value is the weight of a real walk: a walk to u followed by the edge (u, v). Infinity means no walk has been found yet. So dist[v] is always the weight of some actual walk from s to v, and no walk can be shorter than the shortest one:
dist[v] >= δ(s, v) at every momentThis is the upper-bound property. Once dist[v] reaches δ(s, v), no relaxation can lower it further, so it stays there.
Fact 2: Shortest Paths Need at Most V-1 Edges
A simple path never repeats a vertex. In a graph with V vertices, a simple path visits at most V vertices and so uses at most V - 1 edges.
Suppose a shortest walk from s to v repeats a vertex. Then it contains a cycle. With no negative cycles, that cycle weighs zero or more, so cutting it out gives a walk that is no heavier. Repeat until no vertex repeats. The result is a simple path that is still shortest.
So for every reachable v, at least one shortest path has k ≤ V - 1 edges.
Fact 3: Round i Fixes Vertices i Edges Deep
Take a shortest path with k edges:
s = v0 → v1 → v2 → ... → vk = vEvery prefix v0 → ... → vi is itself a shortest path to vi. If a shorter route to vi existed, you could swap it into the full path and get a shorter path to v, which contradicts the assumption that the full path is shortest.
Invariant. After round i, dist[vi] = δ(s, vi).
- Base case (i = 0). Before any round,
dist[s] = 0. The valueδ(s, s)is also 0, because a negative value would require a negative cycle throughs. - Inductive step. Assume that after round
i - 1,dist[v(i-1)] = δ(s, v(i-1)). Roundirelaxes every edge, including(v(i-1), vi). After that relaxation:
dist[vi] <= dist[v(i-1)] + w(v(i-1), vi)
= δ(s, v(i-1)) + w(v(i-1), vi)
= δ(s, vi)Fact 1 gives dist[vi] >= δ(s, vi), so the two values are equal.
By Fact 2, k ≤ V - 1, so after V - 1 rounds every vertex on every such path, including v, holds its final distance. Unreachable vertices are never relaxed from a finite value, so they stay at infinity, which is also correct.
| After round | Guaranteed correct |
|---|---|
| 0 | `s` |
| 1 | every vertex whose shortest path has 1 edge |
| i | every vertex whose shortest path has at most `i` edges |
| V - 1 | every reachable vertex |
"At Least as Good" Versus "Exactly i Edges"
The invariant is a lower limit on progress. Within a round, the code updates dist in place, so a value improved earlier in the round can feed later edges in that same round. One round can therefore push information along many edges. In-place Bellman-Ford does at least as well as the strict "at most i edges" values, and often better.
The script below compares the two versions on the tutorial's example graph. The strict version copies the array before each round, so its round-i values are exactly the best distances using at most i edges.
import math
edges = [(0, 1, 4), (0, 2, 5), (1, 2, -3), (2, 3, 4), (1, 3, 6)]
n, source = 4, 0
def relax_round(dist, edges, in_place):
src = dist if in_place else dist[:] # strict version reads last round's values
for u, v, w in edges:
if src[u] + w < dist[v]:
dist[v] = src[u] + w
strict = [math.inf] * n
inplace = [math.inf] * n
strict[source] = inplace[source] = 0
for r in range(1, n):
relax_round(strict, edges, in_place=False)
relax_round(inplace, edges, in_place=True)
print(f"round {r}: at-most-{r}-edges={strict} in-place={inplace}")Output:
round 1: at-most-1-edges=[0, 4, 5, inf] in-place=[0, 4, 1, 5]
round 2: at-most-2-edges=[0, 4, 1, 9] in-place=[0, 4, 1, 5]
round 3: at-most-3-edges=[0, 4, 1, 5] in-place=[0, 4, 1, 5]In this example the edge list happens to follow the order of the shortest path, so the in-place version finishes in one round. The strict version needs all three rounds, because the shortest path to vertex 3 uses three edges (0 → 1 → 2 → 3).
The Bound Is Tight
Edge order decides how fast the algorithm converges, but some edge orders really do need all V - 1 rounds. On a chain whose edges are listed backward, each round moves information forward by exactly one edge:
import math
def count_changing_rounds(n, edges, source):
dist = [math.inf] * n
dist[source] = 0
rounds = 0
for _ in range(n - 1):
changed = False
for u, v, w in edges:
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
changed = True
if not changed:
break
rounds += 1
return rounds, dist
forward = [(0, 1, 1), (1, 2, 1), (2, 3, 1), (3, 4, 1)]
backward = list(reversed(forward))
print(count_changing_rounds(5, forward, 0)) # (1, [0, 1, 2, 3, 4])
print(count_changing_rounds(5, backward, 0)) # (4, [0, 1, 2, 3, 4])With backward order, round 1 can only use (0, 1), because the edges listed before it start at vertices that are still at infinity. Round 2 is the first round that can use (1, 2), and so on. Four rounds are needed for five vertices, so V - 1 is a worst-case bound that real inputs can hit. The early-exit check that skips useless rounds gets its own chapter later.
Where the Argument Breaks
Each step of the proof depends on an assumption. These are the cases where an assumption fails:
| Assumption | What goes wrong if it fails |
|---|---|
| No reachable negative cycle | Fact 2 fails. Going around the cycle again always lowers the weight, so no simple path is shortest and `δ` is minus infinity. Distances keep dropping after round `V - 1`. |
| Every edge is relaxed every round | The inductive step needs edge `(v(i-1), vi)` in round `i`. If you skip edges, for example by relaxing only from vertices that changed and getting the bookkeeping wrong, the guarantee is lost. |
| Infinity plus a weight stays infinite | Python's `math.inf + (-3)` is still `inf`, so the comparison is safe. In C or Java, a sentinel such as `INT_MAX / 2` minus 3 is less than the sentinel, so unreachable vertices get fake finite distances. Guard with `dist[u] != INF` before relaxing. |
The first row is the useful one. If the graph has no negative cycle, nothing can change after round V - 1. So a change in round V proves that a negative cycle is reachable from the source. The next chapter uses exactly this fact.
Exercises
Exercise
A graph has 6 vertices and no negative cycles. After round 3 of Bellman-Ford, which vertices are guaranteed to have their final distance?
Show answer
Every vertex that has a shortest path from the source using at most 3 edges, plus the source itself. Vertices whose shortest paths need 4 or 5 edges may already be correct because of a lucky edge order, but nothing guarantees it until rounds 4 and 5.
Exercise
Why does the proof need every prefix of a shortest path to be a shortest path itself?
Show answer
The inductive step assumes that dist[v(i-1)] equals δ(s, v(i-1)) and then adds one edge. If the prefix to v(i-1) were not shortest, knowing that dist[v(i-1)] is correct would not tell you that following this particular path gives δ(s, vi). Optimal substructure is what links each round to the next.
Exercise
For a chain of V vertices with all edges listed in forward order (0→1, 1→2, ...), how many rounds change at least one distance? Why does that not contradict the V-1 bound?
Show answer
One round. In-place relaxation walks the whole chain in a single pass. V-1 is an upper bound on the rounds that are needed, not the exact number, and the reversed order shows that some inputs reach it.
Exercise
You want the cheapest route from A to B that uses at most 2 edges. You run in-place Bellman-Ford for 2 rounds and read dist[B]. What can go wrong, and how do you fix it?
Show answer
In-place updates can carry a distance across several edges within one round, so dist[B] may describe a path with more than 2 edges. Copy the distance array at the start of each round, read only from the copy, and run exactly 2 rounds.