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.

The One Thing to Remember

TSP is NP-hard. There is no known polynomial-time algorithm that always finds the optimal tour. For nn cities, brute force checks n!n! tours. For 50 cities, 50!≈3×106450! \approx 3 \times 10^{64} — more than atoms in the observable universe. This is why we use heuristics.

The Problem Formally

Given: A complete graph with nn vertices, edge weights d(i,j)d(i,j) representing distance/cost between cities ii and jj.

Find: A Hamiltonian cycle (tour visiting every vertex exactly once) with minimum total cost.

min⁡π∈permutations∑i=1n−1d(πi,πi+1)+d(πn,π1)\min_{\pi \in \text{permutations}} \sum_{i=1}^{n-1} d(\pi_i, \pi_{i+1}) + d(\pi_n, \pi_1)

Distance Matrix

The input to TSP is a distance matrix (also called cost matrix). For 5 cities:

ABCDE
A—37105
B3—496
C74—87
D1098—2
E5672—
  • The matrix is symmetric (d(A,B)=d(B,A)d(A,B) = d(B,A)) 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: d(i,j)=d(j,i)d(i,j) = d(j,i) for all pairs. Road networks are symmetric.
  • Asymmetric TSP (ATSP): d(i,j)≠d(j,i)d(i,j) \neq d(j,i) in general. Airfares, one-way streets, shipping costs are asymmetric.

The Scale of the Problem

nnTours to check (n!n!)Time at 1M/sec
512Instant
10362,8800.4 seconds
15≈1.3×1012\approx 1.3 \times 10^{12}15 days
20≈2.4×1018\approx 2.4 \times 10^{18}76,000 years
50≈3×1064\approx 3 \times 10^{64}105110^{51} 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.

  1. Start at any city
  2. Go to the nearest unvisited city
  3. Repeat until all cities visited
  4. Return to start
Nearest NeighborStep 0 / 6
Current City
Tour Path
Visited
Unvisited

Start at city A. All cities unvisited.

Current Tour Cost
0
Visited
—

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

PropertyValue
Time complexityO(n2)O(n^2)
Solution qualityWithin 12(⌈log⁡2n⌉+1)\frac{1}{2}(\lceil\log_2 n\rceil + 1) of optimal
Worst case~50% longer than optimal
Deterministic?Depends on starting city
Starting City Matters

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)

  1. Try swapping edges (B,C) and (E,D): A→B→E→C→D→A = 3+6+7+8+10 = 34. Worse.
  2. Try swapping edges (A,B) and (D,A): This creates A→D→E→C→B→A = 10+2+7+4+3 = 26. Same.
  3. 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

MethodTypeTimeQuality
Nearest NeighborConstructiveO(n2)O(n^2)~25% over optimal
Greedy (Kruskal-like)ConstructiveO(n2log⁡n)O(n^2 \log n)~20% over optimal
Savings (Clarke-Wright)ConstructiveO(n2log⁡n)O(n^2 \log n)~15% over optimal
2-OptLocal searchO(n2)O(n^2) per passImproves initial tour
3-OptLocal searchO(n3)O(n^3) per passBetter than 2-opt
Lin-KernighanVariable depthO(n2.2)O(n^{2.2})Near-optimal
ChristofidesApproximationO(n3)O(n^3)Within 1.5× optimal

Christofiles Algorithm (Guaranteed Bound)

For metric TSP (triangle inequality holds), Christofides guarantees a tour within 1.5× optimal:

  1. Build a minimum spanning tree (MST)
  2. Find a minimum weight perfect matching on the odd-degree vertices
  3. Combine them into a tour

This is the best known polynomial-time approximation ratio for metric TSP.

Properties Summary

PropertyValue
Problem classNP-hard
Exact solution timeExponential
Best exact methodBranch and Bound, Held-Karp DP
Best heuristicLin-Kernighan-Helsgaun
Approximation ratio1.5× (Christofides, metric TSP)
InapproximableNo constant ratio for general TSP (unless P=NP)
QuizWhat is the time complexity of the nearest neighbor heuristic for TSP?
QuizWhat does 2-opt do?
QuizFor metric TSP, what is the best polynomial-time approximation guarantee?