Floyd-Warshall All-Pairs Shortest Paths
An algorithm that finds the shortest path between every pair of nodes in a network simultaneously, by repeatedly asking whether routing through one more intermediate node ever beats the best route found so far.
The Floyd-Warshall algorithm computes the shortest distance between every pair of nodes in a weighted graph — for example, the cheapest currency-conversion path between every pair of currencies in an FX network, including indirect routes through intermediate currencies. Instead of running a single-source shortest-path search separately from each starting node, it fills in one distance matrix for all pairs at once.
The core idea is a triple loop: for every possible intermediate node , and every pair of nodes , check whether routing from to through is shorter than the best known direct route — that is, update . Doing this for every from 1 to , in order, guarantees that by the end, every pair's distance has considered every possible intermediate node as a potential shortcut. For a graph with currencies, this costs time — for currencies, about update checks, comfortably fast even on modest hardware.
Concretely, if converting USD to EUR directly implies a rate corresponding to a "distance" of 5 (in log-rate terms, so distances add along a path), but USD to JPY to EUR sums to a distance of 4.2, the algorithm updates the USD-EUR entry to reflect the cheaper indirect route the moment JPY is considered as intermediate node — this is exactly the check underlying cross-currency triangular-arbitrage detection.
Floyd-Warshall finds shortest paths between all pairs of nodes at once, in time, by iteratively checking whether routing through each candidate intermediate node improves on the best distance found so far.
Practice in interviews
Further reading
- Cormen et al., Introduction to Algorithms, ch. 25