The Travelling Salesman Problem
A salesperson has to visit every town on a list once and return home, travelling as short a distance as possible. This is the travelling salesman problem (TSP): find the Hamiltonian cycle of least weight. It sounds simple, but no one knows a fast method that always finds the best answer for large graphs. Instead, you’ll learn to trap the answer between two numbers: an upper bound and a lower bound.
Key ideas
Section titled “Key ideas”Why the TSP is hard
Section titled “Why the TSP is hard”In a complete graph with vertices, the number of different Hamiltonian cycles (ignoring the starting point and direction) is
That’s cycles for towns, but nearly million for towns and about for . Checking them all quickly becomes impossible, even for a computer. That’s why we use algorithms that give good bounds.
Classical and practical problems
Section titled “Classical and practical problems”- In the classical TSP, the graph is complete and you visit each vertex exactly once.
- In the practical TSP, the graph may not be complete, and you’re allowed to pass through a vertex more than once (a delivery van can drive through a town twice).
To turn a practical problem into a classical one, make a table of least distances: for every pair of vertices, write the length of the shortest route between them, even if it passes through other vertices. Then work with this complete table. At the end, translate any “edge” of the table back into the real route it stands for.
Upper bound: the nearest-neighbour algorithm
Section titled “Upper bound: the nearest-neighbour algorithm”- Choose a starting vertex.
- From the current vertex, go to the nearest vertex not yet visited.
- Repeat until every vertex has been visited, then return to the start.
This gives a real Hamiltonian cycle, so its length is an upper bound: the best cycle can’t be longer. Different starting vertices can give different cycles. The best upper bound is the smallest one you find.
Lower bound: the deleted-vertex algorithm
Section titled “Lower bound: the deleted-vertex algorithm”- Delete one vertex and all the edges joined to it.
- Find the weight of a minimum spanning tree of the vertices that are left.
- Add the weights of the two shortest edges from the deleted vertex.
The result is a lower bound: the best cycle can’t be shorter. Why? Take any Hamiltonian cycle and remove the deleted vertex: it loses two edges at that vertex (each at least as long as the two shortest), and what’s left is a path through all the other vertices, which is a spanning tree, so it weighs at least as much as the MST.
Deleting different vertices can give different values. The best lower bound is the largest one.
Interpreting the bounds
Section titled “Interpreting the bounds”If the lower bound is itself the length of a Hamiltonian cycle (or equals an upper bound you’ve found), that cycle is optimal. Usually the deleted-vertex “network” isn’t a cycle, though, so the bound can’t be reached.
Worked examples
Section titled “Worked examples”Example 1: A table of least distances
Section titled “Example 1: A table of least distances”A courier must visit the five depots P, Q, R, S, T in the figure (distances in km) and return to the start. Complete a table of least distances.
Solution. For each pair, find the shortest route, which may go through other vertices:
- P to S: P–Q–S or P–R–S .
- P to T: there’s no road; P–R–T (P–Q–R–T is ).
- Q to S: the road QS beats Q–R–S .
- Q to T: Q–R–T (or Q–S–T ).
- R to T: the road RT beats R–S–T .
- All other pairs: the direct road is shortest.
| P | Q | R | S | T | |
|---|---|---|---|---|---|
| P | – | 6 | 9 | 14 | 16 |
| Q | 6 | – | 4 | 8 | 11 |
| R | 9 | 4 | – | 5 | 7 |
| S | 14 | 8 | 5 | – | 3 |
| T | 16 | 11 | 7 | 3 | – |
Example 2: Nearest-neighbour upper bound
Section titled “Example 2: Nearest-neighbour upper bound”Use the nearest-neighbour algorithm, starting at P, on the table from Example 1 to find an upper bound for the courier’s route. Write down the actual route on the roads.
Solution.
| From | Unvisited options | Go to |
|---|---|---|
| P | Q 6, R 9, S 14, T 16 | Q (6) |
| Q | R 4, S 8, T 11 | R (4) |
| R | S 5, T 7 | S (5) |
| S | T 3 | T (3) |
| T | back to P | P (16) |
Upper bound: km.
The step T to P () stands for the route T–R–P, so the actual route is P–Q–R–S–T–R–P. It passes through R twice, which is allowed in the practical problem.
(This isn’t the best possible route: P–Q–S–T–R–P is km. The nearest-neighbour algorithm gives a good cycle, not always the best one.)
Example 3: Upper and lower bounds for a classical problem
Section titled “Example 3: Upper and lower bounds for a classical problem”The table gives the travel times, in minutes, between five sites.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 12 | 10 | 15 | 8 |
| B | 12 | – | 3 | 7 | 9 |
| C | 10 | 3 | – | 6 | 11 |
| D | 15 | 7 | 6 | – | 14 |
| E | 8 | 9 | 11 | 14 | – |
- (a) Use the nearest-neighbour algorithm starting at A to find an upper bound.
- (b) Use the deleted-vertex algorithm, deleting A, to find a lower bound.
- (c) Write down an inequality for the length of the optimal cycle.
Solution. (a) From A the nearest is E (). From E: B , C , D , so B (). From B: C . From C: D . Back to A: .
The upper bound is minutes.
(b) Delete A. For B, C, D, E, Kruskal’s algorithm takes BC , CD , rejects BD (cycle B–C–D–B), and takes BE . MST weight: .
The two shortest edges from A are AE and AC . Lower bound: minutes.
(c) .
Example 4: Improving the bounds
Section titled “Example 4: Improving the bounds”For the table in Example 3, the lower bounds from deleting each vertex are A: , B: , C: , D: , E: .
- (a) State the best lower bound.
- (b) A planner suggests the cycle A–C–D–B–E–A. Find its length and improve the inequality for .
Solution. (a) The best lower bound is the largest: minutes.
(b) A–C–D–B–E–A has length minutes. Any Hamiltonian cycle gives an upper bound, and , so
(In fact, checking all cycles on a computer shows is the optimum, but the bounds alone can’t prove that here.)
Common mistakes
Section titled “Common mistakes”Using a direct distance when a shorter route exists. In a practical problem, fill the table with least distances. In Example 1, the least distance from P to T is through R, even though there’s no road PT.
Choosing the wrong “best” bound. The best upper bound is the smallest upper bound; the best lower bound is the largest lower bound. Both squeeze the answer from either side.
Forgetting to return to the start. The nearest-neighbour cycle ends with the edge back to the starting vertex. Leaving it out gives a number that isn’t a bound at all.
Including the deleted vertex in the MST. Find the MST of the remaining vertices only, then add the two shortest edges from the deleted vertex, not one.
Thinking the lower bound is a route. The deleted-vertex network usually isn’t a cycle, so you can’t travel it. It just tells you no cycle can be shorter.
Not translating back to the real network. In a practical problem, an entry like T to P () stands for T–R–P. Give the real route when asked.
Practice
Section titled “Practice”1. (Warm-up) How many different Hamiltonian cycles (ignoring starting point and direction) are there in ? In ?
Solution
2. (Warm-up) For a TSP, the nearest-neighbour algorithm from different starting vertices gives , and , and the deleted-vertex algorithm gives , and . Write down the best inequality for the length of the optimal cycle.
Solution
Best upper bound: the smallest, . Best lower bound: the largest, . So .
3. (Warm-up) Explain why the length of any Hamiltonian cycle you find is an upper bound for the travelling salesman problem.
Solution
The optimal cycle is the shortest of all the Hamiltonian cycles. The cycle you found is one of them, so the optimal one is no longer than it.
4. (Core) A graph has edges AB , BC , CD , AD and BD .
- (a) Complete a table of least distances.
- (b) Use the nearest-neighbour algorithm, starting at A, to find an upper bound, and write down the actual route.
Solution
(a) A to C: A–B–C . A to D: A–B–D , shorter than the road AD . B to D: the road BD beats B–C–D .
| A | B | C | D | |
|---|---|---|---|---|
| A | – | 5 | 9 | 12 |
| B | 5 | – | 4 | 7 |
| C | 9 | 4 | – | 6 |
| D | 12 | 7 | 6 | – |
(b) A to B (), B to C (), C to D (), D back to A (). Upper bound: .
D to A () is really D–B–A, so the route is A–B–C–D–B–A.
5. (Core) The table gives the distances, in km, between five towns.
| K | L | M | N | O | |
|---|---|---|---|---|---|
| K | – | 20 | 35 | 25 | 30 |
| L | 20 | – | 18 | 28 | 24 |
| M | 35 | 18 | – | 22 | 16 |
| N | 25 | 28 | 22 | – | 19 |
| O | 30 | 24 | 16 | 19 | – |
Use the nearest-neighbour algorithm, starting at K, to find an upper bound.
Solution
- K: L , N , O , M . Go to L.
- L: M , O , N . Go to M.
- M: O , N . Go to O.
- O: N . Go to N.
- N: back to K, .
K–L–M–O–N–K: km.
6. (Core) For the table in Question 5, use the deleted-vertex algorithm, deleting K, to find a lower bound. What can you conclude?
Solution
Delete K. For L, M, N, O, Kruskal’s takes MO , LM , NO (then all four are connected). MST weight: .
Two shortest edges from K: KL and KN . Lower bound: km.
The lower bound equals the upper bound from Question 5, so : the cycle K–L–M–O–N–K, of length km, is optimal.
7. (Core) Use the table of least distances from Example 1.
- (a) Use the nearest-neighbour algorithm starting at S to find an upper bound, and give the actual route.
- (b) Use the deleted-vertex algorithm, deleting T, to find a lower bound.
Solution
(a) S to T (); T: R , Q , P , so R (); R: Q , P , so Q (); Q to P (); P back to S ().
Upper bound: km. P to S () is really P–Q–S (or P–R–S), so one actual route is S–T–R–Q–P–Q–S.
(b) Delete T. For P, Q, R, S: Kruskal’s takes QR , RS , PQ . MST weight . Two shortest edges from T: TS and TR . Lower bound: km.
8. (Challenge) For the courier problem in Examples 1 and 2:
- (a) Find the lower bound from deleting each of P, Q, R and S, and state the best lower bound.
- (b) Using the cycle P–Q–S–T–R–P, write the best inequality you can for the length of the optimal route.
Solution
(a) Using the table of least distances:
- Delete P: MST of Q, R, S, T is ST , QR , RS ; two shortest from P: . Bound .
- Delete Q: MST of P, R, S, T is ST , RS , PR ; two shortest from Q: . Bound .
- Delete R: MST of P, Q, S, T is ST , PQ , QS ; two shortest from R: . Bound .
- Delete S: MST of P, Q, R, T is QR , PQ , RT ; two shortest from S: . Bound .
With from deleting T (Question 7), the best lower bound is the largest: km.
(b) P–Q–S–T–R–P has length km, better than the nearest-neighbour bound of . So .
9. (Challenge) Explain carefully why the deleted-vertex algorithm always gives a lower bound for the classical travelling salesman problem.
Solution
Take any Hamiltonian cycle and the deleted vertex X. In the cycle, X is joined to exactly two other vertices, by two edges. Their total weight is at least the total of the two shortest edges from X.
Removing X and those two edges from the cycle leaves a path through all the other vertices. A path that includes every remaining vertex is a spanning tree of them, so its weight is at least the weight of their minimum spanning tree.
Adding these up: every Hamiltonian cycle weighs at least (MST of the remaining vertices) (two shortest edges from X). In particular, the optimal cycle does, so this total is a lower bound.