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.
Homework
Section titled “Homework”Try these independently after the practice questions. Show your setup and explain your reasoning. Use exact answers where possible, and check that your answers fit the question. Homework solutions are not provided on the published site.
All distances are hypothetical. Show each step of every algorithm (the options you compare and the one you choose); no ties occur at any step.
Foundations
Section titled “Foundations”H1. (Foundations) Use the formula for the number of Hamiltonian cycles in a complete graph (ignoring starting point and direction).
- (a) How many Hamiltonian cycles are there in ? In ?
- (b) Show that going from towns to towns multiplies the number of cycles by .
H2. (Foundations) For a travelling salesman problem, the nearest-neighbour algorithm from three different starting vertices gives km, km and km. The deleted-vertex algorithm, deleting three different vertices, gives km, km and km.
- (a) Write down the best inequality for the length of the optimal cycle.
- (b) Someone claims to have found a cycle of length km. Explain why they must have made a mistake.
- (c) Later, you find a Hamiltonian cycle of length km. Improve your inequality.
H3. (Core) A mobile health clinic visits six communities, A to F, in a hypothetical rural area of northern Ontario. The roads between them (lengths in km) are:
AB , AC , BC , BD , BF , CD , CE , DE , DF , EF .
There are no other roads.
- (a) Complete a table of least distances for the six communities.
- (b) For which pairs of communities joined by a road is the least distance shorter than the road itself? Give the shorter route for each.
H4. (Core) Use your table of least distances from H3.
- (a) Use the nearest-neighbour algorithm, starting at A, to find an upper bound for the clinic’s round trip.
- (b) Use the nearest-neighbour algorithm, starting at B, to find another upper bound.
- (c) State the best upper bound, and write down the actual route on the roads that achieves it, starting and ending at B.
H5. (Core) A park ranger checks five campgrounds, G, H, J, K and M, in a hypothetical provincial park, starting and finishing at the same campground. The table gives the shortest driving distances between them in km.
| G | H | J | K | M | |
|---|---|---|---|---|---|
| G | – | 23 | 29 | 34 | 41 |
| H | 23 | – | 24 | 28 | 25 |
| J | 29 | 24 | – | 39 | 40 |
| K | 34 | 28 | 39 | – | 33 |
| M | 41 | 25 | 40 | 33 | – |
- (a) Use the nearest-neighbour algorithm starting at G to find an upper bound.
- (b) Use the nearest-neighbour algorithm starting at H to find an upper bound.
- (c) State the best upper bound from (a) and (b).
H6. (Core) (IB style) Use the table in H5.
- (a) Use the deleted-vertex algorithm, deleting G, to find a lower bound. List the edges of your minimum spanning tree.
- (b) Use the deleted-vertex algorithm, deleting H, to find a lower bound.
- (c) Using your answers to H5 and to (a) and (b), write down the best inequality for the length of the ranger’s optimal route.
- (d) A computer checks all Hamiltonian cycles and reports that the optimal route is G–J–H–M–K–G. Find its length, and comment on how close each of your bounds was.
Challenge
Section titled “Challenge”H7. (Challenge) Riya uses the table in H5 and writes:
“Delete M. The minimum spanning tree of G, H, J, K is GH , HJ , HK , total . The shortest edge from M is HM , so the lower bound is km. My lower bounds from deleting G, H and M are , and , so the best lower bound is km. Also, the edges I used form a route the ranger could drive, so a km route exists.”
- (a) Find and correct the mistake in Riya’s calculation for deleting M.
- (b) Find and correct the mistake in her choice of the best lower bound.
- (c) Explain why the edges of the deleted-vertex network (after your correction) do not form a route the ranger could drive.
H8. (Challenge) A technician services five wind turbines, P, Q, R, S and U. The table gives the least distances between them in km.
| P | Q | R | S | U | |
|---|---|---|---|---|---|
| P | – | 5 | 16 | 18 | 26 |
| Q | 5 | – | 19 | 13 | 29 |
| R | 16 | 19 | – | 20 | 17 |
| S | 18 | 13 | 20 | – | 24 |
| U | 26 | 29 | 17 | 24 | – |
- (a) Use the nearest-neighbour algorithm starting at P to find an upper bound.
- (b) Use the deleted-vertex algorithm, deleting U, to find a lower bound.
- (c) Show that the five edges you used in (b) form a Hamiltonian cycle. What can you conclude about the optimal route? Explain.
- (d) Deleting P instead gives a lower bound of km. Explain why this does not contradict your conclusion in (c).
H9. (Challenge) Four towns have this table of least distances (in km), where and .
| A | B | C | D | |
|---|---|---|---|---|
| A | – | 9 | 13 | |
| B | – | 7 | 10 | |
| C | 9 | 7 | – | 8 |
| D | 13 | 10 | 8 | – |
- (a) There are three Hamiltonian cycles. Find the length of each in terms of .
- (b) Find all values of for which A–B–D–C–A is an optimal cycle.
- (c) Find all values of for which the nearest-neighbour algorithm starting at A gives an optimal cycle. Consider the cases and separately.
H10. (Challenge) A courier works from a hub H and delivers to four stops, P, Q, R and S. The only roads (lengths in km) are:
HP , HQ , HR , HS , PQ , RS .
- (a) Try the nearest-neighbour algorithm directly on the road network, starting at H and moving only along roads. Explain where it gets stuck.
- (b) Explain why the road network has no Hamiltonian cycle at all.
- (c) Complete a table of least distances, then use the nearest-neighbour algorithm starting at H to find an upper bound. Write down the actual route on the roads.
- (d) A computer check of all Hamiltonian cycles in your table shows that the shortest has length km. What does this tell you about your route in (c)? Explain why it makes sense, using the road lengths.