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.

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.

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 K7K_7? In K9K_9?
  • (b) Show that going from nn towns to n+1n + 1 towns multiplies the number of cycles by nn.

H2. (Foundations) For a travelling salesman problem, the nearest-neighbour algorithm from three different starting vertices gives 6363 km, 5858 km and 6161 km. The deleted-vertex algorithm, deleting three different vertices, gives 5050 km, 5454 km and 4747 km.

  • (a) Write down the best inequality for the length LL of the optimal cycle.
  • (b) Someone claims to have found a cycle of length 5252 km. Explain why they must have made a mistake.
  • (c) Later, you find a Hamiltonian cycle of length 5656 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 1212, AC 77, BC 44, BD 1010, BF 1818, CD 1313, CE 99, DE 55, DF 66, EF 1414.

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.

GHJKM
G–23293441
H23–242825
J2924–3940
K342839–33
M41254033–
  • (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 LL of the ranger’s optimal route.
  • (d) A computer checks all 1212 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.

H7. (Challenge) Riya uses the table in H5 and writes:

“Delete M. The minimum spanning tree of G, H, J, K is GH 2323, HJ 2424, HK 2828, total 7575. The shortest edge from M is HM 2525, so the lower bound is 75+25=10075 + 25 = 100 km. My lower bounds from deleting G, H and M are 129129, 143143 and 100100, so the best lower bound is 100100 km. Also, the edges I used form a route the ranger could drive, so a 100100 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.

PQRSU
P–5161826
Q5–191329
R1619–2017
S181320–24
U26291724–
  • (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 7070 km. Explain why this does not contradict your conclusion in (c).

H9. (Challenge) Four towns have this table of least distances (in km), where 3≤k≤163 \le k \le 16 and k≠9k \ne 9.

ABCD
A–kk913
Bkk–710
C97–8
D13108–
  • (a) There are three Hamiltonian cycles. Find the length of each in terms of kk.
  • (b) Find all values of kk for which A–B–D–C–A is an optimal cycle.
  • (c) Find all values of kk for which the nearest-neighbour algorithm starting at A gives an optimal cycle. Consider the cases k<9k \lt 9 and k>9k \gt 9 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 44, HQ 66, HR 77, HS 99, PQ 33, RS 55.

  • (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 1212 Hamiltonian cycles in your table shows that the shortest has length 3434 km. What does this tell you about your route in (c)? Explain why it makes sense, using the road lengths.