Documentation

CslibFrontier.Algorithms.Lean.TSP.HamiltonianCount

Inclusion–exclusion counting of Hamiltonian paths #

Karp's inclusion–exclusion algorithm [Kar82] [Bax93] ([FK10], Section 4.2.2, Theorem 4.5 and Eq. (4.4), printed pp. 56–57 / PDF pp. 68–69) counts the Hamiltonian paths of a directed graph on Fin n in O(2ⁿ · n³) operations while keeping only one length-n walk-count vector at a time.

Mathematical identity and source adaptation #

A sequence w : Fin n → Fin n of n vertices is a Hamiltonian path of adj when consecutive vertices are joined by arcs (IsWalk adj w) and w is injective. Specializing the general inclusion–exclusion identity for injective self-maps (Finset.card_filter_injective_eq_sum_powerset in CslibFrontier.Foundations.Combinatorics.InclusionExclusion; cf. [FK10], Section 4.1, Theorem 4.2) to IsWalk adj expresses the number of Hamiltonian paths as the alternating sum (#(hamPaths adj) : ℤ) = ∑ S : Finset (Fin n), (-1 : ℤ) ^ (n - #S) * #(walksIn adj S n), where walksIn adj S k is the set of k-vertex walks of adj whose vertices all lie in S (card_hamPaths_eq_sum_walksIn).

In [FK10], Section 4.2.2, Theorem 4.5 and Eq. (4.4), the algorithm fixes start and end vertices s, t and counts (s, t)-walks on n + 2 vertices. Here hamPaths adj and walksIn adj range over all start and end vertices in Fin n, so totalWalkCount adj S sums the (n - 1)-step walk counts across all starting vertices u : Fin n.

Pure specification and word-RAM realization #

Subsets S ⊆ Fin n are enumerated by their bitmasks k : Fin (2 ^ n) via Fin.foldl (2 ^ n) (using Cslib.Algorithms.Lean.Yates.maskSet). For each k, the number of n-vertex walks inside maskSet n k is computed by n - 1 rounds of restricted vector–adjacency multiplication (walkCountFrom, rounds), summed across starting vertices, and accumulated with sign (-1) ^ (n - #(maskSet n k)) (countHamPaths).

For the checked O((n³ + 1) · 2 ^ n) instruction-count realization on a width-blind standard-basis word RAM (reading n² + 1 flat input words and allocating 3 * n working words for an in-place binary odometer at word widths fitting the n^n-scale walk counts), see CslibFrontier.Algorithms.WordRAM.HamiltonianCount.

Main definitions #

Main results #

References #

Walks and Hamiltonian paths on Fin n #

def Cslib.Algorithms.Lean.TSP.IsWalk {n : ℕ} (adj : Fin n → Fin n → Bool) {k : ℕ} (w : Fin k → Fin n) :

A sequence w : Fin k → Fin n of k vertices is a walk in adj if every consecutive pair of vertices is joined by an arc.

Instances For
    @[instance_reducible]
    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.isWalk_zero {n : ℕ} (adj : Fin n → Fin n → Bool) (w : Fin 0 → Fin n) :
    IsWalk adj w
    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.isWalk_one {n : ℕ} (adj : Fin n → Fin n → Bool) (w : Fin 1 → Fin n) :
    IsWalk adj w
    theorem Cslib.Algorithms.Lean.TSP.isWalk_succ_succ {n : ℕ} (adj : Fin n → Fin n → Bool) {k : ℕ} (w : Fin (k + 2) → Fin n) :
    IsWalk adj w ↔ adj (w 0) (w 1) = true ∧ IsWalk adj (w ∘ Fin.succ)
    def Cslib.Algorithms.Lean.TSP.walksIn {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) (k : ℕ) :
    Finset (Fin k → Fin n)

    The k-vertex walks in adj whose vertices all lie in S.

    def Cslib.Algorithms.Lean.TSP.walksFrom {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) (k : ℕ) (u : Fin n) :
    Finset (Fin (k + 1) → Fin n)

    The (k + 1)-vertex walks in adj starting at u whose vertices all lie in S.

    def Cslib.Algorithms.Lean.TSP.hamPaths {n : ℕ} (adj : Fin n → Fin n → Bool) :
    Finset (Fin n → Fin n)

    The Hamiltonian paths of adj: n-vertex walks visiting every vertex of Fin n once.

    theorem Cslib.Algorithms.Lean.TSP.mem_hamPaths_iff_ofFn {n : ℕ} (adj : Fin n → Fin n → Bool) (w : Fin n → Fin n) :
    w ∈ hamPaths adj ↔ List.IsChain (fun (u v : Fin n) => adj u v = true) (List.ofFn w) ∧ (List.ofFn w).Nodup

    A sequence w : Fin n → Fin n is in hamPaths adj iff List.ofFn w is a duplicate-free chain of arcs of adj.

    theorem Cslib.Algorithms.Lean.TSP.card_hamPaths_eq_sum_walksIn {n : ℕ} (adj : Fin n → Fin n → Bool) :
    ↑(hamPaths adj).card = ∑ S : Finset (Fin n), (-1) ^ (n - S.card) * ↑(walksIn adj S n).card

    Karp–Bax inclusion–exclusion formula for Hamiltonian paths [Kar82] [Bax93] ([FK10], Section 4.2.2, Eq. (4.4)): the number of Hamiltonian paths of adj on Fin n equals the alternating sum over S ⊆ Fin n of the number of n-vertex walks inside S.

    Layered dynamic programming for walk counts #

    def Cslib.Algorithms.Lean.TSP.walkCountFrom {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) :
    ℕ → Fin n → ℕ

    The number of (k + 1)-vertex walks in adj starting at u whose vertices all lie in S, computed by k rounds of restricted vector–adjacency multiplication.

    def Cslib.Algorithms.Lean.TSP.totalWalkCount {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) :

    The total number of n-vertex walks in adj whose vertices all lie in S.

    theorem Cslib.Algorithms.Lean.TSP.card_walksFrom_zero {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) (u : Fin n) :
    (walksFrom adj S 0 u).card = walkCountFrom adj S 0 u

    Base case: the only 1-vertex walk starting at u is the constant map fun _ => u, which lies in S iff u ∈ S.

    theorem Cslib.Algorithms.Lean.TSP.card_walksFrom_succ {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) (k : ℕ) (u : Fin n) :
    (walksFrom adj S (k + 1) u).card = ∑ v : Fin n, if u ∈ S ∧ adj u v = true then (walksFrom adj S k v).card else 0

    Inductive step: partitioning (k + 2)-vertex walks starting at u by their second vertex w 1 (Finset.card_eq_sum_card_fiberwise) and removing u bijects each fiber with (k + 1)-vertex walks starting at an out-neighbour v of u.

    theorem Cslib.Algorithms.Lean.TSP.card_walksFrom {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) (k : ℕ) (u : Fin n) :
    (walksFrom adj S k u).card = walkCountFrom adj S k u

    walkCountFrom counts (k + 1)-vertex walks inside S starting at u.

    theorem Cslib.Algorithms.Lean.TSP.card_walksIn_succ {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) (k : ℕ) :
    (walksIn adj S (k + 1)).card = ∑ u : Fin n, walkCountFrom adj S k u

    Partitioning (k + 1)-vertex walks inside S by their starting vertex w 0.

    theorem Cslib.Algorithms.Lean.TSP.card_walksIn_eq_totalWalkCount {n : ℕ} (adj : Fin n → Fin n → Bool) (S : Finset (Fin n)) :
    (walksIn adj S n).card = totalWalkCount adj S

    totalWalkCount counts n-vertex walks inside S.

    Pure inclusion–exclusion counter #

    Number of set bits of k in 0, …, n - 1, equal to #(maskSet n k).

    Initial walk-count vector for bitmask k: 1 at vertices in maskSet n k, 0 outside.

    def Cslib.Algorithms.Lean.TSP.stepCell {n : ℕ} (adj : Fin n → Fin n → Bool) (k : ℕ) (dp : Vector ℕ n) (u : Fin n) :

    Compute the next walk-count entry at vertex u.

    def Cslib.Algorithms.Lean.TSP.stepVec {n : ℕ} (adj : Fin n → Fin n → Bool) (k : ℕ) (dp : Vector ℕ n) :

    One round of restricted vector–adjacency multiplication.

    def Cslib.Algorithms.Lean.TSP.rounds {n : ℕ} (adj : Fin n → Fin n → Bool) (k r : ℕ) :

    r rounds of restricted vector–adjacency multiplication starting from initVec k.

    Sum the entries of a length-n vector.

    def Cslib.Algorithms.Lean.TSP.subsetWalks {n : ℕ} (adj : Fin n → Fin n → Bool) (k : ℕ) :

    Count the n-vertex walks in adj whose vertices all lie in maskSet n k.

    Karp–Bax inclusion–exclusion Hamiltonian path counter [Kar82] [Bax93] ([FK10], Section 4.2.2, Theorem 4.5): enumerates all 2 ^ n subsets S = maskSet n k via Fin.foldl (2 ^ n), computes #(walksIn adj S n) by n - 1 rounds of restricted vector–adjacency multiplication, and accumulates the signed sum in ℤ.

    Decide whether adj has a Hamiltonian path by testing whether countHamPaths adj > 0.

    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.stepCell_eq {n : ℕ} (adj : Fin n → Fin n → Bool) (k : ℕ) (dp : Vector ℕ n) (u : Fin n) :
    stepCell adj k dp u = ∑ v : Fin n, if u ∈ Yates.maskSet n k ∧ adj u v = true then dp[↑v] else 0
    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.stepVec_eq {n : ℕ} (adj : Fin n → Fin n → Bool) (k : ℕ) (dp : Vector ℕ n) :
    stepVec adj k dp = Vector.ofFn fun (u : Fin n) => ∑ v : Fin n, if u ∈ Yates.maskSet n k ∧ adj u v = true then dp[↑v] else 0
    theorem Cslib.Algorithms.Lean.TSP.rounds_eq {n : ℕ} (adj : Fin n → Fin n → Bool) (k r : ℕ) :
    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.sumVec_eq {n : ℕ} (dp : Vector ℕ n) :
    sumVec dp = ∑ u : Fin n, dp[↑u]
    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.subsetWalks_eq {n : ℕ} (adj : Fin n → Fin n → Bool) (k : ℕ) :
    theorem Cslib.Algorithms.Lean.TSP.fin_foldl_eq_sum {M : Type u_1} [AddCommMonoid M] (m : ℕ) (f : Fin m → M) :
    Fin.foldl m (fun (acc : M) (i : Fin m) => acc + f i) 0 = ∑ i : Fin m, f i

    Accumulating + f i over Fin.foldl m starting from 0 equals the finite sum ∑ i, f i.

    @[simp]
    theorem Cslib.Algorithms.Lean.TSP.countHamPaths_eq {n : ℕ} (adj : Fin n → Fin n → Bool) :

    Correctness of countHamPaths [Kar82] [Bax93]: countHamPaths adj equals the number of Hamiltonian paths of adj.

    #(hamPaths adj) as the ℕ-valued projection of countHamPaths.

    Decision corollary: decideHamPath adj returns true iff adj has a Hamiltonian path.