Documentation

CslibFrontier.Algorithms.WordRAM.Hungarian

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:

  1. In-memory nonnegative/shifted cost matrices (coreCode, core_exec). Given an n × n matrix C : Fin n × Fin n → ℕ of unsigned costs bounded by W stored row-major at baseC, coreCode executes row reduction followed by n alternating-tree phases with per-column slacks minv and predecessor rows bestRow in O(n³) instructions at every word width satisfying Fits w (baseC + n * n + 8 * n + 2 * W + 2). By HgOuterBound and HgStepInv, 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.
  2. Signed integer assignment instances from State.init (inputCode, assignmentProgram, minCostProgram). An Instance specifies n and cost : Fin n × Fin n → ℤ, encoded as n followed by the n² row-major Signed.encode words. inputCode scans the input for a magnitude bound M ≤ inst.maxMag, shifts every cost by + M into [0, 2 * M] (preserving optimal matchings and dual slack steps by hungarian_add_const), runs coreCode, writes the length-prefixed optimal row-to-column assignment list inst.assignment for ExecutesList / Program.ComputesListInTime, and computes Signed.encode of the minimum matching cost for Executes / Program.ComputesInTime, all within hungarianTime inst.n (O(n³)) instructions.

Main definitions #

Main results #

References #