James Ashton MathsFREE MATHEMATICS. OPEN TO EVERYONE.
HomeIB DiplomaA levelAbout
IB Diploma › Maths AI HL › Geometry & trigonometry › Graph algorithms

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.

AHL 3.16HL onlyPaper 1Paper 2Paper 3GDC allowed≈ 65 min to work through
Before you start: graph vocabulary and weighted tables (Graph theory & adjacency matrices).

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.

The big idea

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

NameRule
Walkany sequence of connected edges
Traila walk with no repeated edge
Patha walk with no repeated vertex
Circuita trail that ends where it started
Cyclea circuit with no repeated vertex other than the start/end
Euler and Hamilton Not in booklet: learn it

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.

7546910358ABCDEF

Graph W (weights in km)

Worked example 1Paper 1
For graph W, decide whether there is an Eulerian circuit or an Eulerian trail, and write down a Hamiltonian cycle.
Degrees: A 2, B 4, C 3, D 3, E 4, F 2
Two odd vertices (C and D): no Eulerian circuit, but there is an Eulerian trail from C to D.
Hamiltonian cycle: ABDFECA

2.2 Minimum spanning trees

A spanning tree connects all the vertices with no cycles; with n vertices it has n−1 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 n−1 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.

Worked example 2Paper 1
Use Kruskal’s algorithm to find the MST of W, stating the order in which edges are added.
In order: DE (3), BC (4), AC (5), DF (5), BD (6)
Five edges for six vertices, so stop. Total weight 23 km.
The next edge, AB, would have made the cycle ABC.
7546910358ABCDEF

The minimum spanning tree of W (green), weight 23 km

The table below gives distances (km) between five towns. It is used in WE3 and WE6.

PQRST
P–1291511
Q12–81014
R98–137
S151013–6
T111476–
Worked example 3Paper 1
Use Prim’s algorithm, starting at P, to find the MST for the five towns.
P→R (9); then from {P,R} the smallest is RT (7)
From {P,R,T}: TS (6); from {P,R,T,S}: RQ (8)
Edges PR,RT,TS,RQ; weight 30 km

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.

Chinese postman algorithm Not in booklet: learn it

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.

Worked example 4Paper 1
Find the length of the shortest route that travels along every road of W and returns to its start.
Total weight =57 km; odd vertices C and D
Shortest C to D: CBD=4+6=10 (not CED=12)
Repeat edges CB and BD.
Route length =57+10=67 km
Worked example 5Paper 2
A graph has odd vertices A,B,D,E, with shortest distances AB=4, DE=2, AD=9, BE=7, AE=10, BD=5. Which edges should be repeated?
AB+DE=6, AD+BE=16, AE+BD=15
Least: pair A with B and D with E; add 6 to the total weight.

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.

Worked example 6Paper 2
For the five towns in the table, find an upper bound by nearest neighbour from P, and a lower bound by deleting P.
P→R (9) →T (7) →S (6) →Q (10) →P (12): upper bound 44 km
Delete P: MST of Q,R,S,T is TS,RT,QR with weight 21
Two shortest edges from P: 9 and 11. Lower bound =21+9+11=41 km
41≤ optimal route ≤44
Here the upper bound happens to be the optimum, but you could not know that without checking every cycle.
On your TI-Nspire Calculator tricks →

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.

FluencyP1
A connected graph has degrees 2, 4, 4, 2, 6. Does it have an Eulerian circuit?
Yes: every vertex is even.
FluencyP2
How many edges are in a spanning tree of a graph with 12 vertices?
11
FluencyP3
In graph W, find the length of the shortest route that uses every edge, starting at C and finishing at D.
An Eulerian trail: 57 km.
FluencyP4
For the five-town table, find an upper bound by nearest neighbour starting at Q.
Q→R 8, →T 7, →S 6, →P 15, →Q 12: 48 km. (The bound from P, 44, is better.)
Problem solvingP5
For the five-town table, find a lower bound by deleting T.
MST of P,Q,R,S: QR 8, PR 9, QS 10 = 27; plus 6 + 7 = 40 km.
Problem solvingP6
Explain why, in the Chinese postman problem, the number of odd vertices is always even.
The degrees add to twice the number of edges, an even number, so there is an even number of odd terms.

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.

Question 1Paper 1[8 marks]

A road inspector must drive along every road in the network shown, starting and finishing at A. The weights are distances in km, and the total length of the roads is 45 km.463578210ABCDE

  • (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 (A,B,D,E), so there is no Eulerian circuitR1
(b)
AB+DE=4+2=6A1
AD+BE=9+7=16 and AE+BD=10+5=15A1
Award A1 for each correct pairing total seen; the second A1 needs both of the other two pairings.
repeat AB and DEA1
45+6=51 kmA1
(c)
DE, BC, AB, BDM1A1
M1 for the first two edges in the correct order.
14 kmA1
Question 2Paper 2[9 marks]

A courier based at A must visit five other towns, B to F, and return to A. The table shows the shortest travel distances in km.

ABCDEF
A–1422182516
B14–12202319
C2212–111724
D182011–913
E2523179–15
F1619241315–

  • (a)Use the nearest neighbour algorithm, starting at A, to find an upper bound for the courier’s route. State the route.[3]
  • (b)By deleting vertex A 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, L km, of the shortest route.[1]
  • (d)State, with a reason, whether your upper bound is the optimal route.[1]
(a)
A→B→C→D→E→F→AM1A1
14+12+11+9+15+16=77 kmA1
(b)
MST of B,C,D,E,F: DE 9, CD 11, BC 12, DF 13M1A1
weight 45A1
45+14+16=75 kmA1
(c)
75≤L≤77A1
(d)
it cannot be known from the bounds alone, because the lower and upper bounds are not equalR1
Accept a correct answer from checking all cycles: 77 km is optimal.
Question 3Paper 1[5 marks]

Graph W is shown, with weights in km. Its total weight is 57 km.7546910358ABCDEF

Graph W (weights in km)

  • (a)Find the length of the shortest route that travels along every edge at least once, starting at C and finishing at D. Justify your answer.[2]
  • (b)Find the length of the shortest route that travels along every edge at least once, starting and finishing at C. State the edges that are repeated.[3]
(a)
C and D are the only odd vertices, so an Eulerian trail from C to D existsR1
57 kmA1
(b)
shortest path C to D is CBD=10(M1)
repeat CB and BDA1
67 kmA1

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.

PaperQuestionFocusDifficulty
May 2021 · TZ1 · Paper 1Q10Prim’s algorithm from a table of travel times★★☆
May 2023 · TZ2 · Paper 2Q4Connected and complete; Kruskal’s algorithm for flights★★☆
May 2024 · TZ2 · Paper 2Q4Nearest neighbour upper bound and deleted vertex lower bound★★☆
Nov 2022 · Paper 2Q4Hamiltonian cycles and Eulerian circuits for office visits★★☆
May 2024 · TZ1 · Paper 1Q8Chinese postman with an unknown edge length★★★
Nov 2021 · Paper 3Q1Graph algorithms with an unknown edge weight★★★
Spotted a mistake on this page?Tell me and I'll fix it. The button opens an email with this page already named, or write to jamesashtonmaths@gmail.com.
Report a mistake