The Hungarian Method for the Assignment Problem #
A pure executable implementation of the Hungarian method for the n × n assignment problem
with integer costs c : Fin n × Fin n → ℤ ([Kuh55]; [Mun57]; [CLRS22], §25.3),
growing one alternating tree per row and maintaining a column slack table (the reduced-cost
technique of [EK72]). It returns a minimum-cost perfect matching together with integral
feasible potentials whose total equals the cost, proving Egerváry's min–max theorem for the
assignment problem ([Sch03], Ch. 17). The O(n³) Word-RAM implementation and running-time
bound live in CslibFrontier.Algorithms.WordRAM.Hungarian.
Algorithm and source mapping #
Costs are minimized. Row potentials y, column potentials z and the matching (a column-indexed
mate table mate, as in CslibFrontier.Algorithms.Lean.MaxFlow.HopcroftKarp) are kept with every
reduced cost hgRed c y z u v = c (u, v) - y u - z v nonnegative and every matched edge tight
(reduced cost 0).
- Row reduction:
y u = min_v c (u, v)andz = 0give feasible potentials. - One phase per row
r(hgPhase): the alternating tree is grown from the free rowr.minv jis the least reduced cost from a tree row to the non-tree columnj, andpath jis the alternating path fromrtojthat attains it. Each round ofhgLooppicks the non-tree columnj₁of least slackδ, then raises the tree rows and lowers the tree columns byδ(hgDual), which keeps feasibility and makes the last edge toj₁tight. Ifj₁is free, the matching is augmented alongpath j₁. This is an augmenting path in the sense ofCslibFrontier.Algorithms.Lean.MaxFlow.AugmentingPath, sorbecomes matched. Otherwisej₁and its mate join the tree and the slacks are relaxed through the new row (hgExtend). A round that does not augment adds a matched column to the tree, so the phase augments withinnrounds. - Certificate: after
nphases the matching is perfect and tight for feasible potentials, so weak duality and complementary slackness (CslibFrontier.Algorithms.Lean.MaxFlow.WeightedMatching) show that it has minimum cost and that its cost is∑ y + ∑ z.
Scope #
Square instances of the complete bipartite graph with integer costs. Rectangular instances and forbidden edges have to be encoded by padding and by large costs. Maximum-weight assignment is obtained by negating costs.
Main definitions #
hgRowMin,hgRed: row minima and reduced costs;HgState: the state of a phase.hgArgmin,hgDual,hgRelax,hgExtend,hgApplyEdges,hgInit,hgLoop,hgPhase,hgRows: the steps of one phase and the loop over rows.HungarianOutput,hungarian: the full algorithm.HgOuterBound,HgStepInv: bounded potential, slack, and predecessor-tree invariants on nonnegative costs0 ≤ c (u, v) ≤ W, used by the Word-RAM implementation.
Main results #
hungarian_correct: the output matching is a minimum-cost perfect matching (IsMinCostPerfectMatching), the output potentials are feasible (IsFeasiblePotential), and the matching cost equals the total potential.hungarian_add_const: adding a uniform constantSto all costs shifts row potentials bySand leaves column potentials and the returned matching unchanged.exists_isMinCostPerfectMatching_potential_eq(Egerváry's theorem, integral form), andisMinCostPerfectMatching_iff_exists_potential(min–max form): a perfect matching has minimum cost iff some integral feasible potentials have total equal to its cost.exists_perm_assignment: the permutation formmin_σ ∑ u, c (u, σ u) = max (∑ y + ∑ z).
References #
- H. W. Kuhn, The Hungarian Method for the Assignment Problem
- J. Munkres, Algorithms for the Assignment and Transportation Problems
- J. Edmonds and R. M. Karp, Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, 4th ed.
- A. Schrijver, Combinatorial Optimization: Polyhedra and Efficiency
Algorithm #
State of one phase of the Hungarian method (growing an alternating tree from one root).
Row potentials.
Column potentials.
Matched row of each column.
Columns already in the tree.
Rows in the tree.
Slack of each column not in the tree: its minimum reduced cost from a tree row.
For each column, the tree path reaching it, as
(row, column)edges, last edge first.
The tree-growing loop of one phase: pick the unused column j₁ of minimum slack δ, adjust
the potentials by δ, then augment along the tree path of j₁ if it is free, or add j₁ and its
mate to the tree and relax.
The Hungarian method for the n × n assignment problem with integer costs c: row
reduction, then one alternating-tree phase per row, then extraction of the matching.
Specification of the phase steps #
Field lemmas #
Invariants #
q is the stored tree path of column j (last edge first): it starts at the root r, ends
at j, alternates through matched edges, has tight tail edges inside the tree, and its last edge
has reduced cost 0 if j is in the tree and minv j otherwise.
Stored tree paths are nonempty.
- head (e : Fin n × Fin n) : e ∈ q.head? → e.2 = j ∧ hgRed c y z e.1 e.2 = if vget used j = true then 0 else vget minv j
The head edge ends at
jand attains0(ifjis in the tree) orminv j(otherwise). The last edge (first in root-to-
jorder) starts at the rootr.Consecutive edges alternate through matched edges of
mate.Left endpoints along
qare distinct.Right endpoints along
qare distinct.Tail edges are tight and end at columns already in the tree.
Every left endpoint along
qbelongs to the tree.
Invariant of the tree-growing loop of the phase rooted at r.
The dual adjustment #
Augmenting along a tree path #
Growing the tree #
Counting tree columns #
The loop, the phase and the rows #
Bounded potential and predecessor-array invariants #
Between-phase invariant on nonnegative costs 0 ≤ c (u, v) ≤ W: potentials satisfy
0 ≤ y i ≤ W and -W ≤ z j ≤ 0, with z j = 0 on every unmatched column.
Row potentials are nonnegative and bounded by
W.Column potentials are nonpositive and bounded below by
-W.Unmatched columns have zero potential.
Within-phase invariant on nonnegative costs 0 ≤ c (u, v) ≤ W with predecessor row table
bestRow : Fin n → Fin n encoding s.path together with s.mate.
Row potentials lie in
[0, W].Column potentials lie in
[-W, 0].Unmatched columns have zero potential.
Column slacks lie in
[0, 2 * W].bestRow jis the left endpoint of the head edge ofs.path[j].When
bestRow j = r,s.path[j]is the single edge[(r, j)].- tail_mate (j j' : Fin n) : vget s.mate j' = some (bestRow j) → (vget s.path j).tail = vget s.path j'
When
bestRow jis matched toj', the tail ofs.path[j]iss.path[j'].
Correctness and Egerváry's theorem #
Correctness of the Hungarian method. The output is a minimum-cost perfect matching of the
complete bipartite graph on Fin n × Fin n, the output potentials are feasible, and the cost of
the matching equals the total potential (Egerváry's certificate of optimality).
Uniform shift invariance #
Egerváry's theorem for the integral assignment problem: there is a perfect matching of the
complete bipartite graph on Fin n × Fin n and integral feasible potentials whose total equals
its cost; in particular the matching has minimum cost.
Min–max form of Egerváry's theorem: a perfect matching M has minimum cost iff some
integral feasible potentials have total equal to its cost, and then every feasible potential has
total at most matchingCost c M.