Traveling Salesperson Problem (TSP)
The most famous NP-hard optimization problem — visit every city exactly once at minimum cost
The Simplest Explanation
You have a list of cities and distances between every pair. Visit each city exactly once and return home. Find the shortest possible tour.
That's the entire problem. It's trivial to state, brutally hard to solve optimally for large instances, and shows up everywhere — logistics, circuit board drilling, DNA sequencing.
TSP is NP-hard. There is no known polynomial-time algorithm that always finds the optimal tour. For cities, brute force checks tours. For 50 cities, — more than atoms in the observable universe. This is why we use heuristics.
The Problem Formally
Given: A complete graph with vertices, edge weights representing distance/cost between cities and .
Find: A Hamiltonian cycle (tour visiting every vertex exactly once) with minimum total cost.
Distance Matrix
The input to TSP is a distance matrix (also called cost matrix). For 5 cities:
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | — | 3 | 7 | 10 | 5 |
| B | 3 | — | 4 | 9 | 6 |
| C | 7 | 4 | — | 8 | 7 |
| D | 10 | 9 | 8 | — | 2 |
| E | 5 | 6 | 7 | 2 | — |
- The matrix is symmetric () for standard TSP
- Diagonal is meaningless (distance from a city to itself)
- Read: "From A to B costs 3, from A to C costs 7, from D to E costs 2"
Symmetric vs Asymmetric TSP
- Symmetric TSP: for all pairs. Road networks are symmetric.
- Asymmetric TSP (ATSP): in general. Airfares, one-way streets, shipping costs are asymmetric.
The Scale of the Problem
| Tours to check () | Time at 1M/sec | |
|---|---|---|
| 5 | 12 | Instant |
| 10 | 362,880 | 0.4 seconds |
| 15 | 15 days | |
| 20 | 76,000 years | |
| 50 | years |
This is why exact methods (brute force, BnB, dynamic programming) only work for small instances. For real-world problems with hundreds or thousands of cities, heuristic methods are essential.
Nearest Neighbor Heuristic
The simplest constructive heuristic: always go to the closest unvisited city.
- Start at any city
- Go to the nearest unvisited city
- Repeat until all cities visited
- Return to start
Start at city A. All cities unvisited.
Nearest Neighbor Result
Starting from A: A→B→C→E→D→A = 3+4+7+2+10 = 26
But the optimal tour is: A→E→D→C→B→A = 5+2+8+4+3 = 22
Nearest neighbor missed the optimal by 4 (18% over optimal). This is typical — nearest neighbor can produce tours up to 50% longer than optimal in the worst case.
Properties of Nearest Neighbor
| Property | Value |
|---|---|
| Time complexity | |
| Solution quality | Within of optimal |
| Worst case | ~50% longer than optimal |
| Deterministic? | Depends on starting city |
Running nearest neighbor from each city and taking the best result gives a much better solution. For 5 cities, running from E gives: E→A→B→C→D→E = 5+3+4+8+9 = 29 (worse!). From D: D→E→A→B→C→D = 2+5+3+4+8 = 22 (optimal!). Always try multiple starting cities.
2-Opt Improvement
Nearest neighbor gives an initial tour. 2-opt improves it by swapping edges.
The Idea
Remove two edges from the tour, reconnect the two resulting paths in the other possible way. If the new tour is shorter, keep it.
Before: ...→ A → B → ... → C → D →...
Swap edges A-B and C-D:
After: ...→ A → C → ... → B → D →...
2-Opt on Our Example
Starting tour: A→B→C→E→D→A (cost 26)
- Try swapping edges (B,C) and (E,D): A→B→E→C→D→A = 3+6+7+8+10 = 34. Worse.
- Try swapping edges (A,B) and (D,A): This creates A→D→E→C→B→A = 10+2+7+4+3 = 26. Same.
- Try swapping edges (C,E) and (D,A): A→B→C→D→E→A = 3+4+8+2+5 = 22. Better! Found the optimal.
2-opt is a local search — it finds a locally optimal tour where no 2-edge swap improves it. The result depends on the starting tour.
Other TSP Heuristics
| Method | Type | Time | Quality |
|---|---|---|---|
| Nearest Neighbor | Constructive | ~25% over optimal | |
| Greedy (Kruskal-like) | Constructive | ~20% over optimal | |
| Savings (Clarke-Wright) | Constructive | ~15% over optimal | |
| 2-Opt | Local search | per pass | Improves initial tour |
| 3-Opt | Local search | per pass | Better than 2-opt |
| Lin-Kernighan | Variable depth | Near-optimal | |
| Christofides | Approximation | Within 1.5× optimal |
Christofiles Algorithm (Guaranteed Bound)
For metric TSP (triangle inequality holds), Christofides guarantees a tour within 1.5× optimal:
- Build a minimum spanning tree (MST)
- Find a minimum weight perfect matching on the odd-degree vertices
- Combine them into a tour
This is the best known polynomial-time approximation ratio for metric TSP.
Properties Summary
| Property | Value |
|---|---|
| Problem class | NP-hard |
| Exact solution time | Exponential |
| Best exact method | Branch and Bound, Held-Karp DP |
| Best heuristic | Lin-Kernighan-Helsgaun |
| Approximation ratio | 1.5× (Christofides, metric TSP) |
| Inapproximable | No constant ratio for general TSP (unless P=NP) |