Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.Hungarian

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).

  1. Row reduction: y u = min_v c (u, v) and z = 0 give feasible potentials.
  2. One phase per row r (hgPhase): the alternating tree is grown from the free row r. minv j is the least reduced cost from a tree row to the non-tree column j, and path j is the alternating path from r to j that attains it. Each round of hgLoop picks the non-tree column j₁ of least slack δ, then raises the tree rows and lowers the tree columns by δ (hgDual), which keeps feasibility and makes the last edge to j₁ tight. If j₁ is free, the matching is augmented along path j₁. This is an augmenting path in the sense of CslibFrontier.Algorithms.Lean.MaxFlow.AugmentingPath, so r becomes matched. Otherwise j₁ 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 within n rounds.
  3. Certificate: after n phases 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 #

Main results #

References #

Algorithm #

def Cslib.Algorithms.Lean.MaxFlow.hgRowMin {n : ℕ} (c : Fin n × Fin n → ℤ) (u : Fin n) :

Minimum of row u of c, scanning all n columns.

def Cslib.Algorithms.Lean.MaxFlow.hgRed {n : ℕ} (c : Fin n × Fin n → ℤ) (y z : Vector ℤ n) (u v : Fin n) :

Reduced cost c (u, v) - y u - z v.

State of one phase of the Hungarian method (growing an alternating tree from one root).

  • y : Vector ℤ n

    Row potentials.

  • z : Vector ℤ n

    Column potentials.

  • mate : Vector (Option (Fin n)) n

    Matched row of each column.

  • used : Vector Bool n

    Columns already in the tree.

  • tree : Vector Bool n

    Rows in the tree.

  • minv : Vector ℤ n

    Slack of each column not in the tree: its minimum reduced cost from a tree row.

  • path : Vector (List (Fin n × Fin n)) n

    For each column, the tree path reaching it, as (row, column) edges, last edge first.

One argmin scan step: keep the unused column of smaller slack.

Unused column of minimum slack.

Dual adjustment by δ: raise tree rows, lower tree columns, reduce the slack of the other columns.

def Cslib.Algorithms.Lean.MaxFlow.hgRelax {n : ℕ} (s : HgState n) (c : Fin n × Fin n → ℤ) (i j₁ : Fin n) :

Relax the slacks of the unused columns through the new tree row i, the mate of the column j₁ just added to the tree.

def Cslib.Algorithms.Lean.MaxFlow.hgInit {n : ℕ} (c : Fin n × Fin n → ℤ) (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r : Fin n) :

Initial state of the phase rooted at the free row r.

def Cslib.Algorithms.Lean.MaxFlow.hgExtend {n : ℕ} (c : Fin n × Fin n → ℤ) (s : HgState n) (j₁ i₀ : Fin n) :

Add the column j₁ and its mate i₀ to the tree, then relax through i₀.

def Cslib.Algorithms.Lean.MaxFlow.hgApplyEdges {n : ℕ} (mate : Vector (Option (Fin n)) n) (es : List (Fin n × Fin n)) :
Vector (Option (Fin n)) n

Rematch the right end of every edge of es to its left end.

def Cslib.Algorithms.Lean.MaxFlow.hgLoop {n : ℕ} (c : Fin n × Fin n → ℤ) :
ℕ → HgState n → HgState n

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.

def Cslib.Algorithms.Lean.MaxFlow.hgPhase {n : ℕ} (c : Fin n × Fin n → ℤ) (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r : Fin n) :

One phase: augment the matching by one edge from the free row r.

def Cslib.Algorithms.Lean.MaxFlow.hgRows {n : ℕ} (c : Fin n × Fin n → ℤ) :
List (Fin n) → HgState n → HgState n

Run one phase per row of the list.

Phase state before the first phase: potentials y, z and matching mate, with an empty tree.

Output of the Hungarian method: a perfect matching and potentials certifying its optimality.

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 #

theorem Cslib.Algorithms.Lean.MaxFlow.hgArgmin_some {n : ℕ} {s : HgState n} {k : Fin n} (h : hgArgmin s = some k) :
vget s.used k = false ∧ ∀ (j : Fin n), vget s.used j = false → vget s.minv k ≤ vget s.minv j

Field lemmas #

@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_mate {n : ℕ} (s : HgState n) (δ : ℤ) :
(hgDual s δ).mate = s.mate
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_used {n : ℕ} (s : HgState n) (δ : ℤ) :
(hgDual s δ).used = s.used
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_tree {n : ℕ} (s : HgState n) (δ : ℤ) :
(hgDual s δ).tree = s.tree
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_path {n : ℕ} (s : HgState n) (δ : ℤ) :
(hgDual s δ).path = s.path
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_y {n : ℕ} (s : HgState n) (δ : ℤ) (i : Fin n) :
vget (hgDual s δ).y i = if vget s.tree i = true then vget s.y i + δ else vget s.y i
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_z {n : ℕ} (s : HgState n) (δ : ℤ) (j : Fin n) :
vget (hgDual s δ).z j = if vget s.used j = true then vget s.z j - δ else vget s.z j
theorem Cslib.Algorithms.Lean.MaxFlow.hgDual_minv {n : ℕ} (s : HgState n) (δ : ℤ) (j : Fin n) :
vget (hgDual s δ).minv j = if vget s.used j = true then vget s.minv j else vget s.minv j - δ
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_y {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ : Fin n) :
(hgRelax s c i j₁).y = s.y
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_z {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ : Fin n) :
(hgRelax s c i j₁).z = s.z
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_mate {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ : Fin n) :
(hgRelax s c i j₁).mate = s.mate
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_used {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ : Fin n) :
(hgRelax s c i j₁).used = s.used
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_tree {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ : Fin n) :
(hgRelax s c i j₁).tree = s.tree
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_minv {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ j : Fin n) :
vget (hgRelax s c i j₁).minv j = if vget s.used j = true then vget s.minv j else min (vget s.minv j) (hgRed c s.y s.z i j)
theorem Cslib.Algorithms.Lean.MaxFlow.hgRelax_path {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (i j₁ j : Fin n) :
vget (hgRelax s c i j₁).path j = if vget s.used j = false ∧ hgRed c s.y s.z i j < vget s.minv j then (i, j) :: vget s.path j₁ else vget s.path j
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_y {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r : Fin n) :
(hgInit c y z mate r).y = y
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_z {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r : Fin n) :
(hgInit c y z mate r).z = z
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_mate {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r : Fin n) :
(hgInit c y z mate r).mate = mate
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_used {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r j : Fin n) :
vget (hgInit c y z mate r).used j = false
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_tree {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r i : Fin n) :
vget (hgInit c y z mate r).tree i = decide (i = r)
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_minv {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r j : Fin n) :
vget (hgInit c y z mate r).minv j = hgRed c y z r j
theorem Cslib.Algorithms.Lean.MaxFlow.hgInit_path {n : ℕ} {c : Fin n × Fin n → ℤ} (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (r j : Fin n) :
vget (hgInit c y z mate r).path j = [(r, j)]

Invariants #

structure Cslib.Algorithms.Lean.MaxFlow.HgPath {n : ℕ} (c : Fin n × Fin n → ℤ) (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (used tree : Vector Bool n) (minv : Vector ℤ n) (r j : Fin n) (q : List (Fin n × Fin n)) :

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.

structure Cslib.Algorithms.Lean.MaxFlow.HgInv {n : ℕ} (c : Fin n × Fin n → ℤ) (r : Fin n) (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) (used tree : Vector Bool n) (minv : Vector ℤ n) (path : Vector (List (Fin n × Fin n)) n) :

Invariant of the tree-growing loop of the phase rooted at r.

structure Cslib.Algorithms.Lean.MaxFlow.HgOuter {n : ℕ} (c : Fin n × Fin n → ℤ) (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) :

Invariant between phases.

theorem Cslib.Algorithms.Lean.MaxFlow.HgInv.tree_iff_used {n : ℕ} {c : Fin n × Fin n → ℤ} {r : Fin n} {y z : Vector ℤ n} {mate : Vector (Option (Fin n)) n} {used tree : Vector Bool n} {minv : Vector ℤ n} {path : Vector (List (Fin n × Fin n)) n} (h : HgInv c r y z mate used tree minv path) {i j : Fin n} (hm : vget mate j = some i) :
vget tree i = true ↔ vget used j = true
theorem Cslib.Algorithms.Lean.MaxFlow.HgInv.minv_nonneg {n : ℕ} {c : Fin n × Fin n → ℤ} {r : Fin n} {y z : Vector ℤ n} {mate : Vector (Option (Fin n)) n} {used tree : Vector Bool n} {minv : Vector ℤ n} {path : Vector (List (Fin n × Fin n)) n} (h : HgInv c r y z mate used tree minv path) {j : Fin n} (hj : vget used j = false) :
0 ≤ vget minv j

The dual adjustment #

theorem Cslib.Algorithms.Lean.MaxFlow.hgInv_dual {n : ℕ} {c : Fin n × Fin n → ℤ} {r : Fin n} {s : HgState n} (h : HgInv c r s.y s.z s.mate s.used s.tree s.minv s.path) {j₁ : Fin n} (hj₁ : vget s.used j₁ = false) (hmin : ∀ (j : Fin n), vget s.used j = false → vget s.minv j₁ ≤ vget s.minv j) :
HgInv c r (hgDual s (vget s.minv j₁)).y (hgDual s (vget s.minv j₁)).z s.mate s.used s.tree (hgDual s (vget s.minv j₁)).minv s.path
theorem Cslib.Algorithms.Lean.MaxFlow.minv_dual_self {n : ℕ} (s : HgState n) {j₁ : Fin n} (hj₁ : vget s.used j₁ = false) :
vget (hgDual s (vget s.minv j₁)).minv j₁ = 0

Augmenting along a tree path #

theorem Cslib.Algorithms.Lean.MaxFlow.hgAugment {n : ℕ} {c : Fin n × Fin n → ℤ} {r : Fin n} {y z : Vector ℤ n} {mate : Vector (Option (Fin n)) n} {used tree : Vector Bool n} {minv : Vector ℤ n} {path : Vector (List (Fin n × Fin n)) n} (h : HgInv c r y z mate used tree minv path) {j₁ : Fin n} (hj₁ : vget used j₁ = false) (hz : vget minv j₁ = 0) (hfree : vget mate j₁ = none) :
HgOuter c y z (hgApplyEdges mate (vget path j₁)) ∧ matchedLeft (mateMatching (hgApplyEdges mate (vget path j₁))) = insert r (matchedLeft (mateMatching mate))

Growing the tree #

theorem Cslib.Algorithms.Lean.MaxFlow.hgInv_extend {n : ℕ} {c : Fin n × Fin n → ℤ} {r : Fin n} {s : HgState n} (h : HgInv c r s.y s.z s.mate s.used s.tree s.minv s.path) {j₁ i₀ : Fin n} (hj₁ : vget s.used j₁ = false) (hz : vget s.minv j₁ = 0) (hm : vget s.mate j₁ = some i₀) :
HgInv c r (hgExtend c s j₁ i₀).y (hgExtend c s j₁ i₀).z (hgExtend c s j₁ i₀).mate (hgExtend c s j₁ i₀).used (hgExtend c s j₁ i₀).tree (hgExtend c s j₁ i₀).minv (hgExtend c s j₁ i₀).path
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgExtend_y {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (j₁ i₀ : Fin n) :
(hgExtend c s j₁ i₀).y = s.y
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgExtend_z {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (j₁ i₀ : Fin n) :
(hgExtend c s j₁ i₀).z = s.z
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgExtend_mate {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (j₁ i₀ : Fin n) :
(hgExtend c s j₁ i₀).mate = s.mate
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgExtend_used {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (j₁ i₀ : Fin n) :
(hgExtend c s j₁ i₀).used = vset s.used j₁ true
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.hgExtend_tree {n : ℕ} {c : Fin n × Fin n → ℤ} (s : HgState n) (j₁ i₀ : Fin n) :
(hgExtend c s j₁ i₀).tree = vset s.tree i₀ true

Counting tree columns #

Number of columns in the tree.

theorem Cslib.Algorithms.Lean.MaxFlow.usedCount_le {n : ℕ} {mate : Vector (Option (Fin n)) n} {used : Vector Bool n} (hM : IsMatching (fun (x : Fin n × Fin n) => true) (mateMatching mate)) (hu : ∀ (j : Fin n), vget used j = true → ∃ (i : Fin n), vget mate j = some i) :
theorem Cslib.Algorithms.Lean.MaxFlow.usedCount_vset {n : ℕ} {used : Vector Bool n} {j₁ : Fin n} (hj₁ : vget used j₁ = false) :
usedCount (vset used j₁ true) = usedCount used + 1
theorem Cslib.Algorithms.Lean.MaxFlow.usedCount_all {n : ℕ} {used : Vector Bool n} (h : ∀ (j : Fin n), vget used j = true) :
usedCount used = n
theorem Cslib.Algorithms.Lean.MaxFlow.card_lt_of_root_free {n : ℕ} {mate : Vector (Option (Fin n)) n} (hM : IsMatching (fun (x : Fin n × Fin n) => true) (mateMatching mate)) {r : Fin n} (hr : ∀ (j : Fin n), vget mate j ≠ some r) :
theorem Cslib.Algorithms.Lean.MaxFlow.exists_free_col_of_root_free {n : ℕ} {mate : Vector (Option (Fin n)) n} (hM : IsMatching (fun (x : Fin n × Fin n) => true) (mateMatching mate)) {r : Fin n} (hr : ∀ (j : Fin n), vget mate j ≠ some r) :
∃ (j : Fin n), vget mate j = none

The loop, the phase and the rows #

theorem Cslib.Algorithms.Lean.MaxFlow.hgInv_init {n : ℕ} {c : Fin n × Fin n → ℤ} {y z : Vector ℤ n} {mate : Vector (Option (Fin n)) n} {r : Fin n} (h : HgOuter c y z mate) (hr : r ∉ matchedLeft (mateMatching mate)) :
HgInv c r (hgInit c y z mate r).y (hgInit c y z mate r).z (hgInit c y z mate r).mate (hgInit c y z mate r).used (hgInit c y z mate r).tree (hgInit c y z mate r).minv (hgInit c y z mate r).path

Bounded potential and predecessor-array invariants #

structure Cslib.Algorithms.Lean.MaxFlow.HgOuterBound {n : ℕ} (c : Fin n × Fin n → ℤ) (W : ℕ) (y z : Vector ℤ n) (mate : Vector (Option (Fin n)) n) extends Cslib.Algorithms.Lean.MaxFlow.HgOuter c y z mate :

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.

structure Cslib.Algorithms.Lean.MaxFlow.HgStepInv {n : ℕ} (c : Fin n × Fin n → ℤ) (W : ℕ) (r : Fin n) (s : HgState n) (bestRow : Fin n → Fin n) extends Cslib.Algorithms.Lean.MaxFlow.HgInv c r s.y s.z s.mate s.used s.tree s.minv s.path :

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.

theorem Cslib.Algorithms.Lean.MaxFlow.hgStepInv_init {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {y z : Vector ℤ n} {mate : Vector (Option (Fin n)) n} {r : Fin n} (hc : ∀ (u v : Fin n), 0 ≤ c (u, v) ∧ c (u, v) ≤ ↑W) (h : HgOuterBound c W y z mate) (hr : r ∉ matchedLeft (mateMatching mate)) :
HgStepInv c W r (hgInit c y z mate r) fun (x : Fin n) => r
theorem Cslib.Algorithms.Lean.MaxFlow.hgStepInv_dual {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {r : Fin n} {s : HgState n} {bestRow : Fin n → Fin n} (hc : ∀ (u v : Fin n), 0 ≤ c (u, v) ∧ c (u, v) ≤ ↑W) (h : HgStepInv c W r s bestRow) {j₁ : Fin n} (hj₁ : vget s.used j₁ = false) (hmin : ∀ (j : Fin n), vget s.used j = false → vget s.minv j₁ ≤ vget s.minv j) :
HgStepInv c W r (hgDual s (vget s.minv j₁)) bestRow
theorem Cslib.Algorithms.Lean.MaxFlow.hgStepInv_extend {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {r : Fin n} {s : HgState n} {bestRow : Fin n → Fin n} (hc : ∀ (u v : Fin n), 0 ≤ c (u, v) ∧ c (u, v) ≤ ↑W) (h : HgStepInv c W r s bestRow) {j₁ i₀ : Fin n} (hj₁ : vget s.used j₁ = false) (hz : vget s.minv j₁ = 0) (hm : vget s.mate j₁ = some i₀) :
HgStepInv c W r (hgExtend c s j₁ i₀) fun (j : Fin n) => if vget (vset s.used j₁ true) j = false ∧ hgRed c s.y s.z i₀ j < vget s.minv j then i₀ else bestRow j
theorem Cslib.Algorithms.Lean.MaxFlow.hgOuterBound_augment {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {r : Fin n} {s : HgState n} {bestRow : Fin n → Fin n} (h : HgStepInv c W r s bestRow) {j₁ : Fin n} (hj₁ : vget s.used j₁ = false) (hz : vget s.minv j₁ = 0) (hfree : vget s.mate j₁ = none) :
theorem Cslib.Algorithms.Lean.MaxFlow.HgStepInv.path_eq_singleton {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {r : Fin n} {s : HgState n} {bestRow : Fin n → Fin n} (h : HgStepInv c W r s bestRow) {j : Fin n} (hfree : ∀ (j' : Fin n), vget s.mate j' ≠ some (bestRow j)) :
vget s.path j = [(bestRow j, j)]
theorem Cslib.Algorithms.Lean.MaxFlow.HgStepInv.path_eq_cons {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {r : Fin n} {s : HgState n} {bestRow : Fin n → Fin n} (h : HgStepInv c W r s bestRow) {j j' : Fin n} (hm : vget s.mate j' = some (bestRow j)) :
vget s.path j = (bestRow j, j) :: vget s.path j'
theorem Cslib.Algorithms.Lean.MaxFlow.HgStepInv.length_path_le {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} {r : Fin n} {s : HgState n} {bestRow : Fin n → Fin n} (h : HgStepInv c W r s bestRow) (j : Fin n) :

Correctness and Egerváry's theorem #

theorem Cslib.Algorithms.Lean.MaxFlow.hgRowMin_le {n : ℕ} (c : Fin n × Fin n → ℤ) (u v : Fin n) :
hgRowMin c u ≤ c (u, v)
theorem Cslib.Algorithms.Lean.MaxFlow.hgRowMin_bounds {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} (hc : ∀ (u v : Fin n), 0 ≤ c (u, v) ∧ c (u, v) ≤ ↑W) (u : Fin n) :
0 ≤ hgRowMin c u ∧ hgRowMin c u ≤ ↑W
theorem Cslib.Algorithms.Lean.MaxFlow.hgOuterBound_start {n : ℕ} {c : Fin n × Fin n → ℤ} {W : ℕ} (hc : ∀ (u v : Fin n), 0 ≤ c (u, v) ∧ c (u, v) ≤ ↑W) :
theorem Cslib.Algorithms.Lean.MaxFlow.hungarian_correct {n : ℕ} (c : Fin n × Fin n → ℤ) :
IsMinCostPerfectMatching (fun (x : Fin n × Fin n) => true) c (hungarian c).matching ∧ IsFeasiblePotential (fun (x : Fin n × Fin n) => true) c (hungarian c).y (hungarian c).z ∧ matchingCost c (hungarian c).matching = ∑ u : Fin n, (hungarian c).y u + ∑ v : Fin n, (hungarian c).z v

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 #

theorem Cslib.Algorithms.Lean.MaxFlow.hungarian_add_const {n : ℕ} (c : Fin n × Fin n → ℤ) (S : ℤ) :
(hungarian fun (e : Fin n × Fin n) => c e + S).matching = (hungarian c).matching ∧ (∀ (u : Fin n), (hungarian fun (e : Fin n × Fin n) => c e + S).y u = (hungarian c).y u + S) ∧ ∀ (v : Fin n), (hungarian fun (e : Fin n × Fin n) => c e + S).z v = (hungarian c).z v

Adding a constant S to every cost shifts row potentials by S and leaves column potentials and the output matching unchanged.

theorem Cslib.Algorithms.Lean.MaxFlow.exists_isMinCostPerfectMatching_potential_eq {n : ℕ} (c : Fin n × Fin n → ℤ) :
∃ (M : Finset (Fin n × Fin n)) (y : Fin n → ℤ) (z : Fin n → ℤ), IsMinCostPerfectMatching (fun (x : Fin n × Fin n) => true) c M ∧ IsFeasiblePotential (fun (x : Fin n × Fin n) => true) c y z ∧ matchingCost c M = ∑ u : Fin n, y u + ∑ v : Fin n, z v

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.

theorem Cslib.Algorithms.Lean.MaxFlow.isMinCostPerfectMatching_iff_exists_potential {n : ℕ} (c : Fin n × Fin n → ℤ) {M : Finset (Fin n × Fin n)} (hM : IsPerfectMatching (fun (x : Fin n × Fin n) => true) M) :
IsMinCostPerfectMatching (fun (x : Fin n × Fin n) => true) c M ↔ ∃ (y : Fin n → ℤ) (z : Fin n → ℤ), IsFeasiblePotential (fun (x : Fin n × Fin n) => true) c y z ∧ matchingCost c M = ∑ u : Fin n, y u + ∑ v : Fin n, z v

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.

theorem Cslib.Algorithms.Lean.MaxFlow.exists_perm_assignment {n : ℕ} (c : Fin n × Fin n → ℤ) :
∃ (σ : Equiv.Perm (Fin n)) (y : Fin n → ℤ) (z : Fin n → ℤ), (∀ (τ : Equiv.Perm (Fin n)), ∑ u : Fin n, c (u, σ u) ≤ ∑ u : Fin n, c (u, τ u)) ∧ (∀ (u v : Fin n), y u + z v ≤ c (u, v)) ∧ ∑ u : Fin n, c (u, σ u) = ∑ u : Fin n, y u + ∑ v : Fin n, z v

Assignment problem, permutation form: some permutation σ minimizes ∑ u, c (u, σ u) over all permutations, certified by integral feasible potentials with equal total.