A Bit of History
In 1962, Michael Held and Richard Karp — the same Karp behind Edmonds-Karp's max-flow algorithm from a previous deep dive — published "A Dynamic Programming Approach to Sequencing Problems," showing that the Traveling Salesman Problem previewed in Part 14 could be solved exactly far faster than brute-force enumeration, by cleverly organizing subproblems around subsets of visited cities rather than sequences. The same dynamic-programming-over-subsets idea was independently discovered around the same time by Richard Bellman (of Bellman-Ford fame), and is sometimes called the Bellman-Held-Karp algorithm in recognition of that overlap.
Working Principle
Brute force tries all \((n-1)!\) possible orderings of the remaining \(n-1\) cities after fixing a start. Held-Karp's insight: the cost of the cheapest way to visit a specific set of cities and end at a specific city does not depend on the order in which the earlier cities were visited — only on which set was visited and where the path currently ends. That means the state space is not "all orderings" but merely "all (subset, endpoint) pairs" — vastly smaller.
Define \(C(S, j)\) as the minimum cost of a path starting at city 0, visiting exactly the set of cities \(S\) (with \(0 \in S\)), and ending at city \(j \in S\). The recurrence:
$$C(S, j) = \min_{k \in S, k \neq j} \Big[ C(S \setminus \{j\}, k) + d(k, j) \Big]$$
with base case \(C(\{0\}, 0) = 0\). The final answer is \(\min_{j \neq 0} \big[C(\{0,\ldots,n-1\}, j) + d(j, 0)\big]\), closing the tour back to the start.
Key Insight
Representing each subset \(S\) as a bitmask (an integer whose bits indicate which cities are included) turns the recurrence into simple, fast integer operations — an extremely common and powerful pattern for dynamic programming over subsets, used far beyond the TSP.
Worked Example
For 4 cities \(\{0,1,2,3\}\) with a symmetric distance matrix, the algorithm builds up \(C(S,j)\) for increasingly large subsets \(S\): first all 2-element subsets containing city 0 (i.e., \(\{0,1\}\), \(\{0,2\}\), \(\{0,3\}\)), then all 3-element subsets, then finally the full 4-element set. At each stage, every new value reuses previously computed smaller-subset values — never recomputing a path cost from scratch. With \(n=4\), this means at most \(2^4 \times 4 = 64\) subproblems total, versus \(3! = 6\) full brute-force tours for this tiny example — the savings become dramatic only as \(n\) grows.
Correctness
The recurrence is correct by a straightforward optimal-substructure argument: any optimal path visiting set \(S\) and ending at \(j\) must have arrived at \(j\) from some other city \(k \in S\), having previously visited exactly \(S \setminus \{j\}\) and ended at \(k\) — and that sub-path must itself be optimal (if a cheaper way to visit \(S \setminus \{j\}\) ending at \(k\) existed, splicing it in would produce a cheaper overall path, a contradiction). Trying all valid choices of \(k\) and keeping the minimum is therefore guaranteed to find the true optimum.
Complexity Analysis
There are \(O(2^n)\) subsets and \(n\) choices of endpoint \(j\), and each recurrence evaluation considers up to \(n\) choices of \(k\):
$$\text{Time: } O(n^2 \cdot 2^n) \qquad \text{Space: } O(n \cdot 2^n)$$
This is exponential — still impractical for very large instances — but it is a dramatic improvement over brute force's \(O(n!)\), and remains, over 60 years later, essentially the best known exact algorithm for general TSP instances.
Implementation
Real-World Applications
Exact Route Optimization for Small Fleets
Delivery and field-service companies with a small number of daily stops (typically under 20) can afford the exponential cost of Held-Karp to guarantee a provably optimal route — unlike heuristic approaches from Part 14, which only guarantee near-optimality. This exact-vs-approximate tradeoff decision (small guaranteed-optimal instances vs. large heuristic ones) recurs constantly in logistics software design.
Exercises
- Trace through the Held-Karp recurrence by hand for a 4-city instance, filling in the \(C(S,j)\) table for all subsets containing city 0.
- Explain why representing subsets as bitmasks (rather than, say, Python sets or lists) is essential for the algorithm's practical performance, not just a stylistic choice.
- Compare the number of subproblems Held-Karp solves for \(n=15\) against the number of tours brute force would enumerate, and comment on how quickly the gap grows.
- Challenge: Modify the implementation to also reconstruct and print the optimal tour itself, not just its cost (hint: track which \(k\) achieved the minimum at each step).
Limitations
Still Exponential
\(O(n^2 2^n)\) is only practical up to roughly \(n \approx 20\)-\(25\) cities on typical hardware — well past that, exact solving becomes infeasible regardless of how the subproblems are organized, and the heuristic and approximation algorithms from Part 14 (nearest-neighbor, 2-opt, and the upcoming Christofides deep dive) become the only realistic options.