Documentation

LeanPool.BeyondBethe.BeyondBethe.KuhnMatching

Kuhn Matching #

Executable augmenting-path matching #

This file implements the polynomial augmenting-path algorithm for the positive support of a square rational matrix. A state maps each column to its currently matched row. Failed recursive searches return the enlarged set of visited columns, so a single root search examines each column at most once; this detail is essential for the polynomial bound.

@[reducible, inline]

A table assigning each column either a matched row or no match.

Equations
Instances For

    The column-mate table with every column unmatched.

    Equations
    Instances For
      structure BeyondBethe.IsSupportColumnMate {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (mate : ColumnMate n) :

      A column-to-row table represents a partial matching in the positive support of A.

      • support {col row : Fin n} : mate col = some row → A row col ≠ 0
      • injective {col col' row : Fin n} : mate col = some row → mate col' = some row → col = col'
      Instances For
        def BeyondBethe.RowUnmatched {n : ℕ} (mate : ColumnMate n) (row : Fin n) :

        No column in the mate table is assigned to the given row.

        Equations
        Instances For
          def BeyondBethe.MatchesRow {n : ℕ} (mate : ColumnMate n) (row : Fin n) :

          Some column in the mate table is assigned to the given row.

          Equations
          Instances For
            theorem BeyondBethe.IsSupportColumnMate.update_some {n : ℕ} {A : Matrix (Fin n) (Fin n) ℚ} {mate : ColumnMate n} (h : IsSupportColumnMate A mate) {col row : Fin n} (hrow : RowUnmatched mate row) (hedge : A row col ≠ 0) :
            theorem BeyondBethe.matchesRow_update_none_iff {n : ℕ} {mate : ColumnMate n} {col oldRow row : Fin n} (hinj : ∀ {j j' r : Fin n}, mate j = some r → mate j' = some r → j = j') (hmate : mate col = some oldRow) :
            MatchesRow (Function.update mate col none) row ↔ MatchesRow mate row ∧ row ≠ oldRow
            theorem BeyondBethe.rowUnmatched_update_none_of_mate {n : ℕ} {A : Matrix (Fin n) (Fin n) ℚ} {mate : ColumnMate n} (h : IsSupportColumnMate A mate) {col oldRow : Fin n} (hmate : mate col = some oldRow) :
            theorem BeyondBethe.matchesRow_update_some_iff {n : ℕ} {mate : ColumnMate n} {col newRow row : Fin n} (hcol : mate col = none) :
            MatchesRow (Function.update mate col (some newRow)) row ↔ MatchesRow mate row ∨ row = newRow

            Result of one augmenting-path search. mate? = some m records success; either way, seen contains every column examined by the search.

            • mate? : Option (ColumnMate n)

              The updated mate table on successful augmentation, or none when the search fails.

            • seen : Finset (Fin n)

              The set of columns visited by the search, retained even when augmentation fails.

            Instances For
              @[irreducible]
              def BeyondBethe.kuhnSearch {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) :
              List (Fin n) → Fin n → Finset (Fin n) → ColumnMate n → KuhnSearchResult n

              Depth-first augmenting-path search with a shared visited-column set. The first natural-number argument bounds alternating-path depth; the column list is the part of the current row that remains to be scanned. A failed recursive call threads its enlarged visited set into the rest of the scan. The lexicographic recursion makes both sources of progress explicit.

              Equations
              Instances For
                @[irreducible]
                def BeyondBethe.kuhnSearchWork {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) :
                List (Fin n) → Fin n → Finset (Fin n) → ColumnMate n → ℕ

                Number of column inspections made by kuhnSearch. Recursive work is charged only on the branch actually taken by the executable search.

                Equations
                Instances For
                  def BeyondBethe.kuhnAugment {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (row : Fin n) (seen : Finset (Fin n)) (mate : ColumnMate n) :

                  Search all columns from scratch for an augmenting path rooted at row.

                  Equations
                  Instances For
                    theorem BeyondBethe.kuhnSearch_seen_mono {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (remaining : List (Fin n)) (row : Fin n) (seen : Finset (Fin n)) (mate : ColumnMate n) :
                    seen ⊆ (kuhnSearch A fuel remaining row seen mate).seen
                    theorem BeyondBethe.kuhnSearchWork_le {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (remaining : List (Fin n)) (row : Fin n) (seen : Finset (Fin n)) (mate : ColumnMate n) :
                    kuhnSearchWork A fuel remaining row seen mate ≤ remaining.length + (n + 1) * ((kuhnSearch A fuel remaining row seen mate).seen.card - seen.card)

                    Amortized work bound. Scanning the current suffix is charged directly; each newly visited column pays for at most one fresh full row scan.

                    def BeyondBethe.kuhnAugmentWork {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (row : Fin n) (seen : Finset (Fin n)) (mate : ColumnMate n) :

                    The work count for an augmenting-path search over the full ordered column list.

                    Equations
                    Instances For
                      theorem BeyondBethe.kuhnAugmentWork_le {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (row : Fin n) (mate : ColumnMate n) :
                      kuhnAugmentWork A fuel row ∅ mate ≤ n + (n + 1) * n

                      One augmentation makes at most n + (n+1)n column inspections.

                      theorem BeyondBethe.kuhnSearch_preserves_seen {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (remaining : List (Fin n)) (row c : Fin n) (seen : Finset (Fin n)) (mate mate' : ColumnMate n) (hc : c ∈ seen) (hresult : (kuhnSearch A fuel remaining row seen mate).mate? = some mate') :
                      mate' c = mate c

                      A search never changes a column that was already marked as visited when the search began.

                      theorem BeyondBethe.kuhnSearch_success {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (remaining : List (Fin n)) (row : Fin n) (seen : Finset (Fin n)) (mate mate' : ColumnMate n) (hsupport : IsSupportColumnMate A mate) (hunmatched : RowUnmatched mate row) (hresult : (kuhnSearch A fuel remaining row seen mate).mate? = some mate') :
                      IsSupportColumnMate A mate' ∧ ∀ (r : Fin n), MatchesRow mate' r ↔ MatchesRow mate r ∨ r = row

                      On success, augmenting adds exactly the root row to the set of matched rows, and it preserves the support and injectivity invariants.

                      def BeyondBethe.AllSupportNeighborsIn {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (row : Fin n) (cols : Finset (Fin n)) :

                      Every nonzero support neighbor of the row lies in the specified column set.

                      Equations
                      Instances For
                        structure BeyondBethe.KuhnFailureCertificate {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (mate : ColumnMate n) (row : Fin n) (initialSeen finalSeen : Finset (Fin n)) (remaining : List (Fin n)) :

                        A failed search has explored every remaining support edge of its root. Every newly explored column was occupied, and the old row at that column has all of its support neighbors in the final explored set.

                        • seen_subset : initialSeen ⊆ finalSeen
                        • scanned (col : Fin n) : col ∈ remaining → A row col ≠ 0 → col ∈ finalSeen
                        • occupied_closed (col : Fin n) : col ∈ finalSeen → col ∉ initialSeen → ∃ (oldRow : Fin n), mate col = some oldRow ∧ AllSupportNeighborsIn A oldRow finalSeen
                        Instances For
                          theorem BeyondBethe.kuhnSearch_failure_certificate {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (fuel : ℕ) (remaining : List (Fin n)) (row : Fin n) (seen : Finset (Fin n)) (mate : ColumnMate n) (hroom : n < fuel + seen.card) (hresult : (kuhnSearch A fuel remaining row seen mate).mate? = none) :
                          KuhnFailureCertificate A mate row seen (kuhnSearch A fuel remaining row seen mate).seen remaining

                          The precise failure certificate returned by the executable search. The strict fuel-plus-visited inequality is preserved by every recursive descent; at zero fuel it contradicts the fact that there are only n columns.

                          theorem BeyondBethe.noPerfectMatching_of_kuhnAugment_failure {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (row : Fin n) (mate : ColumnMate n) (hsupport : IsSupportColumnMate A mate) (hunmatched : RowUnmatched mate row) (hfail : (kuhnAugment A (n + 1) row ∅ mate).mate? = none) :

                          If a full search from an unmatched row fails, its alternating-closure certificate contradicts any perfect matching. The contradiction is a finite pigeonhole argument: the root together with the old rows at the explored columns would inject into the explored columns.

                          theorem BeyondBethe.kuhnAugment_succeeds_of_hasPerfectMatching {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (row : Fin n) (mate : ColumnMate n) (hsupport : IsSupportColumnMate A mate) (hunmatched : RowUnmatched mate row) (hperfect : A.HasPerfectMatching) :
                          ∃ (mate' : ColumnMate n), (kuhnAugment A (n + 1) row ∅ mate).mate? = some mate'
                          theorem BeyondBethe.kuhnAugment_success {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (row : Fin n) (mate mate' : ColumnMate n) (hsupport : IsSupportColumnMate A mate) (hunmatched : RowUnmatched mate row) (hresult : (kuhnAugment A (n + 1) row ∅ mate).mate? = some mate') :
                          IsSupportColumnMate A mate' ∧ ∀ (r : Fin n), MatchesRow mate' r ↔ MatchesRow mate r ∨ r = row
                          def BeyondBethe.kuhnBuild {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) :
                          List (Fin n) → ColumnMate n → ColumnMate n

                          Insert the listed rows one at a time, augmenting whenever possible.

                          Equations
                          Instances For
                            def BeyondBethe.kuhnBuildWork {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) :
                            List (Fin n) → ColumnMate n → ℕ

                            The cumulative augmenting-search work for the row list, retaining the previous mate table when a search fails.

                            Equations
                            Instances For
                              theorem BeyondBethe.kuhnBuildWork_le {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (rows : List (Fin n)) (mate : ColumnMate n) :
                              kuhnBuildWork A rows mate ≤ rows.length * (n + (n + 1) * n)

                              The row-building phase has a cubic coordinate-inspection bound.

                              theorem BeyondBethe.kuhnBuild_of_hasPerfectMatching {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (rows : List (Fin n)) (mate : ColumnMate n) (hperfect : A.HasPerfectMatching) (hnodup : rows.Nodup) (hsupport : IsSupportColumnMate A mate) (hunmatched : ∀ r ∈ rows, RowUnmatched mate r) :
                              IsSupportColumnMate A (kuhnBuild A rows mate) ∧ ∀ (r : Fin n), MatchesRow (kuhnBuild A rows mate) r ↔ MatchesRow mate r ∨ r ∈ rows

                              If a perfect matching exists, inserting a duplicate-free list of initially unmatched rows succeeds at every step and adds exactly those rows.

                              theorem BeyondBethe.kuhnBuild_support {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (rows : List (Fin n)) (mate : ColumnMate n) (hnodup : rows.Nodup) (hsupport : IsSupportColumnMate A mate) (hunmatched : ∀ r ∈ rows, RowUnmatched mate r) :

                              Support and injectivity are preserved even when some augmenting searches fail.

                              The mate table produced by inserting all rows in order, starting from the empty matching.

                              Equations
                              Instances For
                                def BeyondBethe.matchedRowDecision {n : ℕ} (mate : ColumnMate n) (row : Fin n) :

                                Decide whether any column is matched to the given row.

                                Equations
                                Instances For

                                  The executable support-perfect-matching decision.

                                  Equations
                                  Instances For

                                    Coordinate inspections in matching construction plus the final n by n row-coverage check.

                                    Equations
                                    Instances For
                                      theorem BeyondBethe.hasPerfectMatching_of_all_rows_matched {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (mate : ColumnMate n) (hsupport : IsSupportColumnMate A mate) (hallRows : ∀ (row : Fin n), MatchesRow mate row) :
                                      theorem BeyondBethe.kuhnAugment_zero {n : ℕ} (A : Matrix (Fin n) (Fin n) ℚ) (row : Fin n) (seen : Finset (Fin n)) (mate : ColumnMate n) :
                                      kuhnAugment A 0 row seen mate = { mate? := none, seen := seen }