The Hungarian method in O(n³) time on the word RAM #
This file implements and proves the O(n³) slack-array Kuhn–Munkres (Hungarian) algorithm on the
standard-basis word RAM (Basis.standard), connecting its output directly to the pure
specification Cslib.Algorithms.Lean.MaxFlow.hungarian and Egerváry's optimality theorem
(IsMinCostPerfectMatching).
Two layers are provided:
- In-memory nonnegative/shifted cost matrices (
coreCode,core_exec). Given ann × nmatrixC : Fin n × Fin n → ℕof unsigned costs bounded byWstored row-major atbaseC,coreCodeexecutes row reduction followed bynalternating-tree phases with per-column slacksminvand predecessor rowsbestRowinO(n³)instructions at every word width satisfyingFits w (baseC + n * n + 8 * n + 2 * W + 2). ByHgOuterBoundandHgStepInv, all row potentials remain in[0, W], negated column potentials remain in[0, W], and reduced costs and slacks remain in[0, 2 * W], so unsigned word arithmetic never underflows or overflows. - Signed integer assignment instances from
State.init(inputCode,assignmentProgram,minCostProgram). AnInstancespecifiesnandcost : Fin n × Fin n → ℤ, encoded asnfollowed by then²row-majorSigned.encodewords.inputCodescans the input for a magnitude boundM ≤ inst.maxMag, shifts every cost by+ Minto[0, 2 * M](preserving optimal matchings and dual slack steps byhungarian_add_const), runscoreCode, writes the length-prefixed optimal row-to-column assignment listinst.assignmentforExecutesList/Program.ComputesListInTime, and computesSigned.encodeof the minimum matching cost forExecutes/Program.ComputesInTime, all withinhungarianTime inst.n(O(n³)) instructions.
Main definitions #
Cslib.Algorithms.WordRAM.Hungarian.Instance: ann × ninteger assignment instance.Cslib.Algorithms.WordRAM.Hungarian.Instance.encode,Cslib.Algorithms.WordRAM.Hungarian.Instance.maxMag,Cslib.Algorithms.WordRAM.Hungarian.Instance.wordBound,Cslib.Algorithms.WordRAM.Hungarian.Instance.assignedCol,Cslib.Algorithms.WordRAM.Hungarian.Instance.assignment,Cslib.Algorithms.WordRAM.Hungarian.Instance.encodedMinCost: input encoding, word-width envelope, and canonical optimal assignment and minimum-cost outputs.Cslib.Algorithms.WordRAM.Hungarian.BaseRegs,Cslib.Algorithms.WordRAM.Hungarian.CostMem: register frame and row-major cost-matrix assertion for in-memory calls tocoreCode.Cslib.Algorithms.WordRAM.Hungarian.coreCode,Cslib.Algorithms.WordRAM.Hungarian.inputCode,Cslib.Algorithms.WordRAM.Hungarian.assignmentProgram,Cslib.Algorithms.WordRAM.Hungarian.minCostProgram: structured and flat word-RAM programs.Cslib.Algorithms.WordRAM.Hungarian.coreTime,Cslib.Algorithms.WordRAM.Hungarian.hungarianTime: explicitO(n³)instruction bounds.
Main results #
Cslib.Algorithms.WordRAM.Hungarian.Instance.isMinCostPerfectMatching_assignment: the pairs(u, inst.assignedCol u)form(hungarian inst.cost).matching, hence a minimum-cost perfect matching.Cslib.Algorithms.WordRAM.Hungarian.core_exec:O(n³)word-RAM execution ofcoreCodeon an in-memoryn × nunsigned cost matrixCbounded byW.Cslib.Algorithms.WordRAM.Hungarian.executesList_assignment,Cslib.Algorithms.WordRAM.Hungarian.computesListInTime_assignment:assignmentProgramcomputes the optimal assignment list inhungarianTime n = O(n³)instructions.Cslib.Algorithms.WordRAM.Hungarian.executes_minCost,Cslib.Algorithms.WordRAM.Hungarian.computesInTime_minCost:minCostProgramcomputes the signed-encoded minimum matching cost inhungarianTime n = O(n³)instructions.