Short answer: The Held-Karp algorithm solves the traveling salesman problem on n cities by dynamic programming over subsets. The cell dp[S][v] is the minimum cost of a path that starts at city 0, visits each city in the subset S exactly once, and ends at v. Each cell tries the predecessor inside S. There are 2^n subsets and n ends, and each cell looks at n predecessors, so the time is O(n² · 2^n) and the memory is O(n · 2^n). The optimum tour returns to city 0 from the best full-set cell.
What the problem actually asks
A tour permutation is n!, which is larger than 2^n · n² for every interesting n, and still exponential. The DP wins because two paths that have visited the same set and ended at the same city have the same legal continuations. Only the cheaper of those paths needs to be kept. The subset is the state, not the order in which the subset was visited.
The recurrence is only legal when the predecessor is in the subset and the last step is an edge into v. A missing edge is an infinite cost and is skipped.
What the algorithm guarantees
On a complete directed or undirected distance matrix the algorithm returns the optimal tour length, and predecessor pointers reconstruct one tour. Time is Θ(n² · 2^n) and memory is Θ(n · 2^n), which is practical only into the mid-twenties of cities and a research computation a little beyond that. The distances may be asymmetric. Negative cycles make the problem unbounded if repeated visits were allowed, and this DP never repeats a city, so a negative edge is just a cheap step. The formulation still assumes a single visit to each city.
Christofides’ algorithm approximates a metric symmetric TSP and does not replace this optimum. A branch-and-bound solver can be faster on some instances and does not improve the worst-case subset bound that Held-Karp states.
The working steps
- Number the cities 0..n−1. Set dp[{0}][0] = 0 and every other cell to infinity.
- For subsets S that contain 0, in increasing order of size, and for each v in S other than 0, set dp[S][v] to the minimum of dp[S without v][u] + dist(u, v) over u in S without v.
- The subset order can be an integer mask loop from 0 to 2^n − 1, skipping masks that do not contain city 0 or that have not yet filled their smaller submasks. Iterating masks in numeric order works if the predecessor mask, which is smaller, is already done.
- The tour cost is the minimum of dp[full set][v] + dist(v, 0) over v.
- Reconstruct by storing the argmin u at each cell and walking backward, deleting v from the mask each time.
The exponential bound is the exact algorithm. It is not a heuristic and not an approximation scheme. Metric TSP approximation belongs to Christofides or to a simpler double-tree heuristic. A path that may revisit cities is a different problem, closer to shortest paths. Held-Karp visits each city once.
Approaches that look simpler and fail
| Approach | Why it fails when the state does not remember the visited set |
|---|---|
| Keep only the last city, as Dijkstra would | Two ways to reach v may have visited different cities. The cheaper one can block a city the tour still needs. The subset is the memory. |
| Iterate masks in an order that reads a larger subset first | The predecessor is a smaller mask. If that cell is still infinity because it has not been computed, the minimum is wrong. |
| Add dist(v, 0) inside every subset | The return edge is only legal for the full set. Adding it early charges a tour that has not visited everyone. |
| Quote a polynomial bound | n² · 2^n is exponential. The win over n! is real and still exponential. |
A 3/2-approximation for metric undirected TSP is Christofides. A greedy tour improvement is 2-opt. Held-Karp is the exact subset DP.
A working example
This Python fragment holds one subset mask and one endpoint per partial tour. Four cities on a square of side 1 have optimal tour length 4.
dist = [
[0, 1, 2, 1],
[1, 0, 1, 2],
[2, 1, 0, 1],
[1, 2, 1, 0],
]
n = 4
INF = 10**9
size = 1 << n
dp = [[INF] * n for _ in range(size)]
dp[1][0] = 0
for mask in range(size):
for u in range(n):
if dp[mask][u] >= INF or not (mask & (1 << u)):
continue
for v in range(n):
if mask & (1 << v):
continue
nxt = mask | (1 << v)
dp[nxt][v] = min(dp[nxt][v], dp[mask][u] + dist[u][v])
full = size - 1
best = min(dp[full][v] + dist[v][0] for v in range(1, n))
assert best == 4
print(best)
Where it is used
Held-Karp solves a traveling-salesman instance of a few dozen cities exactly, one subset at a time. Vehicle-routing tools use it for a small exact subproblem and a heuristic for the rest. Christofides and local search are the approximations once the subset table no longer fits.
When you can drop this algorithm
Drop Held-Karp when n is large enough that 2^n tables will not fit. Use an approximation or a solver with bounds, and do not call the result optimal. Keep the DP when n is small and the tour must be optimal.
Why this stays published
We publish this so a search for Held-Karp returns the subset-and-endpoint state and the O(n² · 2^n) bound. Held-Karp solves the traveling salesman problem exactly by dynamic programming over subsets, in O(n² · 2^n) time.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
