E ExamMaster

Artificial Intelligence · AI Foundations

Negative Costs and Reopening

In AI because negative costs mark the exact boundary of greedy search: knowing why settle-on-pop fails, and what reopening or Bellman–Ford pay to fix it, separates using Dijkstra…

Uniform-cost search rests on one quiet assumption: extending a path can never make it cheaper. This lesson withdraws that assumption, watches the algorithm return a wrong answer without noticing, and surveys the repairs — reopening settled nodes, or switching to Bellman–Ford — and what each one costs.

  • Artificial Intelligence
  • Medium level
  • 5 concepts

1The assumption UCS lives on

Every guarantee in the cost-aware search lesson traces to one sentence: extending a path never lowers its cost. With every edge at least 0, the g of a path is non-decreasing as it grows, so work already popped can never be undercut by work still queued — that is the whole licence for settling a node on first pop.

State the licence carefully and you can see exactly how much it covers. It does not require costs to be large, or equal, or integer; zero-cost edges are fine. It requires only that no edge pays you back. The moment one edge is negative, 'cheapest so far' stops being a fact about the future and becomes a hope about it.

This is worth internalising as a proof habit: when an algorithm's correctness argument uses an assumption exactly once, the counterexamples all live exactly there. The next concept builds the smallest one.

Figure. Path cost g as a route grows, both routes starting at g = 0. The all-positive staircase never steps down, so anything settled earlier stays beaten. The staircase with one paying-back edge steps down mid-route — and everything settled before that step was settled on a false promise.

The licence and its limit

  1. The invariantWith edges ≥ 0, g never decreases along any path, so pop order equals cost order.
  2. Where it is spentThe cut argument uses it once: from the frontier onward, a rival path can only add.
  3. What withdraws itA single negative edge lets a longer path finish cheaper than a settled one.

2One negative edge breaks settle-on-pop

Here is the smallest graph that defeats uniform-cost search. S reaches G directly for 9; S reaches A for 10; and the edge from A to G costs −8 — a rebate, a recovered joule, a subsidised leg. The best route is through A: 10 + (−8) = 2. UCS never finds it.

Trace the pop order to see the failure precisely. S pops at 0 and puts G at 9 and A at 10 on the frontier. G is cheaper, so G pops next — and the search settles it and stops, returning 9, while the path worth 2 still sits one expansion away. Nothing malfunctioned: settle-on-pop did exactly what its proof licenses, and the licence was void because one edge lowered a growing total.

Notice what the counterexample does not need: no negative cycle, no elaborate graph — three nodes and one honest rebate. Whenever your cost model can produce even one negative edge, the algorithm choice has to change before the map does.

Figure. The counterexample that breaks uniform-cost search: UCS expands G at cost 9 and stops, never learning that the expensive-looking detour through A totals 10 + (-8) = 2. "Cheapest so far" is only safe when no later edge can pay you back - which is why planning maps keep costs non-negative.

The pop order that misses the optimum

On the counterexample graph (S→G 9, S→A 10, A→G −8), what does UCS return, and what is actually optimal?

  • direct S→G9
  • S→A10
  • S→A→G = 10 + (−8)2 — the true optimum
  • frontier after S pops: G at 9, A at 10 → G pops firstUCS settles G and returns 9

Pro tip. The failure is silent: the search terminates normally and returns a legal path. Nothing in the output distinguishes 9-because-optimal from 9-because-the-assumption-was-void — the check has to live in the cost model, not in the result.

Why does one negative edge break uniform-cost search even when every complete path still costs more than zero?
  1. Priority queues are unable to hold negative numbers
  2. A negative edge makes the graph cyclic
  3. A path can get cheaper as it is extended, so the first arrival at a node is no longer guaranteed to be the cheapest arrival
  4. It does not break it, so long as no cycle sums to a negative total

Settling a node on first arrival is only safe while extending a path can only add cost. One negative edge withdraws that promise, and a detour discovered later can undercut a node already finished with.

3Reopening: the repair and its price

The repair is mechanically small: stop trusting pops. Keep the best-known g per node, and when a cheaper path to an already-settled node appears, put the node back on the frontier and re-relax its successors — the settled set stops being permanent and becomes provisional. Algorithms that may revisit are called label-correcting, against the label-setting family (Dijkstra, UCS) that never looks back.

What is lost is the guarantee that made label-setting fast. Settle-on-pop meant each node was expanded at most once; with reopening, a node's g can be corrected many times, each correction re-queueing its descendants, and on adversarial graphs the number of reopenings grows exponentially. Correctness returns; the runtime bound does not.

The same trade reappears in informed search: A* with an inconsistent heuristic faces exactly this choice — forbid reopening and accept suboptimal answers, or allow it and give up the expansion bound. Recognising 'this is the reopening trade' inside a new algorithm is most of what this concept is for.

Reopening, mechanically

  1. Cheaper path appearsA relaxation finds a g below the best recorded for a node already popped.
  2. Re-queue the nodeRecord the better g and push the node back on the frontier — settled is now provisional.
  3. Correct downstreamWhen it re-pops, its successors are re-relaxed, and corrections cascade.

4Bellman–Ford: pay for generality

Bellman–Ford abandons cleverness for coverage: relax every edge, V − 1 times over. After pass k, every shortest path that uses at most k edges is correct; and no shortest path needs more than V − 1 edges unless a negative cycle exists, so the table is exact when the passes finish. No ordering, no queue, no assumption about signs.

The bill is the pass structure itself — O(V·E) against Dijkstra's roughly (V + E) \log V — and at sizes routine for planners the gap is not a constant factor; it is the difference between interactive and overnight.

The V-th pass is a free diagnostic: if any edge still relaxes, some cycle sums negative — and then 'shortest path' is not expensive, it is undefined, because another lap always improves. The classic real instance is currency arbitrage: price each conversion at −log(rate) and a negative cycle is exactly a sequence of trades that multiplies to more than 1.

Figure. A cycle whose edges sum to −1: every lap around it lowers the total, so no finite shortest path through it exists. Bellman–Ford flags exactly this — an edge that still relaxes on the V-th pass.

What the generality costs

A road network has V = 10⁴ nodes and E = 10⁵ edges. Compare the operation counts of the two algorithms.

  • Dijkstra: (V + E) log₂ V ≈ 1.1 × 10⁵ × 13.3≈ 1.5 × 10⁶ ops
  • Bellman–Ford: V × E = 10⁴ × 10⁵10⁹ ops
  • 10⁹ / 1.5 × 10⁶≈ 700× dearer

Pro tip. The engineering rule that falls out: run Dijkstra whenever the cost model certifies non-negativity, and keep Bellman–Ford as the audit — run it once to certify no negative cycles, then let the fast path fly.

5Negative costs in practice

Negative edges are rarer than they look and realer than beginners assume. The genuine sources: recovered energy (regenerative braking), rebates and subsidised legs in pricing problems, reward terms folded into a cost, and log-space products — a conversion rate above 1 prices below 0. The first response to a negative edge should always be to ask whether the model means it.

When it is meant, resist the folk repair: adding a big constant to every edge to make everything positive. The shift is not harmless, because routes absorb it in proportion to their edge count — a three-edge route gains three units of shift while a one-edge route gains one — and the ordering of totals can flip. The worked example on this concept flips one.

The principled version of the same instinct exists and is worth knowing by name: Johnson's reweighting adds a potential per node, pricing each edge as w + \phi(u) - \phi(v). The potentials telescope along any route, so every complete S-to-G total moves by the same \phi(S) - \phi(G): orderings survive, and negativity can be removed. That reweighting is the algebra behind A*'s heuristic in the informed-search lesson.

Figure. The same two routes before and after adding 1 to every edge: X wins at 3 against 4, then loses at 6 against 5. Three edges absorbed three units of shift; one edge absorbed one — the repair changed the answer.

The constant-shift repair changes the answer

Route X runs three edges costing −1, −1, 5; route Y is a single edge costing 4. Repair the negatives by adding +1 to every edge and compare.

  • X = −1 − 1 + 53 — the true optimum
  • Y4
  • after +1 per edge: X = 0 + 0 + 66
  • after +1 per edge: Y = 4 + 15 — the wrong route now wins

Pro tip. The shift penalises routes by their length, not their cost. Any honest repair must move every complete S-to-G total by the same amount — which is exactly what Johnson's per-node potentials arrange and a per-edge constant cannot.

Notes

  • A single negative edge breaks settle-on-pop: a later detour can undercut a node already finished with.
  • Reopening restores correctness at the price of the runtime guarantee; Bellman–Ford pays O(VE) up front instead and detects negative cycles.
  • Adding a constant to every edge is not a repair — long routes absorb more shift than short ones and the optimum can change.

Exam traps & shortcuts

  • Never repair negative edges by adding a constant to every edge — longer routes absorb more of the shift than short ones and the optimum can change.
  • One negative edge already breaks settle-on-pop; a negative cycle makes 'shortest' undefined altogether.
  • A search that terminates normally can still be wrong — the non-negativity check belongs in the cost model, not in the output.

Recap

This lesson in brief:

The assumption
Settle-on-pop is licensed by one fact — extending a path never lowers its cost — and the licence is spent exactly once in the proof.
One negative edge
Three nodes and one rebate defeat UCS: it returns 9 and terminates normally while the true optimum of 2 sits one expansion away.
Reopening
Re-queueing settled nodes restores correctness and gives up the each-node-expanded-once bound — the label-setting versus label-correcting trade.
Bellman–Ford
Relax every edge V − 1 times: sign-agnostic, negative-cycle-detecting, and roughly 700× dearer on a routine road network.
No constant-shift repair
Adding a constant per edge moves long routes more than short ones; per-node potentials (Johnson) move every total equally instead.

Practise Negative Costs and Reopening

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • 1 quick check with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.