Learn
Lesson · Graph Algorithms · Shortest paths

Negative Weights and Bellman–Ford

Relax every arc, V - 1 times over. After round i every label that has a shortest path of at most i arcs is correct, so V - 1 rounds suffice — a shortest path cannot repeat a vertex. If a round V still improves something then no shortest path exists, and the predecessor pointers hold the negative cycle that is the reason. Nothing here settles a vertex, which is exactly why it survives a negative arc.

one round = relax every arc once, in whatever order they are stored after round i: every label with a shortest path of at most i arcs is correct V - 1 rounds suffice, because a shortest path has at most V - 1 arcs still improving in round V -> a negative cycle, returned as the cycle the arc ORDER changes the number of rounds, never the answer
One sweep per round, and what a round guarantees

Core ideas

What this lesson establishes

The hard idea is the round invariant, because it is about the number of arcs on a path rather than about distance. The stopping rule and the certificate follow.

1

A round buys one more arc of path length

After one full sweep, every vertex whose shortest path uses one arc is correct; after two, every vertex whose shortest path uses at most two; and so on. A shortest path never repeats a vertex, because removing a repeated stretch cannot make it longer unless that stretch was negative — and if it was, no shortest path exists at all. So V - 1 arcs is the most any shortest path has, and V - 1 rounds is enough.

2

Nothing is settled, which is why negative arcs are survivable

Dijkstra’s schedule declares a vertex finished and stops relaxing its outgoing arcs. Here every arc is relaxed in every round, so a label that improves late still propagates. On the lab's opening graph the label at vertex 5 improves in the second round, from 2 to -2, and the nearest-first schedule on the same graph returns 2 — the right shape of answer and the wrong number.

3

The certificate is the cycle, not a flag

If a full round after the V - 1 still changes a label, some path uses V arcs and is therefore shorter than one that repeats no vertex, which means a cycle of negative total weight. Walking the predecessor pointers back V times from a vertex that moved lands inside that cycle, and following them round closes it. On 1>2 1, 2>3 -3, 3>4 1, 4>2 1 the lab returns the cycle 2, 3, 4, 2, which weighs -3 + 1 + 1 = -1.

The material

Rounds, the invariant, and the certificate

Why a round is the right unit, why V minus 1 of them suffice, what the arc order changes, and the negative cycle as evidence.

The algorithm

  1. Set the source's label to 0 and every other to infinity.
  2. Repeat V - 1 times: relax every arc, once, in whatever order they happen to be stored.
  3. Stop early if a full round changes nothing, because nothing later will change either.
  4. Perform one more round. If anything improves, report a negative cycle and recover it from the predecessor pointers.
Theorem · The round invariant

After i complete rounds, for every vertex v whose shortest path from the source uses at most i arcs, d[v] equals that shortest distance.

By induction on i. For i = 0 the only such vertex is the source, whose label is 0.

Assume it after i rounds and let v have a shortest path s, ..., u, v with i + 1 arcs. The prefix to u is itself a shortest path with i arcs, so d[u] is correct after round i, by hypothesis. Round i + 1 relaxes the arc from u to v at some point, and by then d[u] is at most its correct value, so afterwards d[v] is at most d[u] + w, the true shortest distance. The upper-bound invariant of the relaxation step says it is not less, so it is equal.

□

Note what the proof does not need: the arcs may be relaxed in any order within a round, and d[u] may have been improved earlier in the same round, which only helps. That is why the algorithm is indifferent to how the arcs are stored, and also why the number of rounds it actually needs is not.

Example · Five vertices, four negative arcs, and no negative cycle

The lab opens on 1>2 6, 1>3 7, 2>3 8, 2>4 5, 2>5 -4, 3>4 -3, 3>5 9, 4>2 -2, 5>1 2, 5>4 7 from vertex 1. The distances come out 0, 2, 7, 4, -2. The first round reaches 0, 2, 7, 4, 2 and the second improves only vertex 5, to -2; the third changes nothing and the run stops after 3 rounds and 30 relaxations. Floyd's all-pairs matrix, computed separately, gives the same row from vertex 1. The nearest-first schedule gives 0, 2, 7, 4, 2.

The route to vertex 2 is 1, 3, 4, 2, three arcs, at 7 - 3 - 2 = 2. Its label was already final after the first round even though its path has three arcs, because the arcs happened to be swept in an order that carried the improvement all the way along in one pass. The invariant promises correctness by round three; it does not forbid getting there sooner.

The arc order is worth a measurement of its own

Take a chain of five arcs each of weight 1, and type it backwards: 5>6 1, 4>5 1, 3>4 1, 2>3 1, 1>2 1. Each round pushes the frontier forward by exactly one arc, so the run takes 6 rounds and 20 relaxations, and the round each label became final is 0, 1, 2, 3, 4, 5. Type the same five arcs forwards and one sweep carries the improvement the whole way: 2 rounds, 10 relaxations, and every label final in round 1. Both give the distances 0, 1, 2, 3, 4, 5.

That is the cleanest example on this course of a count moving while an answer does not. Same vertices, same arcs, same weights, same distances; two thirds less work. And it is also why the bound is V - 1 rounds rather than something smaller: the backwards chain needs every one of them, so no smaller number is correct for all inputs.

The certificate

Theorem · Negative cycle detection

If a round after the V - 1 improves any label, then some cycle reachable from the source has negative total weight, and no shortest path exists to the vertices beyond it. If no such round improves anything, every label is a true shortest distance.

The forward direction is the round invariant read backwards: if no negative cycle is reachable then every shortest path has at most V - 1 arcs, so round V - 1 finished the job and round V finds nothing. If something does improve, the improving path uses V arcs, hence repeats a vertex, hence contains a cycle; and it could only be an improvement if that cycle is negative.

Example · A negative cycle, returned as the cycle

On 1>2 1, 2>3 -3, 3>4 1, 4>2 1 the lab runs all four rounds, something changes in the last, and 16 relaxations are performed. The labels it prints, 0, -3, -5, -4, are not distances and the panel does not present them as any: they are one lap further round the loop than the previous values. What it returns instead is the cycle 2, 3, 4, 2, whose weight is -1, so every lap subtracts one more.

A flag saying “negative cycle” would leave the reader with nowhere to go. The cycle itself is actionable: in a scheduling or currency model it names the loop that must be broken, and in a debugging session it names the arc whose weight was typed with the wrong sign.

Measured against proved. On the opening graph: 3 rounds, 30 relaxations, five distances. On the backwards chain: 6 rounds, 20 relaxations. The bound is O(V E), from V - 1 rounds of E relaxations each, and it is attained — the backwards chain needs every round. What the measurements cannot tell you is that no ordering forces more than V - 1, and that is what the invariant is for.

Interactive

Bellman–Ford, round by round

One round per arc on a shortest path, and a round that still improves something is a negative cycle

Rounds of relaxing every arc, and what a round buysafter round k, every shortest path of at most k arcs is exact
the arc that set this labela negative cyclethe round a label became final

Method

Running the rounds

One sweep is one unit of progress. Count the sweeps, not the improvements.

1

Sweep every arc every round, including the ones that did nothing

Skipping arcs whose tails did not change is a real optimisation and it changes what the round invariant says. Get the plain version right first: the correctness argument is about complete sweeps, and a partial sweep needs its own argument.

2

Stop early on a round that changes nothing

If a full sweep improves no label, no later sweep can either, since nothing has changed for it to build on. On the forwards chain this cuts six rounds to two. It is the one shortcut that costs nothing to justify.

3

Run the extra round deliberately

The V-th round is not part of computing the distances; it is the test for whether they mean anything. Code that stops at V - 1 returns numbers on a graph with a negative cycle and gives no sign that they are not distances.

4

Recover the cycle rather than reporting a boolean

From a vertex that improved in the last round, follow the predecessor pointers back V times to land inside the cycle, then follow them until you return to where you started. That is the evidence, and it is what makes the result actionable rather than merely negative.

Rounds, orders and certificates

Score 0 / 0

Three rounds on ten arcs, and the label that moves late

The arcs are 1>2 6, 1>3 7, 2>3 8, 2>4 5, 2>5 -4, 3>4 -3, 3>5 9, 4>2 -2, 5>1 2, 5>4 7, swept in that order, from vertex 1.

start d = 0 - - - - round 1, sweeping the ten arcs in order 1>2 6 d[2] = 6 1>3 7 d[3] = 7 2>4 5 d[4] = 11 2>5 -4 d[5] = 2 3>4 -3 d[4] = 4 (7 - 3 beats 11) 4>2 -2 d[2] = 2 (4 - 2 beats 6) after round 1 d = 0 2 7 4 2 round 2 2>5 -4 gives d[5] = 2 - 4 = -2 after round 2 d = 0 2 7 4 -2 round 3 nothing improves; stop 3 rounds, 30 relaxations round each label became final 0 1 1 1 2 arcs on its own shortest path 0 3 1 2 4 route to 2 1 - 3 - 4 - 2 7 - 3 - 2 = 2 route to 5 1 - 3 - 4 - 2 - 5 7 - 3 - 2 - 4 = -2 the all-pairs matrix, row from 1 0 2 7 4 -2 agrees nearest unfinished first on the same arcs 0 2 7 4 2 WRONG at 5

The round-final column is at most the arc-count column at every vertex, and strictly below it at three of them. That is the invariant doing exactly what it promises and no more: round i guarantees correctness for paths of at most i arcs, and a lucky sweep order can deliver a four-arc path in round two.

Vertex 5 is where the nearest-first schedule breaks. That schedule settles 5 at 2 when 2 is the smallest unfinished label, and never revisits it; the arc 2>5 of weight -4 then improves d[2]'s own route too late to help. Here every arc is relaxed in every round, so nothing is too late.

For a faded rehearsal, change 5>1 2 to 5>1 -2 and predict what the lab reports before running it. The supplied first move is this: the route to 5 costs -2, so going on to 1 now costs -4, and 1 already had a label of 0. Say whether that makes the loop through 1, 3, 4, 2, 5 negative, what the extra round will find, and what the panel should print instead of distances.

Common mistakes

1
Stopping at V - 1 rounds and reporting the labelsOn a graph with a reachable negative cycle those labels are one lap's worth of arithmetic, not distances, and nothing about them looks wrong. The extra round is the only thing standing between a correct answer and a confidently presented meaningless one, and it costs a single sweep.
2
Reading the rounds needed as a property of the graphIt is a property of the graph and the arc order together. The lab's two chain presets are the same graph with the arcs typed in two orders, and they take 6 rounds and 2. Anyone benchmarking this algorithm on one arc ordering and quoting the round count has measured their input format.
3
Treating the absence of settling as an inefficiency to be fixedRelaxing every arc in every round is what makes a late improvement propagate, and that is the whole reason this algorithm survives negative arcs where nearest-first does not. The optimisation of skipping arcs whose tails have not changed is real, but it is a change to the algorithm and it needs its own correctness argument.

Check your understanding

✓
Finish when you can run the rounds by hand, read the round-final column against the arc count, and recover a cycle.

You should be able to sweep the arcs in order recording the label table per round, state what round i guarantees, explain why V - 1 is both sufficient and tight, run the extra round as a test rather than as part of the computation, and walk the predecessor pointers back to a negative cycle.

Note: “Shortest and Longest Paths in a DAG” removes the cycles entirely. On a graph with no cycle at all there is an order in which one sweep suffices — the topological order — and because no cycle can be repeated for profit, the same pass with the sign flipped computes the LONGEST path, which is a problem nothing else on this course can solve.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.