Four problems where a cost can be negative or a limit counts edges. The move: run rounds, not a heap, read each round from a copy when edges are limited, and treat one more drop as a negative cycle.
You work on a payments app, an ad platform and a currency desk. The four problems on this street ask for the cheapest chain of transfers or swaps. In each one the cost can be negative, the number of edges is limited, or a gain compounds.
Dijkstra's method cannot call a cost final in these cases. So the search runs in rounds, and in each round it tries every edge once.
| Problem | A round does | The answer reads | The rounds stop |
|---|---|---|---|
| Cheapest route with negative fees | relax every transfer | the cost of the target | when nothing changes, after at most n - 1 rounds |
| Shortest path with at most k edges | relax every move from a copy of the last round | the target after round k | after exactly k rounds |
| Detect a negative cycle | relax every move | whether round n still lowers a cost | after round n, then spread the marks |
| Arbitrage in an exchange table | multiply rates and keep the largest | whether a start ends above 1 | at the first round where one does |
Exercise · Predict the output: One round, written in place. Three transfers of fee 1 form a chain from hub 0 to hub 3. This code runs one round and writes the new costs into the same list. What does it print for hub 3?
edges = [[0, 1, 1], [1, 2, 1], [2, 3, 1]]
best = [0, 99, 99, 99]
for a, b, fee in edges:
if best[a] < 99 and best[a] + fee < best[b]:
best[b] = best[a] + fee
print(best[3])Three questions pick the algorithm, and the statement answers them.
| Problem | Negative cost? | Edge limit? | Negative cycle? |
|---|---|---|---|
| Cheapest route with negative fees | yes | no | promised not to exist |
| Shortest path with at most k edges | yes | exactly k | harmless, rounds stop at k |
| Detect a negative cycle | yes | no | the whole question |
| Arbitrage in an exchange table | rates, as products | the loop length | a gaining loop is one |
After round k, every cost is at most the cost of the cheapest route with k edges or fewer. If no negative cycle is reachable, nothing changes after round n - 1. A drop in round n proves that one is.
A route with n vertices has at most n - 1 edges, so n - 1 rounds are enough when no loop pays. The cost of the loop of rounds is n - 1 passes over m edges. With 1,000 vertices and 5,000 edges that is 4,995,000 relaxations.
Each round reads a copy of the last one and adds exactly one edge to every route.
Exercise · Put it in order: A round reads the last round. Arrange the lines into a function that returns the cheapest fee to every hub using at most `rounds` transfers. Each round must read a copy of the last one. A hub that is not reached keeps 1000000000.
def fees_after_rounds(n, edges, source, rounds):
best = [10**9] * n
best[source] = 0
for _ in range(rounds):
prev = best[:]
for a, b, fee in edges:
if prev[a] < 10**9 and prev[a] + fee < best[b]:
best[b] = prev[a] + fee
return best
prev = bestThe Academy lesson Dijkstra's shortest paths builds both algorithms and shows why a negative cost breaks the heap. This street does not teach that again. It changes four things about Bellman-Ford.
A heap that calls a vertex final is wrong. Take edges 0-1 cost 2, 0-2 cost 3, 2-1 cost -4 and 1-3 cost 1. Dijkstra's method calls vertex 1 final at cost 2 and returns 3 for vertex 3. The rounds find the route through vertex 2 and return 0.
A limit on edges needs the copy. On the chain of three transfers, writing in place reaches hub 3 in one round at cost 3. Reading a copy leaves hub 3 unreached after one round, which is the true answer for one edge.
A negative cycle has no answer, and the mark spreads. Take edges 0-1 cost 1, 1-2 cost -2, 2-1 cost 1 and 2-3 cost 1. After 5 rounds the costs are 0, -1, -2 and -1.
Round 6 still lowers hub 2 to -3, so the loop between hubs 1 and 2 pays. Hub 3 is also unbounded, because it is reached from hub 2. The mark must spread to it.
Products replace sums. Three swaps with rates 1.2, 1.2 and 0.8 multiply to 1.152, a gain. With a cost of minus the logarithm of each rate, the three costs add to -0.1415, a negative cycle. The solution keeps exact fractions and never takes a logarithm.
A heap that calls a vertex final returns 3 here. The rounds find the route through the negative edge, and return 0.
Exercise · Break it: A loop the source cannot reach. This reports whether a negative cycle is reachable from the source. It starts every hub at cost 0 instead of only the source. Find an input where it gives the wrong answer.
def reachable_negative_cycle(n, edges, source):
best = [0] * n
for _ in range(n):
changed = False
for a, b, fee in edges:
if best[a] + fee < best[b]:
best[b] = best[a] + fee
changed = True
if not changed:
return False
return TrueSolve them in this order. The first uses plain rounds, the second a limit on edges, the third a negative cycle, and the last compounds rates.
Before you code each problem, write three lines: whether a cost can be negative, whether the edges are limited, and whether a negative cycle can be reachable. The answers pick the rounds, the copy and the extra round.
Give each problem about 25 minutes. Spend the first five on the three lines, and say them aloud as you would to an interviewer. Then write what the code returns when the target is unreachable.
Three edge cases return on every rung. An unreached cost must never take part in a sum, so guard it. A vertex may reach itself by a loop of one edge, and a round that changes nothing lets you stop.
When a solution fails, check the copy before the loop. Most wrong answers on this street come from a round that reads its own writes, or from an unreached cost that looks cheap after a negative fee.
Checkpoint · Bellman-Ford and negative edges
Q1/5 Edges 0-1 cost 2, 0-2 cost 3, 2-1 cost -4 and 1-3 cost 1. What does a heap that calls a vertex final return for vertex 3, and what is the right cost?
Answer: B
The heap calls vertex 1 final at cost 2, before it sees the route through vertex 2 at cost -1. Rounds keep relaxing and find the cost 0.
Q2/5 What does round k of Bellman-Ford guarantee?
Answer: B
A round adds one edge to every route. After k rounds, every route of k edges or fewer has been tried.
Q3/5 The route may use at most k transfers. Why must each round read a copy of the last round?
Answer: B
On a chain of three transfers, writing in place reaches the end in one round. A copy adds exactly one transfer per round.
Q4/5 After n - 1 rounds, one more round still lowers a cost. What does that prove?
Answer: B
A route without a loop has at most n - 1 edges, so no cost can drop after n - 1 rounds. A drop in round n needs a loop that pays.
Q5/5 Three swaps have rates 1.2, 1.2 and 0.8. Is the loop a gain?
Answer: B
Rates multiply, and 1.2 times 1.2 times 0.8 is 1.152, above 1. Adding the rates, or looking at one rate, answers a different question.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.