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 #
Cslib.Algorithms.Lean.TSP.walkCosts: the costs of the walks from0throughStot.Cslib.Algorithms.Lean.TSP.heldKarp: the Bellman–Held–Karp algorithm.Cslib.Algorithms.Lean.TSP.HeldKarp.entry,HeldKarp.fill,HeldKarp.layer,HeldKarp.table: a table entry, the entries for one set, one layer, and the table.
Main results #
isLeast_walkCosts_of_forall: Bellman's principle for walks through subsets.HeldKarp.isLeast_table: table invariant after filling subsets of cardinality at mostk.isLeast_heldKarp:heldKarp dis the least cost of a tour.isLeast_heldKarp_permutations: the same against brute force, over all permutations of the cities1, …, n.
References #
Minimum folds #
Fold min over the values of g on l, starting from acc.
Walks through subsets #
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.
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 #
A Held–Karp table: an entry for each set S of the cities 1, …, n and target t.
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 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.
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 #
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.