Rohim’s one-way leg costs, some negative: every round checks every leg, a table of costs per round, stopping when nothing changes. What does round V find, and…
Why it matters
Negative weights show up for real: currency rates (an arbitrage is a negative cycle), costs that can turn into income, routing protocols like RIP (distance vector). Dijkstra gives a wrong answer there with no warning; Bellman-Ford gives the right one, or tells you there is none.
The key idea
Round k settles routes of k legs
Common mistakes
Using Dijkstra with negative weights Dijkstra settles a village the moment it leaves the heap; a negative leg found later lowers a settled village, and the villages after it never hear of it. For negative weights use Bellman-Ford (or Johnson for all pairs).
Not checking for a negative cycle After V − 1 rounds, check every leg once more. If one still lowers a cost, a negative cycle is reachable from the start: walk back V times through asal to get inside it, then go round it to name it.
Always running all V − 1 rounds Keep berubah: a round that changes nothing means every cost is already the cheapest, so it may stop. The rounds needed depend on the legs’ order; V − 1 is only the worst case.
Try it in this playground
Until the route is read Run until a round changes nothing and the route is read through asal.
Dijkstra goes wrong Run to the end on a map where Dijkstra gives a different cost for the market.
Catch a negative cycle Make a loop that pays (T→S at 2, say) and watch the 7th round name it.
Change the ledger’s order Reverse the ledger or order it along the routes, run to the end, and compare the rounds.