Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.AugmentingPath

Augmenting Paths, Berge's Theorem, and the Hopcroft–Karp Phase Bound #

Combinatorial core of the Hopcroft–Karp maximum bipartite matching algorithm ([HK73]; [CLRS09], Problem 26-6), on the edge-indicator representation G : L × R → Bool and Finset (L × R) matchings of CslibFrontier.Algorithms.Lean.MaxFlow.BipartiteMatching.

Main definitions #

Main results #

Limitations #

References #

Left endpoints of the edges of M.

Right endpoints of the edges of M.

@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.mem_matchedLeft {L : Type u_1} {R : Type u_2} [DecidableEq L] {M : Finset (L × R)} {u : L} :
u ∈ matchedLeft M ↔ ∃ (v : R), (u, v) ∈ M
@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.mem_matchedRight {L : Type u_1} {R : Type u_2} [DecidableEq R] {M : Finset (L × R)} {v : R} :
v ∈ matchedRight M ↔ ∃ (u : L), (u, v) ∈ M
theorem Cslib.Algorithms.Lean.MaxFlow.card_matchedLeft {L : Type u_1} {R : Type u_2} [DecidableEq L] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) :
theorem Cslib.Algorithms.Lean.MaxFlow.card_matchedRight {L : Type u_1} {R : Type u_2} [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsMatching.card_le_card_left {L : Type u_1} {R : Type u_2} {G : L × R → Bool} {M : Finset (L × R)} [Fintype L] (hM : IsMatching G M) :

A matching has at most as many edges as there are left vertices.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMatching.card_le_card_right {L : Type u_1} {R : Type u_2} {G : L × R → Bool} {M : Finset (L × R)} [Fintype R] (hM : IsMatching G M) :

A matching has at most as many edges as there are right vertices.

def Cslib.Algorithms.Lean.MaxFlow.rightNbrs {L : Type u_1} {R : Type u_2} [Fintype R] (G : L × R → Bool) (S : Finset L) :

Right vertices adjacent in G to some vertex of S.

def Cslib.Algorithms.Lean.MaxFlow.altLeft {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) :
ℕ → Finset L

Left vertices reachable from free left vertices by an M-alternating path with at most j matched edges.

def Cslib.Algorithms.Lean.MaxFlow.altRight {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (j : ℕ) :

Right vertices adjacent to altLeft G M j.

def Cslib.Algorithms.Lean.MaxFlow.IsLayer {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (u : L) (j : ℕ) :

u first appears in alternating layer j.

def Cslib.Algorithms.Lean.MaxFlow.NoShortAug {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (k : ℕ) :

No alternating path with at most k - 1 matched edges from a free left vertex ends at a free right vertex: every vertex of altRight G M j, j < k, is matched.

@[simp]
theorem Cslib.Algorithms.Lean.MaxFlow.mem_rightNbrs {L : Type u_1} {R : Type u_2} [Fintype R] {G : L × R → Bool} {S : Finset L} {v : R} :
v ∈ rightNbrs G S ↔ ∃ u ∈ S, G (u, v) = true
theorem Cslib.Algorithms.Lean.MaxFlow.mem_altLeft_zero {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} :
u ∈ altLeft G M 0 ↔ u ∉ matchedLeft M
theorem Cslib.Algorithms.Lean.MaxFlow.mem_altLeft_succ {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {j : ℕ} :
u ∈ altLeft G M (j + 1) ↔ u ∈ altLeft G M j ∨ ∃ (v : R), (u, v) ∈ M ∧ v ∈ altRight G M j
theorem Cslib.Algorithms.Lean.MaxFlow.mem_altRight {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {v : R} {j : ℕ} :
v ∈ altRight G M j ↔ ∃ u ∈ altLeft G M j, G (u, v) = true
theorem Cslib.Algorithms.Lean.MaxFlow.altLeft_mono {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {i j : ℕ} (h : i ≤ j) :
altLeft G M i ⊆ altLeft G M j
theorem Cslib.Algorithms.Lean.MaxFlow.altRight_mono {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {i j : ℕ} (h : i ≤ j) :
altRight G M i ⊆ altRight G M j
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayer.unique {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {i j : ℕ} (hi : IsLayer G M u i) (hj : IsLayer G M u j) :
i = j
theorem Cslib.Algorithms.Lean.MaxFlow.exists_isLayer {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {j : ℕ} (h : u ∈ altLeft G M j) :
∃ i ≤ j, IsLayer G M u i
theorem Cslib.Algorithms.Lean.MaxFlow.isLayer_zero {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} :
IsLayer G M u 0 ↔ u ∉ matchedLeft M
theorem Cslib.Algorithms.Lean.MaxFlow.NoShortAug.mono {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k k' : ℕ} (h : NoShortAug G M k) (hk : k' ≤ k) :
NoShortAug G M k'
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayer.le_of_mem {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {i j : ℕ} (hu : IsLayer G M u i) (h : u ∈ altLeft G M j) :
i ≤ j
theorem Cslib.Algorithms.Lean.MaxFlow.mem_altLeft_succ_of_mem {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {v : R} {j : ℕ} (huv : (u, v) ∈ M) (hv : v ∈ altRight G M j) :
u ∈ altLeft G M (j + 1)
theorem Cslib.Algorithms.Lean.MaxFlow.rightNbrs_mono {L : Type u_1} {R : Type u_2} [Fintype R] {G : L × R → Bool} {S T : Finset L} (h : S ⊆ T) :
rightNbrs G S ⊆ rightNbrs G T
theorem Cslib.Algorithms.Lean.MaxFlow.mem_altRight_of_isLayer {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {v : R} {i j : ℕ} (hu : IsLayer G M u i) (hij : i ≤ j) (hG : G (u, v) = true) :
v ∈ altRight G M j
def Cslib.Algorithms.Lean.MaxFlow.layerCover {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (j : ℕ) :

Vertex cover built from alternating layer j: unreached left vertices and reached right vertices.

theorem Cslib.Algorithms.Lean.MaxFlow.isVertexCover_layerCover {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (j : ℕ) :
theorem Cslib.Algorithms.Lean.MaxFlow.card_mul_le_of_noShortAug {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {M' : Finset (L × R)} (hM' : IsMatching G M') {k : ℕ} (hk : NoShortAug G M k) :
k * M'.card ≤ (k + 1) * M.card

Layered counting lemma. If no alternating path with fewer than k matched edges leads from a free left vertex to a free right vertex, then every matching M' satisfies k * |M'| ≤ (k + 1) * |M|.

def Cslib.Algorithms.Lean.MaxFlow.augment {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] (M N : Finset (L × R)) :
Finset (L × R)

Replace the edges of M at left vertices covered by N with the edges of N.

theorem Cslib.Algorithms.Lean.MaxFlow.mem_augment {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {M N : Finset (L × R)} {e : L × R} :
e ∈ augment M N ↔ e ∈ N ∨ e ∈ M ∧ e.1 ∉ matchedLeft N
theorem Cslib.Algorithms.Lean.MaxFlow.isMatching_augment {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} (hM : IsMatching G M) (hN : IsMatching G N) (hcompat : ∀ (u : L) (v : R), (u, v) ∈ M → v ∈ matchedRight N → u ∈ matchedLeft N) :

Augmenting a matching M by a matching N gives a matching provided every edge of M whose right end is covered by N also has its left end covered by N.

theorem Cslib.Algorithms.Lean.MaxFlow.card_augment {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} (hM : IsMatching G M) (hN : IsMatching G N) :

Exact size of an augmentation: each newly covered left vertex adds one edge.

The left vertices covered by an augmentation are those covered by N or by M.

structure Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (p : List (L × R)) :

An M-augmenting path u₀ v₀ u₁ v₁ … uₗ vₗ, recorded by its non-matching edges (uᵢ, vᵢ): consecutive ones are joined by matching edges (uᵢ₊₁, vᵢ) ∈ M, all vertices are distinct, and both ends u₀, vₗ are free.

theorem Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath.isMatching_toFinset {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {p : List (L × R)} (hp : IsAugmentingPath G M p) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath.compat {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {p : List (L × R)} (hp : IsAugmentingPath G M p) (hM : IsMatching G M) (u : L) (v : R) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath.matchedLeft_toFinset_subset {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {p : List (L × R)} (hp : IsAugmentingPath G M p) (u : L) :
u ∈ matchedLeft p.toFinset → (∃ h ∈ p.head?, h.1 = u) ∨ u ∈ matchedLeft M

Every left vertex of an augmenting path other than its first is already matched.

theorem Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath.compat_iff {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {p : List (L × R)} (hp : IsAugmentingPath G M p) (hM : IsMatching G M) (u : L) (v : R) :

An augmenting path covers a matched left vertex iff it covers its mate.

theorem Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath.isMatching_augment {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {p : List (L × R)} (hp : IsAugmentingPath G M p) (hM : IsMatching G M) :

Augmenting along an augmenting path gives a matching.

theorem Cslib.Algorithms.Lean.MaxFlow.IsAugmentingPath.card_augment {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {p : List (L × R)} (hp : IsAugmentingPath G M p) (hM : IsMatching G M) :

Augmenting along an augmenting path increases the matching size by one.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaxMatching.not_isAugmentingPath {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMaxMatching G M) {p : List (L × R)} :

A maximum matching has no augmenting path (the easy direction of Berge's theorem).

inductive Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (U : Finset R) (k : ℕ) :
L → ℕ → List (L × R) → Prop

IsLayeredPath G M U k u i p: p lists the non-matching edges of an alternating path that starts at u in exact layer i, visits exact layers i, i + 1, …, k, avoids the right vertices in U, and ends at a free right vertex.

inductive Cslib.Algorithms.Lean.MaxFlow.IsLayeredPrefix {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (U : Finset R) (r : L) :
L → ℕ → List (L × R) → Prop

IsLayeredPrefix G M U r u j q: q lists the non-matching edges of an alternating path from the free vertex r to u in exact layer j, avoiding U.

theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.isLayer {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k i : ℕ} {u : L} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
IsLayer G M u i
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.le {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k i : ℕ} {u : L} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
i ≤ k
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.mono {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U U' : Finset R} (hU : U' ⊆ U) {k i : ℕ} {u : L} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
IsLayeredPath G M U' k u i p
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPrefix.isLayer {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {r u : L} {j : ℕ} {q : List (L × R)} (h : IsLayeredPrefix G M U r u j q) :
IsLayer G M u j
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPrefix.append {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {r u : L} {j : ℕ} {q : List (L × R)} (hq : IsLayeredPrefix G M U r u j q) {p : List (L × R)} (hp : IsLayeredPath G M U k u j p) :
IsLayeredPath G M U k r 0 (q ++ p)
theorem Cslib.Algorithms.Lean.MaxFlow.exists_isLayeredPrefix {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {u : L} {j : ℕ} (hu : IsLayer G M u j) :
∃ (r : L) (q : List (L × R)), IsLayeredPrefix G M ∅ r u j q
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.head {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
∃ (v : R) (l : List (L × R)), p = (u, v) :: l
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.edge {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) (e : L × R) :
e ∈ p → G e = true
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.notMem {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) (e : L × R) :
e ∈ p → e.2 ∉ U
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.mem_layers {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) (e : L × R) :
e ∈ p → ∃ (j : ℕ), i ≤ j ∧ IsLayer G M e.1 j ∧ (e.2 ∉ matchedRight M ∧ j = k ∨ ∃ (w : L), (w, e.2) ∈ M ∧ IsLayer G M w (j + 1))
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.nodup_left {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.nodup_right {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} (hM : IsMatching G M) {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.chain {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) :
List.IsChain (fun (a b : L × R) => (b.1, a.2) ∈ M) p
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.last_free {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) (e : L × R) :
e ∈ p.getLast? → e.2 ∉ matchedRight M
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.isAugmentingPath {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} (hM : IsMatching G M) {u : L} {p : List (L × R)} (h : IsLayeredPath G M U k u 0 p) :

A layered path starting in layer 0 is an augmenting path.

theorem Cslib.Algorithms.Lean.MaxFlow.noShortAug_of_forall_not_isAugmentingPath {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) (h : ∀ (p : List (L × R)), ¬IsAugmentingPath G M p) (k : ℕ) :

If M has no augmenting path, then no alternating layer reaches a free right vertex.

Berge's theorem (bipartite case): a matching is maximum iff it has no augmenting path.

theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredPath.mem_left {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {U : Finset R} {k : ℕ} {u : L} {i : ℕ} {p : List (L × R)} (h : IsLayeredPath G M U k u i p) (e : L × R) :
e ∈ p → e.1 = u ∨ ∃ e' ∈ p, (e.1, e'.2) ∈ M
inductive Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) (k : ℕ) :
List (L × R) → Prop

IsLayeredFamily G M k l: l is the concatenation of layered paths of length k starting at distinct free left vertices, each avoiding the right vertices used by the later ones. This is what one depth-first phase of Hopcroft–Karp produces.

theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily.layered {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k : ℕ} {l : List (L × R)} (h : IsLayeredFamily G M k l) (u : L) (v : R) :
(u, v) ∈ l.toFinset → ∃ (i : ℕ), IsLayer G M u i ∧ (v ∉ matchedRight M ∧ i = k ∨ ∃ (w : L), (w, v) ∈ M ∧ IsLayer G M w (i + 1))
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily.compat {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) {l : List (L × R)} (h : IsLayeredFamily G M k l) (u : L) (v : R) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily.isMatching {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) {l : List (L × R)} (h : IsLayeredFamily G M k l) :
theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily.exists_free {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k : ℕ} {l : List (L × R)} (h : IsLayeredFamily G M k l) (hne : l ≠ []) :
∃ u ∈ matchedLeft l.toFinset, u ∉ matchedLeft M

A nonempty layered family starts at a free left vertex.

theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily.nodup_right {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) {l : List (L × R)} (h : IsLayeredFamily G M k l) :
structure Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M N : Finset (L × R)) (k : ℕ) :

N is the edge set found by one Hopcroft–Karp phase for M with shortest augmenting path parameter k: a matching whose edges follow the alternating layers of M, ending in layer k at free right vertices, such that each M-edge is either kept or replaced at both ends, and no layered path with k + 1 non-matching edges from a free left vertex avoids the vertices of N.

theorem Cslib.Algorithms.Lean.MaxFlow.IsLayeredFamily.isMaximalLayeredAugmentation {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) {l : List (L × R)} (h : IsLayeredFamily G M k l) (hmax : ∀ r ∉ matchedLeft l.toFinset, ∀ (p : List (L × R)), ¬IsLayeredPath G M (matchedRight l.toFinset) k r 0 p) :

A layered family that cannot be extended by a further layered path is a maximal layered augmentation.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation.altLeft_augment_subset {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} {k : ℕ} (hk : NoShortAug G M k) (hN : IsMaximalLayeredAugmentation G M N k) {j : ℕ} (hj : j ≤ k) :
altLeft G (augment M N) j ⊆ altLeft G M j

Alternating layers do not grow after a phase: altLeft (augment M N) j ⊆ altLeft M j for j ≤ k.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation.noShortAug_augment {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} {k : ℕ} (hk : NoShortAug G M k) (hN : IsMaximalLayeredAugmentation G M N k) :
NoShortAug G (augment M N) (k + 1)

Phase lemma (Hopcroft–Karp). Augmenting M along a maximal layered augmentation for the shortest augmenting path parameter k leaves no alternating path with at most k matched edges between free vertices: the shortest augmenting path length strictly increases.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation.card_lt_card_augment {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) (hN : IsMaximalLayeredAugmentation G M N k) {u : L} (hu : u ∈ matchedLeft N) (hfree : u ∉ matchedLeft M) :
M.card < (augment M N).card

A phase covering some free left vertex strictly enlarges the matching.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation.exists_free_of_mem {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) (hN : IsMaximalLayeredAugmentation G M N k) {e : L × R} (he : e ∈ N) :
∃ u ∈ matchedLeft N, u ∉ matchedLeft M

Progress. A nonempty maximal layered augmentation covers a left vertex that was free.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation.exists_mem {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} {k : ℕ} (hk : NoShortAug G M k) (hN : IsMaximalLayeredAugmentation G M N k) {t : R} (ht : t ∈ altRight G M k) (hfree : t ∉ matchedRight M) :
∃ (e : L × R), e ∈ N

If a free right vertex is adjacent to layer k, a maximal layered augmentation for k is nonempty.

theorem Cslib.Algorithms.Lean.MaxFlow.IsMaximalLayeredAugmentation.card_lt_card_augment_of_mem_altRight {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M N : Finset (L × R)} {k : ℕ} (hM : IsMatching G M) (hk : NoShortAug G M k) (hN : IsMaximalLayeredAugmentation G M N k) {t : R} (ht : t ∈ altRight G M k) (hfree : t ∉ matchedRight M) :
M.card < (augment M N).card

A phase run after a BFS that found a free right vertex in layer k strictly enlarges the matching.

theorem Cslib.Algorithms.Lean.MaxFlow.mul_card_sub_le_of_noShortAug {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {M' : Finset (L × R)} (hM' : IsMatching G M') {t : ℕ} (ht : NoShortAug G M t) :
t * (M'.card - M.card) ≤ M.card

Deficit bound: if no augmenting path has fewer than t matched edges, then M is within |M| / t of every matching.

inductive Cslib.Algorithms.Lean.MaxFlow.IsPhaseRun {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] (G : L × R → Bool) :
Finset (L × R) → ℕ → Prop

IsPhaseRun G M n: starting from M, n Hopcroft–Karp phases are performed. Each phase follows a breadth-first search that found the shortest augmenting path parameter k (no free right vertex in layers < k, one in layer k) and augments along a maximal layered augmentation.

theorem Cslib.Algorithms.Lean.MaxFlow.IsPhaseRun.add_card_le_and_add_le_two_mul_sqrt {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {n : ℕ} (hrun : IsPhaseRun G M n) {t : ℕ} (ht : NoShortAug G M t) {M' : Finset (L × R)} (hM' : IsMaxMatching G M') :
n + M.card ≤ M'.card ∧ (t ≤ M'.card.sqrt → t + n ≤ 2 * M'.card.sqrt)

Hopcroft–Karp phase bound. A run of n phases from a matching M with no augmenting path shorter than t satisfies n + |M| ≤ ν and, when t ≤ ⌊√ν⌋, t + n ≤ 2⌊√ν⌋, where ν is the maximum matching size. In particular, from the empty matching at most 2⌊√ν⌋ phases occur.

theorem Cslib.Algorithms.Lean.MaxFlow.IsPhaseRun.le_two_mul_sqrt {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {n : ℕ} (hrun : IsPhaseRun G ∅ n) {M' : Finset (L × R)} (hM' : IsMaxMatching G M') :
n ≤ 2 * M'.card.sqrt

From the empty matching, Hopcroft–Karp performs at most 2⌊√ν⌋ augmenting phases.

theorem Cslib.Algorithms.Lean.MaxFlow.size_layerCover_of_stable {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {j : ℕ} (hstable : altLeft G M (j + 1) = altLeft G M j) (hj : altRight G M j ⊆ matchedRight M) :
(layerCover G M j).size = M.card

If the alternating layers of M stop growing at j and reach no free right vertex, then the layer cover at j has size |M|.

theorem Cslib.Algorithms.Lean.MaxFlow.isMaxMatching_isMinVertexCover_of_stable {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {j : ℕ} (hstable : altLeft G M (j + 1) = altLeft G M j) (hj : altRight G M j ⊆ matchedRight M) :

König certificate from a closed BFS. When the alternating layers stop growing without reaching a free right vertex, M is a maximum matching and the layer cover is a minimum vertex cover of the same size.

theorem Cslib.Algorithms.Lean.MaxFlow.mem_altRight_iff_of_stable {L : Type u_1} {R : Type u_2} [Fintype L] [Fintype R] [DecidableEq L] [DecidableEq R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {j : ℕ} (hstable : altLeft G M (j + 1) = altLeft G M j) (hj : altRight G M j ⊆ matchedRight M) {v : R} :
v ∈ altRight G M j ↔ ∃ (w : L), (w, v) ∈ M ∧ w ∈ altLeft G M j

Under stability, the right vertices reached by the layers are exactly the mates of reached left vertices.