Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.ChainCover

Minimum chain cover by Hopcroft–Karp #

minChainCover e computes a minimum chain cover and a maximum antichain of a finite poset α, given as an enumeration e : Fin n ≃ α with decidable <. It runs Fulkerson's reduction (CslibFrontier.Algorithms.Lean.MaxFlow.Dilworth) on the output of Hopcroft–Karp:

  1. hopcroftKarpDense (enumLtGraph e) returns a maximum matching M and a minimum König cover C of the bipartite graph joining index i to index j when e i < e j.
  2. Starting from the labels i ↦ i, chainLabels scans every pair of indices and, for each pair (i, j) ∈ M, relabels j's chain with i's label (mergeChains). The resulting label fibres are chains, and there are n - |M| labels.
  3. The indices missed by C on both sides form an antichain of at least n - |C| = n - |M| elements (uncoveredStep).

minChainCover_correct proves that the label fibres are chains, the returned index set is an antichain, and the number of labels equals the size of the antichain, n - |M|. With weak duality this certifies both as optimal (card_le_card_antichain_of_isAntichain, card_antichain_le_of_isChain_fibre), which is the algorithmic content of Dilworth's theorem.

Machine-checked O(n^{5/2}) word-RAM running-time bounds for minimum chain cover and maximum antichain live in CslibFrontier.Algorithms.WordRAM.HopcroftKarp.

Main definitions #

Main results #

References #

def Cslib.Algorithms.Lean.MaxFlow.enumLtGraph {α : Type u_1} {n : ℕ} [LT α] [DecidableLT α] (e : Fin n ≃ α) :
Fin n × Fin n → Bool

Fulkerson's graph ltGraph read through an enumeration e : Fin n ≃ α: index i is joined to index j when e i < e j.

Output of minChainCover.

  • labels : Vector (Fin n) n

    Chain label of each index; indices with equal labels lie on one chain.

  • antichain : Finset (Fin n)

    A maximum antichain, as a set of indices.

  • matching : Finset (Fin n × Fin n)

    The maximum matching of enumLtGraph e found by Hopcroft–Karp.

def Cslib.Algorithms.Lean.MaxFlow.mergeChains {n : ℕ} (labels : Vector (Fin n) n) (p : Fin n × Fin n) :
Vector (Fin n) n

Merge the chain of index j into the chain of index i, for p = (i, j): every label equal to j's becomes i's.

def Cslib.Algorithms.Lean.MaxFlow.chainLabels {n : ℕ} (M : Finset (Fin n × Fin n)) :
List (Fin n × Fin n) → Vector (Fin n) n → Vector (Fin n) n

Scan the candidate pairs ps and merge along those in M.

def Cslib.Algorithms.Lean.MaxFlow.uncoveredStep {n : ℕ} (C : VertexCover (Fin n) (Fin n)) (A : Finset (Fin n)) (i : Fin n) :

Insert index i into the antichain when the König cover C misses it on both sides.

Minimum chain cover of a finite poset enumerated by e : Fin n ≃ α, by Fulkerson's reduction: a maximum matching of enumLtGraph e from hopcroftKarpDense, chains obtained by merging along its edges, and a maximum antichain read off the König cover.

def Cslib.Algorithms.Lean.MaxFlow.ChainCoverOutput.label {α : Type u_1} {n : ℕ} (e : Fin n ≃ α) (out : ChainCoverOutput n) (x : α) :
Fin n

The chain label of x : α in an output out for the enumeration e.

The number of chains of an output: the number of distinct labels in the table.

Correctness #

theorem Cslib.Algorithms.Lean.MaxFlow.chainLabels_eq_foldl_rowMate {n : ℕ} {M : Finset (Fin n × Fin n)} (rm : Fin n → Option (Fin n)) (hrm : ∀ (i j : Fin n), rm i = some j ↔ (i, j) ∈ M) (labels₀ : Vector (Fin n) n) :
chainLabels M (List.finRange n ×ˢ List.finRange n) labels₀ = List.foldl (fun (lab : Vector (Fin n) n) (i : Fin n) => match rm i with | some j => mergeChains lab (i, j) | none => lab) labels₀ (List.finRange n)

When rm i is the unique right mate of i (rm i = some j ↔ (i, j) ∈ M), scanning List.finRange n ×ˢ List.finRange n in chainLabels equals a single O(n)-outer-step fold over i ∈ List.finRange n.

Correctness of minChainCover. The label fibres are chains, the returned indices form an antichain, and the number of labels equals the size of the antichain, namely n - |M| for the maximum matching M found by Hopcroft–Karp. By weak duality the antichain is maximum and the chain cover minimum (card_le_card_antichain_of_isAntichain, card_antichain_le_of_isChain_fibre).

theorem Cslib.Algorithms.Lean.MaxFlow.card_le_card_antichain_of_isAntichain {α : Type u_1} {n : ℕ} [PartialOrder α] [DecidableLT α] (e : Fin n ≃ α) {A : Finset α} (hA : IsAntichain (fun (x1 x2 : α) => x1 ≤ x2) ↑A) :

The antichain returned by minChainCover is maximum.

theorem Cslib.Algorithms.Lean.MaxFlow.card_antichain_le_of_isChain_fibre {α : Type u_1} {n : ℕ} [PartialOrder α] [DecidableLT α] (e : Fin n ≃ α) {k : ℕ} {f : α → Fin k} (hf : ∀ (i : Fin k), IsChain (fun (x1 x2 : α) => x1 ≤ x2) (f ⁻¹' {i})) :

The chain cover returned by minChainCover is minimum: every covering of α by k chains has k at least the number of its labels.