Documentation

CslibFrontier.Algorithms.Lean.TSP.HeldKarp

The Bellman–Held–Karp algorithm for the traveling salesman problem #

The Bellman–Held–Karp dynamic program [Bel62] [HK62] ([FK10], Section 1.2) computes an optimal traveling salesman tour on n + 1 cities by dynamic programming over subsets of {1, …, n}, instead of examining the n! tours one by one.

Machine-level running-time certificates are proved separately in CslibFrontier.Algorithms.WordRAM.HeldKarp (O(n² · 2ⁿ) standard-basis word-RAM instructions on word-encoded distance matrices) and CslibFrontier.Computability.Complexity.Problems.TSP.HeldKarp (2ⁿ · poly(|enc x|) steps on binary multitape Turing machines for TSP and HamCycle).

Model #

The cities are Fin (n + 1), and d : Fin (n + 1) → Fin (n + 1) → W is an arbitrary cost function with values in a linearly ordered additive commutative monoid W: costs need not be symmetric, nonnegative or metric, and W may be ℕ, ℤ, or WithTop ℕ for missing edges. Every tour starts and ends at city 0, which loses nothing since a closed tour can be rotated. A tour is an ordering L of the other cities 1, …, n, given as a list of Fin n (city i.succ for i : Fin n) that is a permutation of List.finRange n, and its cost is walkCost d 0 (L.map Fin.succ) 0, the sum of d along 0, L, 0. For n = 0 the only tour is [], of cost d 0 0.

Algorithm #

For a set S of cities among 1, …, n and a city t, the table holds the least cost of a walk that leaves 0, visits the cities of S exactly once in some order, and then moves to t. Bellman's principle (isLeast_walkCosts_of_forall) computes this entry from the entries for the sets S.erase i: the walk visits some i ∈ S last. heldKarp fills the table by increasing |S| (HeldKarp.table) and returns the entry for all cities and t = 0.

The entries are indexed by the set S of cities visited before the target. For a target t = i.succ with i ∉ S this is [HK62]'s C(S ∪ {t}, t). The table also fills the entries (S, 0) for every S (HeldKarp.targets always contains city 0): such an entry is the least cost of a walk from 0 through S back to 0, which is not a Held–Karp value unless S is every city. Only the final entry (univ, 0) is read, and it is the optimal tour; Held and Karp compute it by a separate closing minimization.

Main definitions #

Main results #

References #

Minimum folds #

def Cslib.Algorithms.Lean.TSP.HeldKarp.minFold {α : Type u_1} {W : Type u_2} [LinearOrder W] (g : α → W) :
W → List α → W

Fold min over the values of g on l, starting from acc.

Walks through subsets #

def Cslib.Algorithms.Lean.TSP.walkCosts {W : Type u_2} {n : ℕ} [Add W] (d : Fin (n + 1) → Fin (n + 1) → W) (S : Finset (Fin n)) (t : Fin (n + 1)) :
Set W

The costs of the walks that start at city 0, visit each city of S exactly once, in some order, and then move to t. The cities 1, …, n are the successors i.succ of i : Fin n.

theorem Cslib.Algorithms.Lean.TSP.isLeast_walkCosts_empty {W : Type u_2} {n : ℕ} [Add W] [Preorder W] (d : Fin (n + 1) → Fin (n + 1) → W) (t : Fin (n + 1)) :
IsLeast (walkCosts d ∅ t) (d 0 t)

The only walk through no cities is the direct step.

theorem Cslib.Algorithms.Lean.TSP.isLeast_walkCosts_of_forall {W : Type u_2} {n : ℕ} [AddCommMonoid W] [LinearOrder W] [IsOrderedAddMonoid W] {d : Fin (n + 1) → Fin (n + 1) → W} {S : Finset (Fin n)} {t : Fin (n + 1)} {f : Fin n → W} {v : W} (hf : ∀ i ∈ S, IsLeast (walkCosts d (S.erase i) i.succ) (f i)) (hle : ∀ i ∈ S, v ≤ f i + d i.succ t) (hmem : ∃ i ∈ S, v = f i + d i.succ t) :
IsLeast (walkCosts d S t) v

Bellman's principle for walks through subsets [Bel62] [HK62]. If f i is the least cost of a walk from 0 through S.erase i to i.succ, for every i ∈ S, and v is the least value of f i + d i.succ t over i ∈ S, then v is the least cost of a walk from 0 through S to t.

The dynamic program #

@[reducible, inline]

A Held–Karp table: an entry for each set S of the cities 1, …, n and target t.

def Cslib.Algorithms.Lean.TSP.HeldKarp.entry {W : Type u_2} {n : ℕ} [Add W] [LinearOrder W] (d : Fin (n + 1) → Fin (n + 1) → W) (T : Table n W) (S : Finset (Fin n)) (t : Fin (n + 1)) :
List (Fin n) → W

The table entry for (S, t), where l lists the cities of S: d 0 t when S is empty, and otherwise the least of T (S.erase i, i.succ) + d i.succ t over i ∈ l.

The targets of the entries for S: city 0 and the cities 1, …, n outside S. Only the entry (univ, 0) among those for city 0 is read by heldKarp.

def Cslib.Algorithms.Lean.TSP.HeldKarp.fill {W : Type u_2} {n : ℕ} [Add W] [LinearOrder W] (d : Fin (n + 1) → Fin (n + 1) → W) (S : Finset (Fin n)) (l : List (Fin n)) :
Table n W → List (Fin (n + 1)) → Table n W

Fill the entries (S, t) for the targets t ∈ ts, where l lists the cities of S.

def Cslib.Algorithms.Lean.TSP.HeldKarp.layer {W : Type u_2} {n : ℕ} [Add W] [LinearOrder W] (d : Fin (n + 1) → Fin (n + 1) → W) :
Table n W → List (List (Fin n)) → Table n W

Fill the entries of every set listed in ls, for all their targets.

def Cslib.Algorithms.Lean.TSP.HeldKarp.table {W : Type u_2} {n : ℕ} [Add W] [LinearOrder W] (d : Fin (n + 1) → Fin (n + 1) → W) :
ℕ → Table n W

The table after the layers of the sets of sizes 1, …, k, each enumerated by List.sublistsLen. The initial table holds d 0 t, the entries for the empty set.

def Cslib.Algorithms.Lean.TSP.heldKarp {W : Type u_2} [Add W] [LinearOrder W] {n : ℕ} (d : Fin (n + 1) → Fin (n + 1) → W) :
W

The Bellman–Held–Karp algorithm [Bel62] [HK62] for the traveling salesman problem on the cities 0, 1, …, n with costs d: fill the table of least walk costs through each set of the cities 1, …, n, by increasing size, and read off the entry for all of them followed by the return to 0.

Correctness #

theorem Cslib.Algorithms.Lean.TSP.HeldKarp.isLeast_table {W : Type u_2} {n : ℕ} [AddCommMonoid W] [LinearOrder W] [IsOrderedAddMonoid W] (d : Fin (n + 1) → Fin (n + 1) → W) (k : ℕ) (S : Finset (Fin n)) (hS : S.card ≤ k) {t : Fin (n + 1)} (ht : t ∈ targets S) :
IsLeast (walkCosts d S t) (table d k (S, t))

The table invariant. After the layers 1, …, k, the entry (S, t) is the least cost of a walk from 0 through S to t, for every set S of at most k cities and target t among 0 and the cities outside S.

Correctness of the Bellman–Held–Karp algorithm [HK62]. heldKarp d is the least cost of a closed tour that starts at city 0, visits each of the cities 1, …, n exactly once and returns to 0.

The Bellman–Held–Karp algorithm agrees with brute force. heldKarp d is the least cost of the n! tours 0, L, 0, over all permutations L of the cities 1, …, n.