How Bellman-Ford relaxes every edge V minus 1 times

Short answer: Bellman-Ford relaxes every edge once per round, for |V|−1 rounds. A shortest path is simple when no negative cycle is reachable, so it has at most |V|−1 edges, and round k has considered every path of k edges. One more round that still relaxes an edge is a negative cycle reachable from the source. The time is Θ(VE). Negative edges are allowed. The queue optimization of the same relaxations is SPFA, whose worst case is no better.


What the problem actually asks

Dijkstra’s settle step assumes a path never gets cheaper by growing. A negative edge makes that false, so a vertex cannot be closed the first time it looks finished. The replacement is exhaustive: if a better path with at most k edges exists, it will be found by relaxing every edge onto the distances produced with at most k−1 edges.

A negative cycle reachable from the source makes distances undefined, not merely slow. The algorithm’s second result is a witness of that cycle, from the parent pointers of a vertex that improves on round |V|.


What the algorithm guarantees

After |V|−1 full passes over the edge list, dist[v] is the shortest-path distance from the source whenever no negative cycle is reachable, and infinity when v is unreachable. A further pass that updates any distance proves a negative cycle is reachable from the source. Walking parent pointers from the updated vertex, far enough to repeat a vertex, extracts a cycle. Time is Θ(VE) with an adjacency list of E edges. Extra memory is Θ(V).

A pass that performs no update can stop early. That optimization is safe and does not change the worst case. Difference constraints of the form x_i − x_j ≤ w_k are feasible exactly when a constraint graph has no negative cycle, so the same algorithm decides them.


The working steps

  1. Set dist[source] = 0 and every other distance to infinity.
  2. Repeat |V|−1 times: for each edge u→v of weight w, if dist[u] is finite and dist[u]+w < dist[v], set dist[v] = dist[u]+w and parent[v] = u.
  3. Scan every edge once more. An update on this extra pass means a negative cycle is reachable from the source.
  4. To extract the cycle, start from the vertex just updated and follow parents |V| times so the walk is inside the cycle, then follow parents until a vertex repeats.
  5. Optional: stop the main loop after a pass that changes nothing. The extra pass is then unnecessary only if that early stop happened.

The O(VE) bound is the general single-source bound with negative edges. Nonnegative edges do not make the loop faster than its early-exit special case, and Dijkstra is the algorithm that actually uses nonnegativity. A queue of only the vertices whose distances just changed is SPFA. It is the same relaxation order with a different schedule, and its worst case is still Θ(VE).


Approaches that look simpler and fail

Approach Why it fails when a negative cycle is treated as a long path
Stop after V−1 passes and always trust the distances A reachable negative cycle makes some distances able to fall forever. The extra pass is the test that separates a finite answer from that case.
Dijkstra on the same graph because the negatives look rare One reachable negative edge is enough to settle a vertex too early. Rarity is not the hypothesis.
Relax edges in a fixed order and skip the extra pass because a teaching example had no cycle The algorithm without the detection pass returns numbers that are not distances when a cycle exists.
Replace the loop with SPFA and quote a linear typical time as a bound SPFA’s worst case is Θ(VE), and there are families on which the queue version is slow. The bound to publish is the same worst case.

Nonnegative single-source distances belong to Dijkstra. All-pairs distances, including a negative-cycle test on the diagonal, belong to Floyd-Warshall. The queued variant of these relaxations is a scheduling choice, not a better guarantee.


A working example

The Bash script and the Python script relax every edge V−1 times. Edges are 0→1 weight 1, 1→2 weight 2, and a useless 0→2 weight 10. Both print distances 0 1 3.

# edges: u v w
eu=(0 1 0)
ev=(1 2 2)
ew=(1 2 10)
m=${#eu[@]}
n=3
dist=(0 1000000000 1000000000)
for ((round=0; round<n-1; round++)); do
  for ((e=0; e<m; e++)); do
    u=${eu[e]}; v=${ev[e]}; w=${ew[e]}
    if (( dist[u] + w < dist[v] )); then dist[v]=$((dist[u]+w)); fi
  done
done
echo "${dist[*]}"
n = 3
edges = [(0, 1, 1), (1, 2, 2), (0, 2, 10)]
dist = [0, 10**9, 10**9]
for _ in range(n - 1):
    for u, v, w in edges:
        if dist[u] + w < dist[v]:
            dist[v] = dist[u] + w
updated = False
for u, v, w in edges:
    if dist[u] + w < dist[v]:
        updated = True
assert dist == [0, 1, 3] and not updated
print(*dist)

Where it is used

RIP, the Routing Information Protocol, is a distributed Bellman-Ford: each router advertises distances and relaxes them. The same relaxation detects a negative cycle, which is the arbitrage check on a currency graph. OSPF uses Dijkstra because its link weights are nonnegative.


When you can drop this algorithm

Drop Bellman-Ford when every weight is known nonnegative. Dijkstra does less work. Keep it when a negative edge may exist, when a negative cycle must be reported, or when difference constraints must be checked.


Why this stays published

We publish this so a search for Bellman-Ford returns the |V|−1 relaxation rounds and the extra round that witnesses a negative cycle. Bellman-Ford computes single-source distances with negative edges in Θ(VE) time, and a successful relaxation on round |V| is a reachable negative cycle.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *