FX ARBITRAGE
A loop of rates that multiplies past 1 is a negative cycle
The pricing feed's GBP/NGN mid is stuck at ₦1,968 while USD/NGN (₦1,531.2516) and GBP/USD (1.27) keep updating; their cross puts GBP/NGN at ₦1,944.69. Owoleaf takes a 20 bp spread off every leg and checks every pair: rate(X→Y) × rate(Y→X) must be at most 1. What does the check report?
An exchange rate is an edge: one unit of X buys rate(X→Y) units of Y. A loop of conversions multiplies its rates. If the product is above 1, money appears from nowhere. A real market closes that gap in moments; a quote that stays open is a bug in your prices.
Take −ln(rate) as each edge’s weight. The product above 1 becomes a weight sum below 0, so arbitrage is exactly a negative cycle. Bellman-Ford relaxes every edge V−1 times; if an edge still relaxes after that, a negative cycle exists, and the predecessor pointers lead you onto it.
// Pair check: catches a two-leg round trip only.
for (x, y) in pairs:
if rate[x][y] * rate[y][x] > 1: withhold(x, y)
publish(rates)
// A stale GBP/NGN mid keeps GBP→NGN and NGN→GBP consistent with
// each other, so every pair passes. The free money is a three-leg loop.// Arbitrage = a loop whose rates multiply to > 1.
// w = -ln(rate) turns the product into a sum: prod > 1 <=> sum(w) < 0.
for (u, v): w[u][v] = -ln(rate[u][v])
dist[c] = 0 for every c; pred[c] = null // virtual source
repeat V - 1 times:
for (u, v) in edges:
if dist[u] + w[u][v] < dist[v] - eps: dist[v] = dist[u] + w[u][v]; pred[v] = u
for (u, v) in edges: // V-th pass
if dist[u] + w[u][v] < dist[v] - eps:
pred[v] = u; x = v
repeat V times: x = pred[x] // land on the cycle
return walk pred from x back to x // the loop, to report
// Run it on the rates you will QUOTE (spread included), before publishing.
// A cycle is a feed defect: stale or crossed rates. Withhold and alert. Step through FIX: BELLMAN-FORD and watch the dist column. Rates above 1 have negative weights, so the first round already lowers NGN, USD and EUR. Each later round lowers NGN by another 0.0059, the weight of one lap of the cycle, and after three rounds NGN→USD still relaxes. The trace names NGN → USD → GBP → NGN at 1.00593 a loop, GBP/NGN is withheld and the feed owner is paged. PAIR CHECK ONLY publishes the same feed, and Oyin turns ₦1,000,000 into ₦1,005,926.95 in one loop. FRESH FEED finds no cycle: the best loop loses the spread.
This feed has only the pair check. Move the stale GBP/NGN mid away from the fair cross of ₦1,944.69, and move the spread, until the app publishes quotes that let a customer loop money for a profit. Watch the pair check row: it never objects.
GOAL: the pair check passes and a published loop returns more than ₦1,000,000
The DIJKSTRA lesson used the same V-th pass to refuse a graph with a negative cycle. Here the cycle is the answer you want, so you keep the predecessors and read it off. Two details matter. Every currency starts at distance 0, which is a virtual source with a zero edge to each; a run from NGN alone misses a loop between two currencies NGN cannot reach. And compare with a small tolerance, because −ln of rates that agree to the last digit still sums to a few units of float noise, not to exactly 0.
Rewrite findArbitrage(rates) to return a loop whose rates multiply to more than 1, as a list of currencies that starts and ends with the same one (['NGN', 'USD', 'GBP', 'NGN']), or null when no loop pays. rates[x][y] is units of y for one x; a missing entry means no quote. The grader tries the lesson feed, a fresh feed, a four-leg loop, and a loop among currencies NGN cannot reach.
Where you run the check matters as much as how. Run it on the rates you will quote, spread and fees included, because those are the rates a customer can loop. A cycle on the feed’s mid rates that the spread happens to cover is still a stale price: alert on it, even if no customer can profit yet. When a loop does pay, every conversion in it is a real pair of ledger entries, so the loss is already in the books, as PAYMENT LEDGER would record it. Stopping the quote is the only fix that does not involve clawing money back.
FX quoting in wallets and remittance apps, multi-currency cards, treasury desks that build cross rates from a few base pairs, crypto exchanges with many pairs, and any pricing feed where one stale source can sit next to fresh ones.
“Given a table of exchange rates, is there an arbitrage?” Map rates to edges with weight −ln(rate), run Bellman-Ford from a virtual source, use the V-th pass to detect a negative cycle, and walk predecessors to print it: O(V·E). Then say what it means in production: a stale or crossed quote, checked on the spread-adjusted rates before publishing, with the pair withheld and the feed owner alerted.