Graph algorithms: trees & cycles
How do you connect every house to the network with the least cable? How does a gritter cover every road with the least driving? How does a delivery driver visit every town and get home quickly? Each has a standard algorithm, and the travelling salesman problem is so hard that we settle for bounds.
1What you need to know
Tick these off as you go. By the end of the page you should be able to do all of them without notes.
- Use the words walk, trail, path, circuit and cycle correctlyAHL 3.16
- Decide whether a graph has an Eulerian trail or circuitAHL 3.16
- Find Hamiltonian paths and cyclesAHL 3.16
- Find a minimum spanning tree with Kruskal’s algorithm and with Prim’s algorithm, including from a tableAHL 3.16
- Solve the Chinese postman problem for graphs with up to four odd vertices, and explain why the method worksAHL 3.16
- Find an upper bound for the travelling salesman problem with the nearest neighbour algorithmAHL 3.16
- Find a lower bound with the deleted vertex algorithm, and convert a practical problem to the classical oneAHL 3.16
2Notes & worked examples
Each idea is followed by a worked example. Try the question yourself first, then reveal the solution one step at a time.
Three classic problems: join everything up cheaply (minimum spanning tree), travel along every edge (Chinese postman), and visit every vertex (travelling salesman). The first two have quick, exact algorithms. The third has no known quick method, so we trap the answer between an upper bound and a lower bound.
2.1 Walks, trails, paths, circuits and cycles
| Name | Rule |
|---|---|
| Walk | any sequence of connected edges |
| Trail | a walk with no repeated edge |
| Path | a walk with no repeated vertex |
| Circuit | a trail that ends where it started |
| Cycle | a circuit with no repeated vertex other than the start/end |
An Eulerian circuit uses every edge exactly once and returns to the start: it exists if the graph is connected and every vertex has even degree. An Eulerian trail (not closed) exists if exactly two vertices are odd: it starts at one and ends at the other. A Hamiltonian cycle visits every vertex exactly once and returns to the start; there is no simple test for whether one exists.
Graph (weights in km)
2.2 Minimum spanning trees
A spanning tree connects all the vertices with no cycles; with vertices it has edges. A minimum spanning tree (MST) has the least total weight.
Kruskal’s algorithm: list the edges in increasing order of weight. Add each edge in turn unless it makes a cycle. Stop at edges. Prim’s algorithm: start at any vertex. Repeatedly add the cheapest edge joining a vertex already in the tree to one that is not. With a table, cross out the row of each vertex once it joins the tree and look down the columns of the vertices in the tree for the smallest remaining entry.
The minimum spanning tree of (green), weight 23 km
The table below gives distances (km) between five towns. It is used in WE3 and WE6.
| P | Q | R | S | T | |
|---|---|---|---|---|---|
| P | – | 12 | 9 | 15 | 11 |
| Q | 12 | – | 8 | 10 | 14 |
| R | 9 | 8 | – | 13 | 7 |
| S | 15 | 10 | 13 | – | 6 |
| T | 11 | 14 | 7 | 6 | – |
2.3 The Chinese postman problem
The route must travel along every edge at least once and return to the start. If every vertex is even, an Eulerian circuit does it, and the length is the total weight. Otherwise, some edges must be repeated. Repeating a path between two odd vertices makes both of them even, so pair up the odd vertices and choose the pairing whose shortest connecting paths add up to the least.
1. Find the odd vertices. 2. List every way of pairing them (two odd vertices: one pairing; four: three pairings). 3. For each pairing, add the shortest path lengths. 4. Repeat the edges of the cheapest pairing. Route length = total weight of the graph + that extra.
2.4 The travelling salesman problem
The TSP asks for the Hamiltonian cycle of least weight in a complete weighted graph. A practical problem (where some direct routes are missing, or longer than going via another town) is first turned into the classical one by filling in a table of shortest distances. Checking every cycle quickly becomes impossible, so we find bounds.
Nearest neighbour (upper bound): from the start, always go to the nearest unvisited vertex; finally return to the start. The cycle is a real route, so the optimum is no longer. Deleted vertex (lower bound): delete one vertex; find an MST of what is left; add the two shortest edges from the deleted vertex. The optimum is no shorter.
These algorithms are done by hand, and the marks are for showing each step: list the edges in the order they are chosen, write down the pairings and their totals, and say which vertex is deleted. The GDC is useful only for adding weights. For a large table, cross out rows and circle chosen entries as you go so you do not lose track.
3Explore it
Step through Kruskal’s and Prim’s algorithms on graph W. Both find the same tree here (weight 23), but they add the edges in a different order.
4Common mistakes & exam tips
Common mistakes
- Mixing up Eulerian (every edge) and Hamiltonian (every vertex).
- In Kruskal’s algorithm, adding an edge that makes a cycle.
- In Prim’s algorithm, choosing the cheapest edge anywhere instead of the cheapest edge joined to the tree.
- In the Chinese postman problem, using the direct edge between two odd vertices when a path through other vertices is shorter.
- Calling the nearest neighbour result the answer to the TSP. It is only an upper bound.
- In the deleted vertex algorithm, adding only one edge from the deleted vertex instead of the two shortest.
Exam tips
- Always state the order edges are added: the method marks depend on it.
- For four odd vertices, show all three pairings with their totals, then choose.
- "Explain why the algorithm works": repeating a path between two odd vertices adds 1 to the degree of each, making every vertex even.
- Write the final answer to a bounds question as an inequality: lower bound optimum upper bound.
- Try nearest neighbour from different starting vertices: the smallest result is the best upper bound. Try deleting different vertices: the largest result is the best lower bound.
5Practice
Fluency questions build the basics. Problem-solving questions make you think about which method to use.
6Exam-style questions
Original questions in the style of the IB Maths AI HL exam, with full markschemes. Your GDC is allowed throughout: use it, but write down what you entered and what it gave you. Give yourself about 1 minute per mark.
A road inspector must drive along every road in the network shown, starting and finishing at . The weights are distances in km, and the total length of the roads is 45 km.
- (a)Explain why the inspector must drive along some roads more than once.[1]
- (b)Find the length of the shortest possible route, stating which roads are repeated.[4]
- (c)Use Kruskal’s algorithm to find a minimum spanning tree for the network, listing the edges in the order they are added. State its total weight.[3]
| (a) | |
| there are odd vertices (), so there is no Eulerian circuit | R1 |
| (b) | |
| A1 | |
| and | A1 |
| Award A1 for each correct pairing total seen; the second A1 needs both of the other two pairings. | |
| repeat and | A1 |
| km | A1 |
| (c) | |
| , , , | M1A1 |
| M1 for the first two edges in the correct order. | |
| km | A1 |
A courier based at must visit five other towns, to , and return to . The table shows the shortest travel distances in km.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| A | – | 14 | 22 | 18 | 25 | 16 |
| B | 14 | – | 12 | 20 | 23 | 19 |
| C | 22 | 12 | – | 11 | 17 | 24 |
| D | 18 | 20 | 11 | – | 9 | 13 |
| E | 25 | 23 | 17 | 9 | – | 15 |
| F | 16 | 19 | 24 | 13 | 15 | – |
- (a)Use the nearest neighbour algorithm, starting at , to find an upper bound for the courier’s route. State the route.[3]
- (b)By deleting vertex and using a minimum spanning tree of the remaining vertices, find a lower bound for the route.[4]
- (c)Hence write down an inequality for the length, km, of the shortest route.[1]
- (d)State, with a reason, whether your upper bound is the optimal route.[1]
| (a) | |
| M1A1 | |
| km | A1 |
| (b) | |
| MST of : 9, 11, 12, 13 | M1A1 |
| weight 45 | A1 |
| km | A1 |
| (c) | |
| A1 | |
| (d) | |
| it cannot be known from the bounds alone, because the lower and upper bounds are not equal | R1 |
| Accept a correct answer from checking all cycles: 77 km is optimal. | |
Graph is shown, with weights in km. Its total weight is 57 km.
Graph (weights in km)
- (a)Find the length of the shortest route that travels along every edge at least once, starting at and finishing at . Justify your answer.[2]
- (b)Find the length of the shortest route that travels along every edge at least once, starting and finishing at . State the edges that are repeated.[3]
| (a) | |
| and are the only odd vertices, so an Eulerian trail from to exists | R1 |
| km | A1 |
| (b) | |
| shortest path to is | (M1) |
| repeat and | A1 |
| km | A1 |
7Check yourself
Six quick questions, marked instantly. Aim for 6/6 before moving on. Fractions like 3/2 are accepted.
8Past-paper practice
Once you are scoring well above, try these real IB Maths AI HL questions on this topic. Your school or teacher will have the papers.
| Paper | Question | Focus | Difficulty |
|---|---|---|---|
| May 2021 · TZ1 · Paper 1 | Q10 | Prim’s algorithm from a table of travel times | ★★☆ |
| May 2023 · TZ2 · Paper 2 | Q4 | Connected and complete; Kruskal’s algorithm for flights | ★★☆ |
| May 2024 · TZ2 · Paper 2 | Q4 | Nearest neighbour upper bound and deleted vertex lower bound | ★★☆ |
| Nov 2022 · Paper 2 | Q4 | Hamiltonian cycles and Eulerian circuits for office visits | ★★☆ |
| May 2024 · TZ1 · Paper 1 | Q8 | Chinese postman with an unknown edge length | ★★★ |
| Nov 2021 · Paper 3 | Q1 | Graph algorithms with an unknown edge weight | ★★★ |
