Skip to content
Family Table Math
Auto

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.

In a complete graph with nn vertices, the number of different Hamiltonian cycles (ignoring the starting point and direction) is

(n−1)!2\frac{(n - 1)!}{2}

That’s 1212 cycles for 55 towns, but nearly 240240 million for 1313 towns and about 3×10623 \times 10^{62} for 5050. Checking them all quickly becomes impossible, even for a computer. That’s why we use algorithms that give good bounds.

  • 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”
  1. Choose a starting vertex.
  2. From the current vertex, go to the nearest vertex not yet visited.
  3. 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.

  1. Delete one vertex and all the edges joined to it.
  2. Find the weight of a minimum spanning tree of the vertices that are left.
  3. 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.

best lower bound≤length of the optimal cycle≤best upper bound\text{best lower bound} \le \text{length of the optimal cycle} \le \text{best upper bound}

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.

Five vertices A to E. With A deleted, the minimum spanning tree of B, C, D, E uses BC 3, CD 6 and BE 9 (blue, weight 18). The two shortest edges from A, AE 8 and AC 10, are added in orange, giving a lower bound of 36. 3 6 9 8 10 A B C D E
The deleted-vertex lower bound from Example 3: MST of the other vertices (blue) plus the two shortest edges from A (orange).
A weighted graph with vertices P, Q, R, S, T and edges PQ 6, PR 9, QR 4, QS 8, RS 5, RT 7, ST 3. There is no direct edge from P to T. 6 9 4 8 5 7 3 P Q R S T
The road network for Examples 1 and 2 (distances in km).

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 =14= 14 or P–R–S =14= 14.
  • P to T: there’s no road; P–R–T =16= 16 (P–Q–R–T is 1717).
  • Q to S: the road QS =8= 8 beats Q–R–S =9= 9.
  • Q to T: Q–R–T =11= 11 (or Q–S–T =11= 11).
  • R to T: the road RT =7= 7 beats R–S–T =8= 8.
  • All other pairs: the direct road is shortest.
PQRST
P–691416
Q6–4811
R94–57
S1485–3
T161173–

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.

FromUnvisited optionsGo to
PQ 6, R 9, S 14, T 16Q (6)
QR 4, S 8, T 11R (4)
RS 5, T 7S (5)
ST 3T (3)
Tback to PP (16)

Upper bound: 6+4+5+3+16=346 + 4 + 5 + 3 + 16 = 34 km.

The step T to P (1616) 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 6+8+3+7+9=336 + 8 + 3 + 7 + 9 = 33 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.

ABCDE
A–1210158
B12–379
C103–611
D1576–14
E891114–
  • (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 LL of the optimal cycle.

Solution. (a) From A the nearest is E (88). From E: B 99, C 1111, D 1414, so B (99). From B: C 33. From C: D 66. Back to A: 1515.

A–E–B–C–D–A:8+9+3+6+15=41\text{A–E–B–C–D–A:} \quad 8 + 9 + 3 + 6 + 15 = 41

The upper bound is 4141 minutes.

(b) Delete A. For B, C, D, E, Kruskal’s algorithm takes BC 33, CD 66, rejects BD 77 (cycle B–C–D–B), and takes BE 99. MST weight: 3+6+9=183 + 6 + 9 = 18.

The two shortest edges from A are AE 88 and AC 1010. Lower bound: 18+8+10=3618 + 8 + 10 = 36 minutes.

(c) 36≤L≤4136 \le L \le 41.

For the table in Example 3, the lower bounds from deleting each vertex are A: 3636, B: 3434, C: 3333, D: 3333, E: 3636.

  • (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 LL.

Solution. (a) The best lower bound is the largest: 3636 minutes.

(b) A–C–D–B–E–A has length 10+6+7+9+8=4010 + 6 + 7 + 9 + 8 = 40 minutes. Any Hamiltonian cycle gives an upper bound, and 40<4140 \lt 41, so

36≤L≤4036 \le L \le 40

(In fact, checking all 1212 cycles on a computer shows 4040 is the optimum, but the bounds alone can’t prove that here.)

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 1616 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 (1616) stands for T–R–P. Give the real route when asked.

1. (Warm-up) How many different Hamiltonian cycles (ignoring starting point and direction) are there in K5K_5? In K6K_6?

SolutionK5: 4!2=12,K6: 5!2=60K_5: \ \frac{4!}{2} = 12, \qquad K_6: \ \frac{5!}{2} = 60

2. (Warm-up) For a TSP, the nearest-neighbour algorithm from different starting vertices gives 5252, 4949 and 5555, and the deleted-vertex algorithm gives 4141, 4444 and 3939. Write down the best inequality for the length LL of the optimal cycle.

Solution

Best upper bound: the smallest, 4949. Best lower bound: the largest, 4444. So 44≤L≤4944 \le L \le 49.

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 55, BC 44, CD 66, AD 1313 and BD 77.

  • (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 =9= 9. A to D: A–B–D =12= 12, shorter than the road AD =13= 13. B to D: the road BD =7= 7 beats B–C–D =10= 10.

ABCD
A–5912
B5–47
C94–6
D1276–

(b) A to B (55), B to C (44), C to D (66), D back to A (1212). Upper bound: 5+4+6+12=275 + 4 + 6 + 12 = 27.

D to A (1212) 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.

KLMNO
K–20352530
L20–182824
M3518–2216
N252822–19
O30241619–

Use the nearest-neighbour algorithm, starting at K, to find an upper bound.

Solution
  • K: L 2020, N 2525, O 3030, M 3535. Go to L.
  • L: M 1818, O 2424, N 2828. Go to M.
  • M: O 1616, N 2222. Go to O.
  • O: N 1919. Go to N.
  • N: back to K, 2525.

K–L–M–O–N–K: 20+18+16+19+25=9820 + 18 + 16 + 19 + 25 = 98 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 1616, LM 1818, NO 1919 (then all four are connected). MST weight: 16+18+19=5316 + 18 + 19 = 53.

Two shortest edges from K: KL 2020 and KN 2525. Lower bound: 53+20+25=9853 + 20 + 25 = 98 km.

The lower bound equals the upper bound from Question 5, so 98≤L≤9898 \le L \le 98: the cycle K–L–M–O–N–K, of length 9898 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 (33); T: R 77, Q 1111, P 1616, so R (77); R: Q 44, P 99, so Q (44); Q to P (66); P back to S (1414).

Upper bound: 3+7+4+6+14=343 + 7 + 4 + 6 + 14 = 34 km. P to S (1414) 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 44, RS 55, PQ 66. MST weight 1515. Two shortest edges from T: TS 33 and TR 77. Lower bound: 15+3+7=2515 + 3 + 7 = 25 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 LL of the optimal route.
Solution

(a) Using the table of least distances:

  • Delete P: MST of Q, R, S, T is ST 33, QR 44, RS 55 =12= 12; two shortest from P: 6+96 + 9. Bound 2727.
  • Delete Q: MST of P, R, S, T is ST 33, RS 55, PR 99 =17= 17; two shortest from Q: 4+64 + 6. Bound 2727.
  • Delete R: MST of P, Q, S, T is ST 33, PQ 66, QS 88 =17= 17; two shortest from R: 4+54 + 5. Bound 2626.
  • Delete S: MST of P, Q, R, T is QR 44, PQ 66, RT 77 =17= 17; two shortest from S: 3+53 + 5. Bound 2525.

With 2525 from deleting T (Question 7), the best lower bound is the largest: 2727 km.

(b) P–Q–S–T–R–P has length 6+8+3+7+9=336 + 8 + 3 + 7 + 9 = 33 km, better than the nearest-neighbour bound of 3434. So 27≤L≤3327 \le L \le 33.

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.