Skip to content
IB Diploma ProgrammeSL & HL

Graph algorithms

Mathematics: Applications and Interpretation SL & HL Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Graph algorithms notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Graph algorithms notes as text: skim, search, and jump between subtopics.

~24 min read

1. Walks, trails, paths, circuits and cycles

A walk is a sequence of edges in which each edge starts where the last one ended. The names below say what a walk may not repeat.

A trail is a walk with no repeated edge. A path is a walk with no repeated vertex. A circuit is a closed trail: it starts and ends at the same vertex. A cycle is a closed path: it starts and ends at the same vertex and repeats no other vertex.

Every path is a trail, and every cycle is a circuit. A circuit may pass through a vertex twice, so a circuit need not be a cycle.

Worked example 14 marks

The graph shown has vertices A, B, C, D and E. Four walks are listed.

W1: A-B-C-A

W2: A-B-C-D-E-C-A

W3: B-C-D-E-C-A

W4: A-B-C-B

State which walk is

(a) a cycle

(b) a circuit that is not a cycle

(c) a trail that is not a circuit

(d) a walk that is not a trail.

  1. 1

    (a) A1: W1: it starts and ends at A and repeats no other vertex [1]

  2. 2

    (b) A1: W2: no edge is repeated and it is closed, but vertex C is visited twice [1]

  3. 3

    (c) A1: W3: no edge is repeated, but it starts at B and ends at A, so it is not closed [1]

  4. 4

    (d) A1: W4: edge BC is used twice [1]

2. Trees and spanning trees

A tree is a connected graph with no cycles. A tree with n vertices has n - 1 edges.

A spanning tree of a connected graph is a tree made from edges of that graph that joins every vertex. So a spanning tree of a graph with n vertices has n - 1 edges.

The algorithms in this note build trees. Each one adds an edge only if it does not close a cycle.

edges in a tree with n vertices = n - 1

edges to remove = e - (n - 1)

Worked example 14 marks

A connected graph has 9 vertices and 14 edges.

(a) Write down the number of edges in a spanning tree of the graph.

(b) Find the number of edges that must be removed to leave a spanning tree.

(c) Explain why a connected graph with 9 vertices cannot have only 7 edges.

  1. 1

    (a) A1: 9 - 1 = 8 [1]

  2. 2

    (b) M1: 14 - 8 [1]

  3. 3

    A1: 6 edges [1]

  4. 4

    (c) R1: a connected graph contains a spanning tree, which needs 8 edges, and 7 < 8 [1]

3. Eulerian and Hamiltonian routes

The degree of a vertex is the number of edges that meet at it. An Eulerian trail uses every edge exactly once. An Eulerian circuit is an Eulerian trail that starts and ends at the same vertex.

In a connected graph, an Eulerian circuit exists when every vertex has even degree. An Eulerian trail that is not a circuit exists when exactly two vertices have odd degree: it starts at one and ends at the other. With more than two odd vertices there is no Eulerian trail. So to determine whether an Eulerian trail or circuit exists, write down the degree of every vertex and count the odd ones.

A Hamiltonian path visits every vertex exactly once. A Hamiltonian cycle is a Hamiltonian path that returns to its start. It need not use every edge, and there is no degree test for it: find one, or show that none exists.

Worked example 14 marks

The graph shown has vertices A, B, C and D. Its edges are AB, BC, CD, DA and AC.

(a) Determine whether the graph has an Eulerian circuit, an Eulerian trail that is not a circuit, or neither.

(b) Write down a Hamiltonian cycle.

  1. 1

    (a) M1: degrees: A has 3, B has 2, C has 3, D has 2 [1]

  2. 2

    R1: exactly two vertices (A and C) have odd degree [1]

  3. 3

    A1: an Eulerian trail that is not a circuit exists, for example A-B-C-D-A-C [1]

  4. 4

    (b) A1: A-B-C-D-A [1]

4. Kruskal's algorithm

Kruskal's algorithm finds a minimum spanning tree (MST): a spanning tree of least total weight.

List the edges in increasing order of weight. Take the next edge in the list. Add it unless it forms a cycle with the edges already chosen, and reject it if it does. Stop when n - 1 edges have been chosen.

Part-way through, the chosen edges may form several separate pieces. Only the final set must be connected. An edge is rejected when its two ends are already joined by a chosen path.

weight of the MST = sum of the n - 1 chosen weights

Worked example 15 marks

The weighted graph shown has vertices A, B, C, D, E and F. Use Kruskal's algorithm to find a minimum spanning tree. State the edges in the order they are chosen, and the total weight of the tree.

  1. 1

    M1: list the edges in order: EF 3, CD 4, AB 5, BD 6, BC 7, DE 8, AC 9, CE 11, DF 12 [1]

  2. 2

    A1: choose EF (3), CD (4), AB (5) and BD (6): none forms a cycle [1]

  3. 3

    R1: reject BC (7): B and C are already joined by B-D-C, so BC would form a cycle [1]

  4. 4

    A1: choose DE (8): 5 edges for 6 vertices, so stop [1]

  5. 5

    A1: total weight 3 + 4 + 5 + 6 + 8 = 26 [1]

5. Prim's algorithm and the matrix method

Prim's algorithm grows one tree from a start vertex. At each step it adds the cheapest edge that joins a vertex in the tree to a vertex not yet in it.

In the matrix method, work on the distance matrix. Write 1 above the column of the start vertex and cross out its row. Look down every numbered column and circle the smallest entry in a row that is not crossed out. The row of that entry is the new vertex: number its column next and cross out its row. Stop when every row is crossed out.

The circled entries are the edges of the MST. Prim's and Kruskal's algorithms give a tree of the same total weight.

Worked example 15 marks

A weighted graph has vertices A to F. Its weights are given in the matrix, where a dash means there is no edge.

Use Prim's algorithm on the matrix, starting at A, to find a minimum spanning tree. State the edges in the order they are chosen, and the total weight.

ABCDEF
A–59–––
B5–76––
C97–411–
D–64–812
E––118–3
F–––123–
  1. 1

    M1: number column A and cross out row A. The smallest entry in column A is 5, in row B [1]

  2. 2

    A1: choose AB (5), then cross out row B. The smallest entry in columns A and B is 6, in row D: choose BD (6) [1]

  3. 3

    A1: choose CD (4), then DE (8): in each step take the smallest entry in all numbered columns [1]

  4. 4

    A1: choose EF (3): every row is now crossed out [1]

  5. 5

    A1: total weight 5 + 6 + 4 + 8 + 3 = 26 [1]

6. The Chinese postman problem

The Chinese postman problem asks for the shortest route that uses every edge at least once and returns to its start. If every vertex is even, there is an Eulerian circuit and the route length is the total weight of the edges.

If some vertices have odd degree, some edges must be repeated. List the odd vertices and pair them in every possible pairing: two odd vertices give one pairing, four give three. For each pairing add the shortest distances between the paired vertices, and choose the least sum. The route length is the total weight plus that least sum.

Why it works: a route that returns to its start enters and leaves each vertex equally often, so every vertex needs even degree. Repeating a shortest path between two odd vertices makes both even, so the least pairing sum gives the least repeated length. Choose this algorithm when every edge must be used. Use Kruskal or Prim to join all vertices at least cost, and the travelling salesman problem to visit every vertex once.

route = total weight of all edges + least pairing sum

Worked example 16 marks

The weighted graph shown has vertices A, B, C and D. A road inspector must travel along every road at least once, starting and ending at A. Determine the length of the shortest route.

  1. 1

    M1: A, B, C and D each have degree 3, so all four are odd vertices [1]

  2. 2

    M1: the three pairings are AB and CD, AC and BD, AD and BC [1]

  3. 3

    A1: AB + CD = 4 + 8 = 12 and AD + BC = 5 + 3 = 8 [1]

  4. 4

    A1: AC + BD: the shortest route from A to C is A-B-C = 4 + 3 = 7, not the direct edge 8, so 7 + 6 = 13 [1]

  5. 5

    R1: the least sum is 8, so repeat edges AD and BC [1]

  6. 6

    A1: total weight 4 + 8 + 5 + 3 + 6 + 8 = 34, so the route is 34 + 8 = 42 [1]

7. Travelling salesman: nearest neighbour upper bound

The travelling salesman problem asks for the Hamiltonian cycle of least weight in a weighted complete graph: visit every vertex once and return to the start.

A practical problem is first converted to the classical one. Where two places have no direct road, or a direct road longer than another route, write the shortest route between them in a table of least distances. Then every pair of vertices is joined.

The nearest neighbour algorithm starts at a chosen vertex, moves to the nearest unvisited vertex each time, and finally returns to the start. The cycle it gives is a real cycle, so the least weight cannot be more than its length: it is an upper bound. It need not be the least weight, and a different start vertex may give a smaller upper bound.

least weight ≤ length of any Hamiltonian cycle

Worked example 15 marks

The table gives the direct road distances, in km, between five villages A, B, C, D and E. A dash means there is no direct road.

(a) Complete the table of least distances for the four missing entries.

(b) Use the nearest neighbour algorithm, starting at A, to find an upper bound for the length of the shortest route that visits every village once and returns to A.

ABCDE
A–58––
B5–47–
C84––6
D–7––3
E––63–
  1. 1

    (a) M1: for each missing entry, use the shortest route through the other villages [1]

  2. 2

    A1: AD = 12 (A-B-D: 5 + 7) and CD = 9 (C-E-D: 6 + 3) [1]

  3. 3

    A1: AE = 14 (A-C-E: 8 + 6) and BE = 10 (B-C-E: 4 + 6) [1]

  4. 4

    (b) M1: A to B (5), B to C (4), C to E (6), E to D (3), then D to A (12) [1]

  5. 5

    A1: upper bound 5 + 4 + 6 + 3 + 12 = 30 [1]

8. Travelling salesman: deleted vertex lower bound

The deleted vertex algorithm gives a lower bound: the least weight of a Hamiltonian cycle cannot be smaller.

Delete one vertex and its edges. Find a minimum spanning tree of the remaining vertices, then add the two smallest weights of edges at the deleted vertex. The sum is the lower bound.

Why it works: remove a vertex from a Hamiltonian cycle and a path through the other vertices is left, which weighs at least as much as their MST. The cycle also uses exactly two edges at the deleted vertex, and together they weigh at least the two smallest. Deleting a different vertex can give a larger, better lower bound.

lower bound = MST of the remaining vertices + two smallest edges at the deleted vertex

lower bound ≤ least weight ≤ upper bound

Worked example 15 marks

The table gives the least distances between five villages A, B, C, D and E. The nearest neighbour algorithm, starting at A, gave the cycle A-B-C-E-D-A of length 30.

(a) Use the deleted vertex algorithm, deleting A, to find a lower bound for the travelling salesman problem.

(b) Write down the interval that contains the least weight of a Hamiltonian cycle.

ABCDE
A–581214
B5–4710
C84–96
D1279–3
E141063–
  1. 1

    (a) M1: delete A. Use Kruskal on B, C, D, E: DE (3), BC (4), CE (6) [1]

  2. 2

    A1: MST weight 3 + 4 + 6 = 13 [1]

  3. 3

    M1: add the two smallest weights at A: AB (5) and AC (8) [1]

  4. 4

    A1: lower bound 13 + 5 + 8 = 26 [1]

  5. 5

    (b) A1: 26 ≤ least weight ≤ 30 [1]

Recap

  • A trail repeats no edge and a path repeats no vertex. A circuit is a closed trail and a cycle is a closed path.
  • A tree is connected with no cycles. A spanning tree of a graph with n vertices has n - 1 edges.
  • Eulerian circuit: every vertex even. Eulerian trail that is not a circuit: exactly two odd vertices. A Hamiltonian cycle visits every vertex once and need not use every edge.
  • Kruskal: add the cheapest edge that does not form a cycle. Prim: grow from a start vertex, adding the cheapest edge to a new vertex. Both give an MST.
  • Chinese postman: pair the odd vertices every possible way using shortest distances, then add the least sum to the total weight.
  • Travelling salesman: nearest neighbour gives an upper bound. The deleted vertex algorithm gives a lower bound: MST of the rest plus the two smallest edges at the deleted vertex.
  • Choose by the task: every edge at least once is Chinese postman, every vertex once is travelling salesman, join all vertices at least cost is an MST.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. State the name of a walk that starts and ends at the same vertex and repeats no edge.1 mark
    Answer
    1. A circuit.

    Trap: Answering cycle. A cycle must also repeat no vertex, which this walk does not promise.

  2. A connected graph has 7 vertices and 10 edges. Write down the number of edges in a spanning tree of the graph. A. 5 B. 6 C. 7 D. 101 mark
    Answer
    1. The answer is B: a spanning tree of a graph with n vertices has n - 1 = 6 edges.
    2. A is n - 2. C is the number of vertices. D is the number of edges in the whole graph.

    Trap: Choosing 7, the number of vertices, or 10, the number of edges of the whole graph.

  3. Do not use a calculator. A graph has vertices A, B, C, D and E. Its edges are AB, BC, CD, DE, EA and AC. (a) Determine whether the graph has an Eulerian circuit, an Eulerian trail that is not a circuit, or neither. (b) Write down a Hamiltonian cycle.4 marks
    Answer
    1. (a)
    2. Degrees: A has 3, B has 2, C has 3, D has 2, E has 2.
    3. Exactly two vertices, A and C, have odd degree, so there is an Eulerian trail that is not a circuit.
    4. (b)
    5. A-B-C-D-E-A. It visits every vertex once and does not use edge AC.

    Trap: Thinking a Hamiltonian cycle must use every edge, and so rejecting A-B-C-D-E-A because AC is left out.

  4. Do not use a calculator. A weighted graph has vertices A to E. The weights of its edges are in the table. Use Kruskal's algorithm to find a minimum spanning tree. State the edges chosen, in order, and the total weight. | Edge | DE | AB | AC | BC | CD | CE | BD | | --- | --- | --- | --- | --- | --- | --- | --- | | Weight | 3 | 4 | 5 | 6 | 7 | 8 | 9 |4 marks
    Answer
    1. Order: DE 3, AB 4, AC 5, BC 6, CD 7, CE 8, BD 9.
    2. Choose DE (3), AB (4) and AC (5).
    3. Reject BC (6): A, B and C are already joined, so it would form a cycle.
    4. Choose CD (7). This joins D and E to the rest, giving 4 edges for 5 vertices.
    5. Total weight 3 + 4 + 5 + 7 = 19.

    Trap: Adding BC (6) because it is next in the list without checking for a cycle. Edges AB, AC and BC form a cycle, and D and E are never joined to A, B and C.

  5. Do not use a calculator. A weighted graph has vertices P, Q, R, S and T. Its weights are given in the matrix, where a dash means there is no edge. Use Prim's algorithm on the matrix, starting at P, to find a minimum spanning tree. State the edges chosen, in order, and the total weight. | | P | Q | R | S | T | | --- | --- | --- | --- | --- | --- | | P | – | 3 | – | 4 | – | | Q | 3 | – | 6 | – | – | | R | – | 6 | – | 8 | 7 | | S | 4 | – | 8 | – | 5 | | T | – | – | 7 | 5 | – |4 marks
    Answer
    1. Cross out row P. The smallest entry in column P is 3: choose PQ.
    2. Cross out row Q. Columns P and Q give 4 (row S) and 6 (row R): choose PS (4).
    3. Cross out row S. Columns P, Q and S give 6 (row R), 8 (row R) and 5 (row T): choose ST (5).
    4. Cross out row T. Choose QR (6), the smallest left, then every row is crossed out.
    5. Order: PQ, PS, ST, QR. Total weight 3 + 4 + 5 + 6 = 18.

    Trap: Moving on from the vertex just added, so taking QR (6) straight after PQ. That gives PQ, QR, RT, TS with total weight 21, which is not minimum.

  6. Do not use a calculator. A weighted graph has vertices A, B, C and D. The lengths of its edges, in km, are in the table. A route must use every edge at least once and start and end at A. Determine the length of the shortest route. | Edge | AB | BC | CD | DA | BD | | --- | --- | --- | --- | --- | --- | | Length / km | 4 | 3 | 5 | 6 | 9 |4 marks
    Answer
    1. Degrees: A 2, B 3, C 2, D 3. The odd vertices are B and D, so there is one pairing.
    2. The shortest route from B to D is B-C-D = 3 + 5 = 8 km, shorter than the direct edge BD of 9 km (B-A-D is 10 km).
    3. Total weight 4 + 3 + 5 + 6 + 9 = 27 km.
    4. Route length 27 + 8 = 35 km.

    Trap: Repeating the direct edge BD (9 km) and giving 36 km, instead of repeating the shortest route from B to D.

  7. Do not use a calculator. The table gives the direct road distances, in km, between four villages W, X, Y and Z. A dash means there is no direct road. A van starts at W, visits every village once and returns to W. (a) Find the least distance between X and Z. (b) Use the nearest neighbour algorithm, starting at W, to find an upper bound. | | W | X | Y | Z | | --- | --- | --- | --- | --- | | W | – | 7 | 6 | 9 | | X | 7 | – | 3 | – | | Y | 6 | 3 | – | 5 | | Z | 9 | – | 5 | – |4 marks
    Answer
    1. (a)
    2. X and Z have no direct road. X-Y-Z = 3 + 5 = 8 km is shorter than X-W-Z = 7 + 9 = 16 km, so the least distance is 8 km.
    3. (b)
    4. From W the nearest village is Y (6). From Y the nearest unvisited village is X (3), then Z (8), then back to W (9).
    5. Upper bound 6 + 3 + 8 + 9 = 26 km.

    Trap: Leaving out the final leg from Z back to W, which gives 17 km. The route must return to the start.

  8. Do not use a calculator. The table gives the least distances between four towns K, L, M and N. Use the deleted vertex algorithm, deleting M, to find a lower bound for the travelling salesman problem. | | K | L | M | N | | --- | --- | --- | --- | --- | | K | – | 8 | 5 | 9 | | L | 8 | – | 6 | 4 | | M | 5 | 6 | – | 7 | | N | 9 | 4 | 7 | – |4 marks
    Answer
    1. Delete M. The edges among K, L and N are KL 8, KN 9 and LN 4.
    2. Minimum spanning tree: LN (4) and KL (8), weight 12.
    3. The two smallest weights at M are MK (5) and ML (6).
    4. Lower bound 12 + 5 + 6 = 23.

    Trap: Adding only the smallest edge at M, or the two largest. The algorithm adds the two smallest.

  9. A park warden must walk every path in a nature reserve at least once, starting and ending at the entrance A. The table gives the lengths of the paths between junctions A to E, in km. Determine the length of the shortest route. | Path | AC | CD | BD | BC | BE | CE | DE | | --- | --- | --- | --- | --- | --- | --- | --- | | Length / km | 2 | 2 | 2 | 5 | 3 | 6 | 9 |6 marks
    Answer
    1. Degrees: A 1, B 3, C 4, D 3, E 3. The odd vertices are A, B, D and E.
    2. Shortest distances: AB = 6 (A-C-D-B), AD = 4 (A-C-D), AE = 8 (A-C-E), BD = 2, BE = 3, DE = 5 (D-B-E, not the direct 9).
    3. Pairings: AB + DE = 6 + 5 = 11. AD + BE = 4 + 3 = 7. AE + BD = 8 + 2 = 10.
    4. The least is 7, so repeat the routes A-C-D and B-E.
    5. Total weight 2 + 2 + 6 + 9 + 3 + 5 + 2 = 29 km.
    6. Route length 29 + 7 = 36 km.

    Trap: Pairing the two closest odd vertices, B and D (2 km), first. That leaves A with E (8 km), a pairing sum of 10 and a route of 39 km. Try every pairing.

  10. A mobile library leaves the depot A, visits villages B, C, D and E once each and returns to A. The table gives the direct road distances, in km. A dash means there is no direct road. (a) Complete the table of least distances. (b) Use the nearest neighbour algorithm, starting at A, to find an upper bound. (c) Use the deleted vertex algorithm, deleting A, to find a lower bound. (d) Write down the interval that contains the least length of a route. | | A | B | C | D | E | | --- | --- | --- | --- | --- | --- | | A | – | 9 | 4 | 8 | – | | B | 9 | – | 3 | 6 | – | | C | 4 | 3 | – | – | 7 | | D | 8 | 6 | – | – | 5 | | E | – | – | 7 | 5 | – |8 marks
    Answer
    1. (a)
    2. Missing entries: AE = 11 km (A-C-E: 4 + 7), BE = 10 km (B-C-E: 3 + 7), CD = 9 km (C-B-D: 3 + 6).
    3. Check the given roads too: AB is 9 km direct, but A-C-B = 4 + 3 = 7 km, so AB = 7 km in the table of least distances.
    4. (b)
    5. From A the nearest village is C (4). From C: B (3). From B: D (6). From D: E (5). Back to A (11).
    6. Upper bound 4 + 3 + 6 + 5 + 11 = 29 km.
    7. (c)
    8. Delete A. Kruskal on B, C, D, E: BC (3), DE (5), BD (6). Weight 3 + 5 + 6 = 14.
    9. The two smallest weights at A are AC (4) and AB (7).
    10. Lower bound 14 + 4 + 7 = 25 km.
    11. (d)
    12. 25 ≤ least length ≤ 29 km.

    Trap: Filling only the dashes and keeping the direct road AB (9 km). A table of least distances holds the shortest route for every pair, so AB is 7 km; with 9 km the lower bound becomes 26 km, which is wrong.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
A circuit and a cycle are the same thing.A circuit is a closed trail: no edge is repeated. A cycle is a closed path: no vertex is repeated either. Every cycle is a circuit, but not every circuit is a cycle.
There is no Eulerian circuit, so there is no Eulerian route at all.A connected graph with exactly two odd vertices has an Eulerian trail. It starts at one odd vertex and ends at the other.
Nearest neighbour finds the shortest route.Nearest neighbour gives an upper bound for the travelling salesman problem. The shortest route may be smaller.
Delete a vertex: add the smallest edge at that vertex to the MST.Add the two smallest edges at the deleted vertex to the MST of the remaining vertices.

Frequently asked questions

What is the difference between a trail, a path, a circuit and a cycle?
A trail is a walk with no repeated edge, and a path is a walk with no repeated vertex. A circuit is a closed trail, starting and ending at the same vertex, and a cycle is a closed path that repeats no other vertex. Every path is a trail and every cycle is a circuit. A circuit may pass through a vertex twice, so it need not be a cycle.
How many edges does a spanning tree have?
A spanning tree joins every vertex of a connected graph with no cycles, so a spanning tree with n vertices has n - 1 edges. For a connected graph with 9 vertices and 14 edges, the spanning tree has 9 - 1 = 8 edges, so 14 - 8 = 6 edges must be removed. A connected graph with 9 vertices cannot have only 7 edges, because 7 < 8.
How do I tell whether a graph has an Eulerian circuit or an Eulerian trail?
Write down the degree of every vertex and count the odd ones. An Eulerian circuit exists in a connected graph when every vertex has even degree. An Eulerian trail that is not a circuit exists when exactly two vertices have odd degree, and it starts at one and ends at the other. With more than two odd vertices there is no Eulerian trail. For a graph with odd vertices A and C, an Eulerian trail such as A-B-C-D-A-C exists.
How do I use Kruskal's algorithm to find a minimum spanning tree?
List the edges in increasing order of weight, then take each edge in turn. Add it unless it forms a cycle with the edges already chosen, and stop when n - 1 edges have been chosen. The weight of the MST is the sum of those chosen weights. In one example the edges EF, CD, AB, BD and DE are chosen, giving a total weight of 3 + 4 + 5 + 6 + 8 = 26. The edge BC is rejected because it would form a cycle.
Why is my Chinese postman route too short when I pair up the odd vertices?
Pairing odd vertices must use the shortest route between each pair, not always the direct edge. Between A and C the route A-B-C has length 4 + 3 = 7, which is shorter than the direct edge 8. Add the total weight of all edges to the least pairing sum, 5 + 3 = 8 for AD and BC, to get 34 + 8 = 42 for the route that uses every road.
How do I find upper and lower bounds for the travelling salesman problem?
The nearest neighbour algorithm gives an upper bound: it moves to the nearest unvisited vertex each time and returns to the start, and the least weight cannot be more than that cycle's length. The deleted vertex algorithm gives a lower bound: find a minimum spanning tree of the remaining vertices, then add the two smallest weights of edges at the deleted vertex. For the five villages, the upper bound is 30 km.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters