Sub topic 3
Edge Relaxation: The Core Operation
This chapter assumes you know what a weighted directed graph is, can read basic Python, and understand that a shortest-path algorithm keeps a distance estimate for each vertex. Bellman-Ford, Floyd-Warshall, and Dijkstra all depend on one small step called edge relaxation. Each algorithm decides in its own way which edges to relax and in what order.
The working model
Every vertex v has a distance estimate dist[v]. At the start, the source gets 0 and every other vertex gets infinity. Relaxing an edge (u, v) with weight w asks one question: does going to u first and then taking this edge give a shorter route to v than the best route found so far?
If it does, you lower dist[v] and record u as the predecessor of v. If it doesn't, nothing changes.
A worked example
import math
def relax(u, v, w, dist, pred):
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
pred[v] = u
return True
return False
dist = [0, math.inf, math.inf]
pred = [None, None, None]
print(relax(1, 2, -3, dist, pred), dist) # False [0, inf, inf]
print(relax(0, 1, 4, dist, pred), dist) # True [0, 4, inf]
print(relax(0, 2, 5, dist, pred), dist) # True [0, 4, 5]
print(relax(1, 2, -3, dist, pred), dist) # True [0, 4, 1]
print(relax(0, 2, 5, dist, pred), dist) # False [0, 4, 1]
print(pred) # [None, 0, 1]The first call does nothing because vertex 1 has not been reached yet, and inf + (-3) is still inf. Once dist[1] becomes 4, the same edge lowers dist[2] from 5 to 1. This shows that the order of relaxation matters. A single pass in a poor order can miss improvements, which is why Bellman-Ford repeats its passes.
Two invariants that make relaxation safe
- Upper bound:
dist[v]is always the length of some real path from the source, or infinity. So it never drops below the true shortest distance, as long as no negative cycle exists. - Monotonic: relaxation only lowers estimates. Once
dist[v]equals the true shortest distance, no later relaxation can change it.
Together these mean you can relax edges in any order, as many times as you like, without losing correctness. The only open question is how many relaxations are needed. A later chapter answers that for Bellman-Ford.
Common errors
| Mistake | Effect |
|---|---|
| Using `<=` instead of `<` | Predecessors switch between equal-length paths, and you lose the "no change" signal used for early exit |
| Updating `dist[v]` but not `pred[v]` | Distances are correct, but you can't rebuild the paths |
| Integer sentinel such as `INF = 10**9` | `INF + (-3) < INF` is true, so unreachable vertices get fake finite distances |
Exercise
With INF = 10**9, dist = [0, INF, INF], you relax edge (1, 2, -5) without guarding against INF. What is dist[2] afterward, and why is it wrong?
Show answer
dist[2] becomes 999999995, because INF + (-5) < INF. The value is wrong because vertex 1 is unreachable, so no real path to vertex 2 exists and dist[2] should stay INF. The fix is to skip relaxation whenever dist[u] == INF.
Exercise
In the worked example, why does the final call relax(0, 2, 5, ...) return False?
Show answer
dist[2] is already 1, and dist[0] + 5 = 5 is not less than 1. A shorter route through vertex 1 has already been recorded.