On this page
You have a list of cities and the distance between every pair. The travelling salesman problem (TSP) asks for the cheapest way of visiting all of the cities and returning to your starting point, as the University of Waterloo's TSP page defines it. It is also the clearest place to see why some problems get hard so quickly.
Why does brute force need (n−1)!/2 tours?
Brute force lists every tour, measures each one and keeps the shortest. Count the tours like this:
Fix the starting city. A loop is the same loop wherever it starts, which leaves (n−1)! orders for the other cities.
Each loop is counted twice, once in each direction, so divide by 2.
When distances are the same both ways, there are (n−1)!/2 distinct tours; Wikipedia's TSP article notes that this symmetry halves the number of possible solutions. For 5 cities that is 12 tours, for 10 cities 181,440 tours, and for 20 cities about 6.1 × 10^16 tours. To revise the counting, see our permutations and combinations formulas for Class 11.
How do you solve a 5-city example?
Put five cities on a grid: A (0, 0), B (2, 2), C (−3, 2), D (5, 0) and E (−3, −1). These are the straight-line distances, rounded to two decimals; the totals use exact distances, so your sums may differ by 0.01.
AB 2.83, AC 3.61, AD 5, AE 3.16
BC 5, BD 3.61, BE 5.83
CD 8.25, CE 3, DE 8.06
Brute force. Add up the edges of all 12 tours and the shortest is A–D–B–C–E–A: 5 + 3.61 + 5 + 3 + 3.16 = 19.77. The longest, A–B–E–D–C–A, is 28.57, almost half as long again.
Nearest neighbour. Start at A and always go to the closest unvisited city: B (2.83), then D (3.61), then E (8.06 just beats C at 8.25), then C and home. A–B–D–E–C–A = 21.10, which is 1.33 more than the best, because cheap early choices force the long D–E edge.
2-opt. Delete two edges and reconnect the pieces the other way, reversing the section between them. Keep the change if the tour gets shorter; stop when no swap helps.
From the 21.10 tour, remove D–E and C–A and add D–C and E–A. You get A–B–D–C–E–A = 20.84.
That tour now crosses itself: A–B and D–C meet at (1, 1). Remove those two edges and add A–D and B–C. You get A–D–B–C–E–A = 19.77, the shortest tour.
On large inputs, 2-opt stops at a tour that no single swap can improve, with no promise that it is the best.
How does the MST method guarantee a tour within twice the best?
This method comes with a promise, as long as distances obey the triangle inequality (a direct trip is never longer than a trip through a third city), as straight-line distances do.
Build a minimum spanning tree (MST), the cheapest set of edges connecting every city. Kruskal's rule (take the shortest edge that closes no loop) picks AB, CE, AE and BD: total 12.60.
Walk around the tree, using every edge twice: 25.19.
Skip cities already visited; by the triangle inequality a shortcut never adds length. Visiting A, B, D, E, C gives a tour of 21.10.
Why 2? Delete one edge from the best tour and you have a path through every city, a spanning tree, so the best tour is at least the MST. Your tour is at most twice the MST, so at most twice the best. For more on these trees, read how many trees n labelled vertices have, by Cayley's formula.
Nicos Christofides improved this guarantee by adding a minimum-cost matching to the tree. His worst-case analysis proves the answer is strictly less than 3/2 times the optimum. The Wikipedia article dates the method to 1976 and notes that Serdyukov found it independently.
Why is the travelling salesman problem NP-hard?
The TSP is an NP-hard problem in combinatorial optimisation. Its decision version (is there a tour no longer than L?) is NP-complete, as Wikipedia's TSP article states. Richard Karp's 1972 paper, Reducibility among Combinatorial Problems, includes the Hamilton circuit problem among its 21 NP-complete problems. To turn it into a TSP, give each road in the graph length 1 and each missing road length 2. A tour of length n exists exactly when the graph has a Hamiltonian cycle. To learn more about those cycles, read what a Hamiltonian cycle is, with Dirac's test.
NP-hard does not mean unsolvable. The Concorde program found the optimal tour through 85,900 cities from a chip-design (VLSI) application, in a computation carried out in 2005/06.
How can you practise it yourself?
Mark six points on graph paper: (n−1)!/2 gives 60 tours.
Run nearest neighbour from two starting cities and compare.
Remove crossings with 2-opt swaps.
Build the MST and check that your tour is under twice its weight.
If an idea such as the spanning-tree bound does not click, the Fermi Concept Library covers concepts in mathematics and computing, and the Fermi Problem Library has a step-by-step solver that explains the reasoning.



