← all experiments

Back With More

Experiment № 109 · November 3, 2026

Bellman and Ford (1956–58) find the cheapest route from one place to every other by relaxing every edge, again and again: if going through u beats the best known way to v, remember that. Here the edges are currency trades. Take −log of each rate and multiplying becomes adding, so a loop of trades that comes back with more is a cycle of negative length, and an n-th pass that still improves anything proves one exists.

The market

Order the trades are checked in

pass— trades checked— improvements— verdict—

The whole algorithm

best[source] = 0, every other = ∞repeat n − 1 times: for each trade u → v at rate r: w = −log r // × becomes + if best[u] + w < best[v]: best[v] = best[u] + w, via[v] = uone more pass: does any trade still improve? yes → walk via[] back n steps: a loop that pays

The loop it found

Nothing yet.

Each node's number is the most of that currency one unit of the source can become. Violet arrows are the via pointers, the last trade on each best route. After n − 1 clean passes every honest route has been found, because a route that doesn't repeat a currency has at most n − 1 trades.

Click a currency to start from it · click an arrow to sweeten its rate by 1% (right-click sours it) · space plays · S steps · F finishes · R runs again · N new market · H hides panels

Experiment № 109 · The story

Back With More

Why I picked it

Dijkstra already turned up in this collection, hiding inside A*, and I'd always filed Bellman–Ford as the slower cousin you use when someone hands you negative edges. Then I met the currency version, where taking the logarithm of each exchange rate turns a chain of multiplications into a sum of lengths, and a loop of trades that pays becomes a road that's shorter than staying put. That one change of units turns the slow cousin into the only tool for the job, and I wanted to watch it happen.

What I enjoy about it

How dumb each step is. It doesn't search, prioritise or plan. It walks the whole list of trades and asks the same question of each one: would going through here be better? Then it does it all again. What I like most is the extra lap at the end. The algorithm knows that any honest route visits each currency at most once, so it's finished after n − 1 passes. If anything still improves on pass n, the only explanation is a loop that keeps paying, and following the via pointers back lands you on it. Turn the mispricing up and you can watch the numbers keep climbing, pass after pass, because nothing can stop them.

Something you might take away

Knowing when you should be finished is as useful as knowing how to finish. Bellman–Ford's last pass isn't more work towards the answer; it's a test of whether the world is the kind of place where an answer exists. And the log trick is worth keeping in your pocket: when things compound by multiplying, take logs and they add up like distances, and every tool built for distances works on them. Real arbitrage is mostly fees and latency, by the way. Push the fee slider up a little and most of the free money disappears.