Documentation

CslibFrontier.Algorithms.Lean.MaxFlow.BipartiteMatching

Maximum Bipartite Matching and König's Theorem via Network Flows #

Formalizes the reduction of maximum bipartite matching and minimum bipartite vertex cover to integral s-t network flow ([FF62], Chapter II, Sections 4–5, Theorem 4.1; [CLRS09], Section 26.3, Lemma 26.9, Theorem 26.10 & Corollary 26.11, pp. 732–735), derives König's Theorem ([Kon31], pp. 116–119), bridges to Cslib.Circuits.BipartiteMatching.HasPerfectMatching, and provides a pure executable solver via edmondsKarpDense. For the checked Word-RAM running-time bound (O((m + r)^5) instructions), see CslibFrontier.Algorithms.WordRAM.EdmondsKarp.

Model and source mapping #

Main definitions #

Main results #

References #

Bipartite matchings and vertex covers #

def Cslib.Algorithms.Lean.MaxFlow.IsMatching {L : Type u_1} {R : Type u_2} (G : L × R → Bool) (M : Finset (L × R)) :

M is a matching in the bipartite graph G : L × R → Bool if every pair in M is an edge of G and no two edges in M share a left endpoint or a right endpoint.

Instances For
    @[instance_reducible]
    instance Cslib.Algorithms.Lean.MaxFlow.instDecidableIsMatching {L : Type u_1} {R : Type u_2} [DecidableEq L] [DecidableEq R] (G : L × R → Bool) (M : Finset (L × R)) :
    def Cslib.Algorithms.Lean.MaxFlow.IsMaxMatching {L : Type u_1} {R : Type u_2} (G : L × R → Bool) (M : Finset (L × R)) :

    M is a maximum-cardinality matching in G.

    structure Cslib.Algorithms.Lean.MaxFlow.VertexCover (L : Type u_3) (R : Type u_4) :
    Type (max u_3 u_4)

    A bipartite vertex cover on (L, R), consisting of chosen left and right vertex subsets.

    • left : Finset L

      Chosen left vertices.

    • right : Finset R

      Chosen right vertices.

    Total cardinality |C.left| + |C.right| of a bipartite vertex cover C.

    def Cslib.Algorithms.Lean.MaxFlow.IsVertexCover {L : Type u_1} {R : Type u_2} (G : L × R → Bool) (C : VertexCover L R) :

    C is a vertex cover of G if every edge (u, v) with G (u, v) = true has u ∈ C.left or v ∈ C.right.

    Instances For
      @[instance_reducible]
      def Cslib.Algorithms.Lean.MaxFlow.IsMinVertexCover {L : Type u_1} {R : Type u_2} (G : L × R → Bool) (C : VertexCover L R) :

      C is a minimum-cardinality vertex cover of G.

      Unit-capacity flow network for a bipartite graph #

      inductive Cslib.Algorithms.Lean.MaxFlow.BipartiteVertex (L : Type u_3) (R : Type u_4) :
      Type (max u_3 u_4)

      Vertices of the unit-capacity s-t flow network for a bipartite graph on (L, R): designated source and sink together with left u (u : L) and right v (v : R).

      Instances For

        Canonical equivalence between BipartiteVertex L R and Bool ⊕ (L ⊕ R).

        @[instance_reducible]

        Unit-capacity s-t network associated with G : L × R → Bool ([CLRS09], Section 26.3, pp. 732–733): unit arcs .source → .left u, .left u → .right v when G (u, v) = true, and .right v → .sink.

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

        Extract a bipartite matching from any feasible integral flow f on bipartiteNetwork G by selecting pairs (u, v) carrying positive flow ([CLRS09], Lemma 26.9, p. 733).

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

        Construct a feasible unit integral flow on bipartiteNetwork G of value M.card from any bipartite matching M ([CLRS09], Lemma 26.9, p. 733).

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

        Cuts, vertex covers, and König's Theorem #

        Turn a bipartite vertex cover C into an s-t cut of bipartiteNetwork G of capacity C.size ([FF62], Chapter II, Section 4, Theorem 4.1).

        Extract a bipartite vertex cover of size at most cutCapacity S from any s-t cut S of bipartiteNetwork G.

        theorem Cslib.Algorithms.Lean.MaxFlow.card_le_size_of_matching_vertexCover {L : Type u_1} {R : Type u_2} [Finite L] [Finite R] {G : L × R → Bool} {M : Finset (L × R)} (hM : IsMatching G M) {C : VertexCover L R} (hC : IsVertexCover G C) :

        Weak duality for bipartite matchings and vertex covers: every matching M in G has cardinality at most the size of every vertex cover C of G.

        Any maximum flow f and minimum cut S of equal value on bipartiteNetwork G yield a maximum matching flowToMatching f and minimum König vertex cover cutToVertexCover G S of equal size.

        theorem Cslib.Algorithms.Lean.MaxFlow.exists_maxMatching_eq_minVertexCover {L : Type u_1} {R : Type u_2} [Finite L] [Finite R] (G : L × R → Bool) :
        ∃ (M : Finset (L × R)) (C : VertexCover L R), IsMaxMatching G M ∧ IsMinVertexCover G C ∧ M.card = C.size

        König's Theorem ([Kon31], pp. 116–119; [FF62], Chapter II, Section 4, Theorem 4.1): every finite bipartite graph G : L × R → Bool admits a maximum matching M and a minimum vertex cover C of equal size (M.card = C.size).

        Bridge to Cslib.Circuits.BipartiteMatching.HasPerfectMatching #

        HasPerfectMatching G holds if and only if G admits a matching of cardinality Fintype.card V.

        Flow characterization of HasPerfectMatching G: G : V × V → Bool has a perfect matching if and only if bipartiteNetwork G admits an integral flow of value Fintype.card V.

        Pure executable bipartite matching and König cover solver #

        Equivalence between Bool and Fin 2 (false ↦ 0, true ↦ 1).

        Computable vertex equivalence BipartiteVertex (Fin m) (Fin r) ≃ Fin (2 + (m + r)).

        Reindexed Fin (2 + (m + r)) flow network for G : Fin m × Fin r → Bool.

        Output of bipartiteMatching on G : Fin m × Fin r → Bool.

        • matching : Finset (Fin m × Fin r)

          Maximum-cardinality matching in G.

        • cover : VertexCover (Fin m) (Fin r)

          Minimum-cardinality König vertex cover of G.

        • augmentations : ℕ

          Number of shortest-path augmentations performed by Edmonds–Karp.

        Compute a maximum bipartite matching and minimum König vertex cover of G : Fin m × Fin r → Bool via edmondsKarpDense.

        Correctness of bipartiteMatching: returns a maximum matching res.matching and minimum vertex cover res.cover satisfying König strong duality res.matching.card = res.cover.size.

        Decide whether G : Fin n × Fin n → Bool has a perfect matching via bipartiteMatching.