A reduction is a translation from one problem to another such that solving the translated version solves the original. If you can translate your problem into a known-solved problem cheaply, you inherit that problem's solution and never have to write your own. This is the most leveraged move in engineering — and the most under-used one. An assignment problem ("match drivers to deliveries minimizing total miles") is a reduction away from a published algorithm with a Rust library that handles thousands of items in milliseconds. A constraint problem ("each shift must have at least two staff, no person works two shifts back-to-back") is a reduction away from a solver that the industry already built. When your team faces a new problem, the first question is not "what should we build" — it is "what known problem is this in disguise."
03
Reducibility — turning one problem into another
The most leveraged move in engineering is recognizing your new problem is an old problem in disguise.
- Driver-to-delivery assignment is the canonical assignment problem in disguise. The reduction is mechanical — build a cost matrix, hand it to a solver from
pathfinding, read the matching back. No new algorithm to write.use pathfinding::kuhn_munkres::kuhn_munkres_min; use pathfinding::matrix::Matrix; fn assign_drivers(miles: Vec<Vec<i32>>) -> Vec<usize> { let m = Matrix::from_rows(miles).expect("rectangular"); let (_total, assignment) = kuhn_munkres_min(&m); assignment // assignment[driver] = delivery } - The function is called
kuhn_munkres_min— a stack of two surnames you probably have not seen on an algorithm before. Harold Kuhn published the method in 1955; James Munkres refined the analysis in 1957. The rest of the world calls it the Hungarian algorithm because Kuhn rediscovered an idea from two Hungarian mathematicians of the 1930s (Dénes Kőnig and Jenő Egerváry) and credited them in the name. Three names, one algorithm — papers say "Hungarian," textbooks say "Kuhn-Munkres," the Rust crate uses the historical-receipt version.// One algorithm, four people, three names: // 1931 Kőnig (Hungary) — duality theorem // 1932 Egerváry (Hungary) — algorithmic refinement // 1955 Kuhn (US) — "The Hungarian Method" // 1957 Munkres (US) — polynomial-time analysis // // Papers call it the Hungarian algorithm. // Crates call it kuhn_munkres. use pathfinding::kuhn_munkres::kuhn_munkres_min; - A
Matrixis a rectangular grid of numbers — rows × columns, every row the same length. Here each row is a driver and each column is a delivery, and the cell at(driver, delivery)is the cost (miles) of that pairing.Matrix::from_rowstakes aVec<Vec<i32>>(a list of equal-length lists) and turns it into a real matrix with row/column accessors..expect("rectangular")makes the function panic loudly if the rows are not all the same length — a matrix has no room for a ragged input.// delivery 0 delivery 1 delivery 2 // ┌─────────────────────────────────────────┐ // driver 0│ 12 8 20 │ // driver 1│ 9 15 11 │ // driver 2│ 14 10 7 │ // └─────────────────────────────────────────┘ // // Cell (1, 2) = 11 means driver 1 → delivery 2 costs 11 miles. let miles = vec![ vec![12, 8, 20], vec![ 9, 15, 11], vec![14, 10, 7], ]; let m = Matrix::from_rows(miles).expect("rectangular"); -
//starts a line comment in Rust — everything after the two slashes, until the end of that line, is ignored by the compiler. It is a note to the reader, not code. The trailing note// assignment[driver] = deliverydescribes the shape of the return value:assignmentis aVec<usize>where the index is the driver number and the entry at that index is the delivery they were assigned to. So ifassignment[1] == 2, driver 1 is going on delivery 2.// "//" starts a line comment — everything after it on the same // line is ignored by the compiler. Notes to the reader, not code. let assignment = vec![0, 2, 1]; // index = driver, value = delivery // ^ ^ ^ // │ │ └── driver 2 → delivery 1 // │ └───── driver 1 → delivery 2 // └──────── driver 0 → delivery 0 // Look up which delivery driver 1 got: let d = assignment[1]; // d == 2