Traveling Salesman Problem
DSA · Graphs
Theory
The Traveling Salesman Problem (TSP) asks for the shortest route that visits every city exactly once and returns to the start. It is the classic NP-hard problem: the number of possible tours grows factorially, so 20 cities already have about 6 x 10^16 routes. Exact methods include brute force over permutations (O(n!)) and the Held-Karp dynamic programming algorithm, which stores the best cost for each subset of visited cities ending at each city, giving O(n^2 * 2^n). For real logistics with hundreds of stops, heuristics are used instead: nearest neighbour (always go to the closest unvisited city), 2-opt improvements that remove crossing edges, or metaheuristics such as simulated annealing. These find routes close to optimal quickly.
Why we use it
TSP models delivery routes, drilling paths on circuit boards and sequencing tasks. It is also the standard example for explaining NP-hardness and the trade-off between exact and approximate algorithms.
When to use it
Use exact DP for up to about 15-20 points. Use heuristics for larger instances where a near-optimal route is good enough.
How it works
- Cities with pairwise distances
- Few cities (<= 15)? Yes: Exact: Held-Karp DP over subsets. No: Heuristic: nearest neighbour + 2-opt.
- Compute the tour cost
- Return best tour found
Syntax
Brute force O(n!)
Held-Karp DP O(n^2 * 2^n)
Nearest neighbour O(n^2) (approximate)Example
from itertools import permutations
from functools import lru_cache
d = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0],
]
n = len(d)
# 1. Brute force
best = min(permutations(range(1, n)), key=lambda p: sum(d[a][b] for a, b in zip((0,) + p, p + (0,))))
cost = sum(d[a][b] for a, b in zip((0,) + best, best + (0,)))
print("Brute force:", (0,) + best + (0,), cost)
# 2. Held-Karp dynamic programming
@lru_cache(maxsize=None)
def hk(mask, last):
if mask == (1 << n) - 1:
return d[last][0]
return min(d[last][j] + hk(mask | 1 << j, j) for j in range(n) if not mask & 1 << j)
print("Held-Karp:", hk(1, 0))
# 3. Nearest neighbour heuristic
tour, seen = [0], {0}
while len(tour) < n:
nxt = min((j for j in range(n) if j not in seen), key=lambda j: d[tour[-1]][j])
tour.append(nxt); seen.add(nxt)
print("Nearest neighbour:", tour + [0], sum(d[a][b] for a, b in zip(tour, tour[1:] + [0])))Key points
- NP-hard: no known polynomial exact algorithm
- Held-Karp DP is O(n^2 * 2^n), practical up to ~20 cities
- Heuristics give good routes fast but not guaranteed optimal
- Classic example of exact vs approximate trade-offs