LAB: TRADE COMPRESSION
Banks accumulate webs of offsetting trades. Compression removes as much gross notional as possible without changing anyone's net position, which frees up capital and cuts counterparty risk. This is a toy version of the problem behind the $4T on my home page. Generate a market, then let the solver run. Arrows point from the payer to the receiver, and thickness is notional.
THE MATH
Let trade e = (u→v) have notional w_e, and let y_e be the amount compressed.
maximize Σ_e y_e
subject to Σ_(e into i) y_e − Σ_(e out of i) y_e = 0 for every bank i
0 ≤ y_e ≤ w_e for every trade e
Removing a circulation keeps every net position fixed, so this is a min-cost circulation problem (cost −1 per unit compressed). The lab solves it exactly with Klein's algorithm: find a negative cycle in the residual graph with Bellman–Ford, push flow around it, repeat until none is left. Backward arcs (shown as ⇢ in the log) let a later cycle undo part of an earlier, greedier one. That's why the result is the true optimum and not just a good guess.
Lower bound. If new trades could be created as well as shrunk, the best possible gross would be ½·Σ|neti|. Real compression adds risk limits, counterparty preferences and maturities, and needs commercial MIP solvers. The core idea is the same.
Verified: the solver's answers matched SciPy's HiGHS LP solver on 300 random markets. Source: js/solver.js, about 100 lines.