All revision notes topics

3.16 Graph algorithms: trees, cycles and routesIB Maths: Applications and Interpretation HL: Revision notes

Section 1

Walks, trails, paths, circuits and cycles

A graph has vertices joined by edges; the degree of a vertex is the number of edges meeting it. A walk is any sequence of edges linking vertices. A trail is a walk with no repeated edge. A path is a walk with no repeated vertex. A circuit is a closed trail (starts and ends at the same vertex). A cycle is a closed path: no vertex repeats except the start and end. A tree is a connected graph with no cycles; a tree with nn vertices has n−1n-1 edges. A complete graph has an edge between every pair of vertices. A graph is connected if there is a walk between any two vertices.

Key termswalktrailpathcircuitcycletree
Common mistake

Mixing up circuit and cycle. A circuit may pass through a vertex more than once; a cycle may not.

Section 2

Eulerian trails and circuits

An Eulerian trail uses every edge exactly once; an Eulerian circuit does so and returns to the start. For a connected graph:

  • all vertices of even degree ⇒\Rightarrow an Eulerian circuit exists (and every vertex is a possible start);
  • exactly two vertices of odd degree ⇒\Rightarrow an Eulerian trail exists, but no circuit, and it must start at one odd vertex and end at the other;
  • more than two odd vertices ⇒\Rightarrow neither exists. Example: edges AB,AC,BC,BD,CD,CE,DEAB, AC, BC, BD, CD, CE, DE give degrees 2,3,4,3,22,3,4,3,2. Only BB and DD are odd, so there is an Eulerian trail from BB to DD, e.g. B→A→C→B→D→C→E→DB\to A\to C\to B\to D\to C\to E\to D.
Key termsEulerian trailEulerian circuitodd vertex
Exam tip

Count the odd vertices first. It decides everything: 0 gives a circuit, 2 gives a trail, more gives neither.

Section 3

Hamiltonian paths and cycles

A Hamiltonian path visits every vertex exactly once; a Hamiltonian cycle does so and returns to the start. Unlike the Eulerian case, there is no simple rule for deciding whether one exists: you find one by trial, or show none exists by checking that a vertex cannot be reached with the edges available. In a complete graph with n≥3n\ge3 vertices a Hamiltonian cycle always exists. A Hamiltonian cycle need not use every edge.

Key termsHamiltonian pathHamiltonian cycle
Common mistake

Checking only that each vertex appears once. Every consecutive pair in your list must also be an actual edge, including the return to the start.

Section 4

Minimum spanning trees: Kruskal and Prim

A minimum spanning tree (MST) of a connected weighted graph is a tree containing every vertex with the least possible total weight. Kruskal's algorithm: sort the edges by weight; add the smallest edge that does not form a cycle; stop at n−1n-1 edges. Prim's algorithm: start at any vertex; repeatedly add the smallest edge joining a vertex in the tree to a vertex outside; stop when all vertices are included. Kruskal suits a list of edges; Prim suits a table of distances. Prim's matrix method: write the distance matrix. Start at AA: delete row AA and circle column AA entries. Choose the smallest uncrossed entry in the circled columns; its row gives the new vertex, so delete that row and mark its column. Repeat until all rows are deleted. Example: AB=5,AC=3,AD=8,BC=6,BD=4,CD=7AB=5, AC=3, AD=8, BC=6, BD=4, CD=7. Prim from AA adds ACAC (3), ABAB (5), BDBD (4): total 1212. Kruskal gives the same tree.

Key termsminimum spanning treeKruskal's algorithmPrim's algorithmmatrix method
Exam tip

Both algorithms must give the same total weight. If they differ, check for a cycle you missed.

Section 5

The Chinese postman problem

The Chinese postman problem asks for the shortest closed route that traverses every edge at least once. If all vertices are even, the answer is the total weight (an Eulerian circuit). Otherwise some edges must be repeated. Algorithm (up to four odd vertices):

  1. List the odd vertices.
  2. Use a table of least distances if edges are not the shortest connection.
  3. List every way to pair the odd vertices and total the least distances.
  4. Choose the pairing with the least total and repeat those paths.
  5. Route length == total of all edge weights ++ repeated distance. Why it works: the repeated edges make every vertex even, giving an Eulerian circuit, and the least total repeated distance gives the shortest such route. With two odd vertices only one pairing exists; with four odd vertices there are three pairings. Example: AB=9,AC=4,AD=6,BC=3,BD=8,CD=5AB=9, AC=4, AD=6, BC=3, BD=8, CD=5. All four vertices are odd. Pairings: AB&CD=7+5=12AB\&CD=7+5=12, AC&BD=4+8=12AC\&BD=4+8=12, AD&BC=6+3=9AD\&BC=6+3=9. Route =35+9=44=35+9=44.
Key termsChinese postman problemodd verticespairingrepeated edges
Common mistake

Using the direct edge as the distance between two odd vertices. Check for a shorter path through other vertices.

Section 6

Travelling salesman problem: upper and lower bounds

The travelling salesman problem (TSP) asks for the Hamiltonian cycle of least weight in a weighted complete graph. No quick exact algorithm is known, so we find bounds. If the practical problem is not a complete graph (some roads missing, or a shorter route exists via another vertex), first complete a table of least distances to make it a complete graph. Upper bound: nearest neighbour algorithm. From the start, go to the nearest unvisited vertex; repeat; return to the start. The tour is valid, so the optimum is at most its length. Try different starts to improve it. Lower bound: deleted vertex algorithm. Delete a vertex vv; find the MST of the remaining graph; add the two shortest edges incident to vv. Any tour contains such a structure, so the optimum is at least this total. Repeating with each vertex deleted and taking the largest gives the best lower bound. Example: with the distances in the exam question, nearest neighbour from AA gives 6262, deleting EE gives 5454, so 54≤L≤6254\le L\le62.

Key termstravelling salesman problemnearest neighbourupper bounddeleted vertexlower boundtable of least distances
Exam tip

Quote the route and its length for the upper bound, and the MST edges plus the two added edges for the lower bound. Both are needed for the marks.

Section 7

Choosing the right algorithm

  • Need a cheapest connected network (cables, pipes, roads)? Use a minimum spanning tree (Kruskal or Prim).
  • Need to traverse every edge (street cleaning, post)? Chinese postman.
  • Need to visit every vertex once and return (delivery, inspection)? Travelling salesman: give bounds. Justify the choice by stating what must be visited: every edge, every vertex, or just connect everything.
Key termsjustify the algorithm
Exam tip

Write a one-line reason for the algorithm you choose; the guide asks you to justify it.

That's the notes covered.

Carry on to the next subtopic.

Exam questions on 3.16 Graph algorithms: trees, cycles and routes

  1. A connected graph has vertices A,B,C,D,EA, B, C, D, E and edges AB,AC,BC,BD,CD,CE,DEAB, AC, BC, BD, CD, CE, DE.
    Write down an Eulerian trail for this graph.2 marks
  2. A company must connect six sites A,B,C,D,E,FA, B, C, D, E, F with cable. The possible links and their costs, in thousands of AED, are: ABAB 12, ACAC 9, ADAD 13, BCBC 7, BDBD 15, CDCD 10, CECE 14, DEDE 6, DFDF 11, EFEF 8.
    Use Prim's algorithm, starting at AA, to find the order in which the edges are added to the tree.2 marks
  3. A postal worker must walk along every road of an estate with four junctions A,B,C,DA, B, C, D, starting and finishing at AA. The road lengths, in hundreds of metres, are: ABAB 9, ACAC 4, ADAD 6, BCBC 3, BDBD 8, CDCD 5.
    By considering the degree of each junction, explain why some roads must be walked more than once, and find, for each pairing of the odd junctions, the least total distance that must be repeated.3 marks
See the full worksheet

Written by the Exaim team, led by Shaun Daswani (Head of Upper Secondary, Improve ME Institute; MSc Financial Mathematics, Imperial College London; BSc, UCL) and Jason Daswani (operational lead, Improve ME Institute; LSE).