Sub topic 17
Application: Finding Currency Arbitrage with Logarithmic Weights
This chapter assumes you know how Bellman-Ford relaxes edges, how its extra pass detects a negative cycle, and how a predecessor array records paths. Here you apply all three to a real problem: finding a sequence of currency trades that ends with more money than it started with.
From Exchange Rates to a Graph
Model each currency as a vertex. Each exchange rate is a directed edge. An edge from USD to EUR with rate 0.9 means one dollar buys 0.9 euros.
A trading loop such as USD → EUR → GBP → USD multiplies the rates along it. If the product is greater than 1, you have found an arbitrage opportunity:
0.9 × 0.8 × 1.4 = 1.008
One dollar becomes 1.008 dollars. Shortest-path algorithms add weights. They do not multiply them, so the rates need to be transformed first.
The Logarithm Trick
Logarithms turn products into sums: log(a × b) = log a + log b. Set each edge weight to the negative log of its rate:
w(u, v) = −log(rate(u, v))
Then a cycle has a rate product greater than 1 exactly when its weight sum is negative:
| Condition on rates | Condition on weights |
|---|---|
| r1 × r2 × … × rk > 1 | Σ log ri > 0 |
| (same) | Σ −log ri < 0 |
An arbitrage opportunity is a negative cycle, and Bellman-Ford already detects those.
Detecting and Recovering the Cycle
You want any arbitrage loop, wherever it sits in the graph. Starting every distance at 0 has the same effect as adding a virtual source with zero-weight edges to every currency, so a negative cycle anywhere becomes reachable. After the final pass, walking the predecessor array back n times from a vertex that just relaxed puts you on the cycle. From there, you follow predecessors until you return to the same vertex.
import math
def find_arbitrage(currencies, rates, eps=1e-12):
n = len(currencies)
index = {c: i for i, c in enumerate(currencies)}
edges = [(index[a], index[b], -math.log(r)) for (a, b), r in rates.items()]
dist = [0.0] * n # acts like a virtual source linked to every vertex
pred = [None] * n
x = None
for _ in range(n): # n passes: the last one detects a cycle
x = None
for u, v, w in edges:
if dist[u] + w < dist[v] - eps:
dist[v] = dist[u] + w
pred[v] = u
x = v
if x is None:
return None # no arbitrage
for _ in range(n): # step back n times to land on the cycle
x = pred[x]
cycle = [x]
y = pred[x]
while y != x:
cycle.append(y)
y = pred[y]
cycle.append(x)
cycle.reverse() # predecessors point backward
return [currencies[i] for i in cycle]
currencies = ["USD", "EUR", "GBP"]
rates = {
("USD", "EUR"): 0.9,
("EUR", "GBP"): 0.8,
("GBP", "USD"): 1.4,
("USD", "GBP"): 0.7,
("GBP", "EUR"): 1.2,
}
print(find_arbitrage(currencies, rates)) # ['USD', 'EUR', 'GBP', 'USD']Common Errors
- Using
+log(rate)instead of−log(rate). That version finds loops that lose money. - Running from a single source. A cycle that the source cannot reach goes undetected. Start every distance at 0, or add a virtual source.
- Reporting the vertex that relaxed as if it were on the cycle. That vertex may only lead into the cycle. Walk back n steps first.
- Using Floyd-Warshall for one check. A negative diagonal entry also reveals arbitrage, but it costs O(V³) compared with Bellman-Ford's O(VE). Floyd-Warshall only pays off when you also need every pairwise best rate.
Exercise
Rates are A→B = 2.0, B→C = 0.5, C→A = 1.0. Is there arbitrage? Give the cycle's total weight under w = −log(rate).
Show answer
No. The product is 2.0 × 0.5 × 1.0 = 1.0, so the weight sum is −(log 2 + log 0.5 + log 1) = −(0.693 − 0.693 + 0) = 0. The cycle is not negative, so it does not count as arbitrage. The eps tolerance keeps rounding errors from reporting it as one.
Exercise
Why does initializing every distance to 0 find cycles that a single-source run starting at USD could miss?
Show answer
Setting every distance to 0 is equivalent to adding a new vertex with zero-weight edges to every currency and running Bellman-Ford from it. Every vertex is then reachable, so every negative cycle in the graph is reachable and will be detected.