Algorithms / Travelling salesman problem

Travelling salesman problem

Find the shortest round trip through every city. Easy to state, easy for five cities, hopeless by brute force for fifty: why that is, and how good tours are found anyway.

Intermediate7 steps~7 min
Home — city A, where the trip starts and ends
City — must be visited exactly once
Road — its number is the length in km
Tour — a round trip through all the cities
// THE SHORT VERSION

In 30 seconds

A seller must visit a list of cities once each and return home by the shortest route. With a handful of cities you can try every order.

But the number of orders grows as a factorial: each added city multiplies it. No known method finds the guaranteed best tour quickly for every map; the problem is NP-hard.

So in practice we use heuristics, quick rules such as “go to the nearest city” and “uncross two roads”, and approximation algorithms that come with a proven limit on how far from the best they can be.

Travelling salesman problem, step by step

STEPS01 / 07

Distances here are straight lines, rounded to whole km. Smarter exact methods than trying every tour exist and have solved maps with tens of thousands of cities, but all still blow up in the worst case.

STEP 01 / 07 One round trip, five cities
THE MATH
visit every city once return to the start minimise total distance
  1. Step 1: One round trip, five cities. A seller starts at A, must visit B, C, D and E exactly once each, and then return home. The numbers are road lengths in km. Which order of visits makes the whole round trip shortest? This is the travelling salesman problem, and it is much harder than it looks.
  2. Step 2: Try every round trip. The sure method: list every possible round trip and measure it. From A there are 4 choices for the first stop, 3 for the next, then 2, then 1: 24 orders. A trip and its reverse have the same length, which leaves 12 different trips. The shortest is A, B, D, E, C at 165 km; the longest is 263 km.
  3. Step 3: The count explodes. Each extra city multiplies the number of trips. With n cities there are (n − 1)!/2 of them: the factorial, written with an exclamation mark, means multiplying all the whole numbers up to that one. At a billion trips per second, 15 cities take 44 seconds, 20 take about two years, and 25 take about ten million years.
  4. Step 4: Why it is called NP-hard. Checking a claim such as “this tour is under 200 km” is quick: add five numbers. Being sure that no shorter tour exists is the hard part. The problem is NP-hard: a fast exact method for it would also give fast methods for every problem whose answers are quick to check. Nobody has found one, or proved that none can exist.
  5. Step 5: A quick guess: go nearest. If the best tour is out of reach, settle for a good one. A heuristic is a rule of thumb that is fast but promises nothing. The nearest-neighbour rule always moves to the closest city not yet visited. Here it gives 191 km, about 16% longer than the best: grabbing short roads early leaves a long, crossing trip home.
  6. Step 6: Improve it: uncross. A second heuristic repairs a tour instead of building one. Take out two roads and reconnect the loose ends the other way; keep the change if the tour got shorter. This is called 2-opt. Swapping the crossing pair saves 26 km and here happens to reach the best tour. In general it stops at a good tour, not always the best.
  7. Step 7: Fast methods with a promise. An approximation algorithm is a fast method that comes with a proof of how bad it can get. When a detour is never shorter than the direct road, Christofides’ algorithm always lands within 1.5 times the best tour. In practice, good heuristics usually get within a few percent, which is what route planners rely on.
VALUES SO FAR
cities5 (A to E)input
trips····count
best····result
n = 20····count
class····theory
nearest····heuristic
2-opt····heuristic
promise····approx.

Spotted a mistake on this page?