Skip to the workspace
Pebellslearn by building
Shortest Paths at Scale  ›  Lesson

Bellman-Ford & Negative Weights

Bellman-Ford & Negative Weights 30 minRelax every leg, round after round, until nothing moves

dsa / Bellman-Ford & Negative Weights

Explore each scene at your own pace.

01 · Rounds, and what each one guarantees

Rounds, and what each one guarantees

Bellman-Ford & Negative Weights

Definition
The Bellman-Ford algorithm finds shortest paths from a source in a graph that may contain negative edge weights by relaxing every edge V - 1 times. If a further round still improves a cost, a reachable negative cycle exists and no shortest path is defined.
In plain words
Keep going over every road again and again, fixing any price you can improve. After enough rounds the prices stop changing. If they still drop after that, you have found a loop that pays you to drive round it forever.

In short: Bellman-Ford finds cheapest costs on a graph that may have both cycles and negative edges, by relaxing every leg, V − 1 times, in any order. After round k, every stop's cost is at most the cheapest route to it using at most k legs, so V − 1 rounds cover every simple route. One more round that still improves something proves a reachable loop makes money — there is no cheapest route to report, and the algorithm must say so.

A small table: 5 stops across the top, 3 rounds down the side, with round 3 unchanged and a highlighted insight that round 3 being unchanged means every cost is final

The night timetable had credits but no loops, and one sweep handled it. Daytime Meridian is messier. Roads run both ways, so the network is full of cycles, and partner depots rebate a van that carries their returns: on the books, those legs cost less than nothing. The tariff router has to find the cheapest route through that, and there is a new danger. If a few rebate legs form a loop whose total is negative, a van could drive round it forever, getting paid each lap. Then "the cheapest route" doesn't exist, and the router must say so rather than print a number.

Neither tool you have works here. Dijkstra settles stops too early when a rebate can undercut them later, and the DAG sweep needs an order that a cyclic network doesn't have. Bellman-Ford needs neither. It settles nothing and assumes no order; it just relaxes every leg, again and again, until nothing moves.

Rounds, and what each one guarantees

One round relaxes every leg in the network once, in any order. The whole algorithm is V − 1 rounds, where V is the number of stops. The reason is one invariant:

After round k, every stop's cost is at most the cheapest route to it that uses at most k legs.

Round 1 prices every one-leg route out of the start. Round 2 extends the best of those by one more leg, and so on. And if no loop makes money, a cheapest route never needs to visit a stop twice, because cutting out the loop between the two visits can't make it dearer. A route with no repeated stop has at most V − 1 legs, so after V − 1 rounds every cheapest route has been priced.

Watch it on a small piece of the daytime network. The legs are relaxed in a deliberately unhelpful order (listed in the note), so it takes more than one round. Each note records the costs at the end of a round:

watch it move · graph

Each round relaxes every leg once. Costs only ever fall, and a round that changes nothing means every cost is final.

Only the depot has a cost. Every round will try all six legs in the order listed. 5 nodes, 6 edges, directed. At depot. depot: 0.

leg order: quay>yard, yard>harbour, depot>quay, depot>mill, mill>quay, mill>harbour

1 / 6

That last step is the detection rule. After V − 1 rounds, costs can only still fall if some loop keeps making them fall. So one extra round is a proof: if anything improves in round V, there is a money-making loop the start can reach.

Scene 1 of 10
Project context and further resources
You're building a piece ofMeridian — the router
This piece — tariff_route(): Prices routes when some legs pay a rebate, and refuses when a loop of rebates would pay forever.
Scenario Some of Meridian's legs pay the van back: partner depots rebate a van that carries their returns. The tariff router finds the cheapest route when costs can go negative, and refuses to answer, rather than answer wrongly, when a loop of rebates would pay out forever.
tariff_route.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
tariff_route(legs, start, goal) → dictThe cheapest route under rebates as {cost, path}; cost None if unreachable, "loop" if no cheapest cost exists.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.