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 #
Cslib.Algorithms.Lean.TSP.IsWalk:k-vertex walksw : Fin k → Fin ninadj.Cslib.Algorithms.Lean.TSP.walksIn,walksFrom: walks inside a vertex subsetS.Cslib.Algorithms.Lean.TSP.hamPaths: Hamiltonian pathsw : Fin n → Fin ninadj.Cslib.Algorithms.Lean.TSP.walkCountFrom,totalWalkCount: pure layered DP walk counts.Cslib.Algorithms.Lean.TSP.countHamPaths,decideHamPath: pure inclusion–exclusion counter and decider.
Main results #
Cslib.Algorithms.Lean.TSP.card_hamPaths_eq_sum_walksIn: Karp–Bax inclusion–exclusion formula for Hamiltonian paths.Cslib.Algorithms.Lean.TSP.card_walksFrom,card_walksIn_eq_totalWalkCount: correctness of the layered walk-count recurrence.Cslib.Algorithms.Lean.TSP.countHamPaths_eq,Cslib.Algorithms.Lean.TSP.card_hamPaths_eq_toNat_countHamPaths: correctness ofcountHamPaths.Cslib.Algorithms.Lean.TSP.decideHamPath_eq_true_iff: decision corollary.
References #
- E. T. Bax, Inclusion and exclusion algorithm for the Hamiltonian path problem
- F. V. Fomin and D. Kratsch, Exact Exponential Algorithms, Section 4.1 (Theorem 4.2) and Section 4.2.2 (Theorem 4.5 and Eq. (4.4), pp. 56–57)
- R. M. Karp, Dynamic programming meets the principle of inclusion and exclusion
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 #
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.
walkCountFrom counts (k + 1)-vertex walks inside S starting at u.
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).
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.
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.