☰ IB Maths AI HL · course menu
Topics
IB Maths AI HL · free cheatsheet
Graph Theory cheatsheet (IB Maths AI HL)
Every graph-theory tool and trap for IB Mathematics Applications & Interpretation HL — adjacency matrices, counting walks with matrix powers, minimum spanning trees (Kruskal & Prim), Dijkstra shortest paths, the Chinese postman problem and travelling-salesman bounds. Written by an IB-certified teacher.
What this cheatsheet covers
Graph theory is the network topic of IB Mathematics Applications & Interpretation HL, and it is AI to the core: a graph is simply a model of a network — roads between towns, cables between servers, connections in a delivery round — stripped down to vertices joined by edges. Once a real situation is drawn as a weighted graph, standard algorithms optimise it: the cheapest network to build, the shortest route to drive, the tour that visits every customer. A calculator is always allowed, so the marks live in choosing the right algorithm and applying it cleanly — sorting edges, tracking labels, reading a matrix power off the GDC — not in abstract proof.
This cheatsheet condenses the whole of AHL 3.14–3.16 — graph terminology and trees, the adjacency matrix, counting walks with matrix powers, minimum spanning trees (Kruskal & Prim), Dijkstra shortest paths, the Chinese postman problem and travelling-salesman bounds — onto one page, and flags the traps that quietly cost method marks.
Graph terminology & trees
AHL 3.14Degree:$\deg(v)=$ number of edges at $v$ — a loop counts twiceSimple graph:no loops and no repeated edges; then each edge just links two distinct verticesComplete graph:$K_n$ joins every pair, so it has $\dfrac{n(n-1)}{2}$ edges and every vertex has degree $n-1$Weighted graph:each edge carries a number (distance, cost, time) to be minimisedDirected graph:edges have a direction; each vertex has an in-degree and an out-degreeTree:a connected graph with no cycles; $n$ vertices are joined by exactly $n-1$ edges
A graph is a set of vertices (nodes) joined by edges. Two vertices are adjacent if an edge joins them, and the degree of a vertex is the number of edge-ends meeting it.
The adjacency matrix
AHL 3.15Degree:$\deg(i)=$ the sum of row $i$ (here $2,3,4,3,2$) — read straight off the matrixWeighted table:replace each $1$ with the edge's weight to get the weighted adjacency table used by the algorithms below
A graph is stored for the GDC as its adjacency matrix $A$, where entry $A_{ij}$ counts the edges joining vertex $i$ to vertex $j$. Rows and columns follow the same vertex order.
Ignoring the weights, the graph above has adjacency matrix (order $A,B,C,D,E$):
$$A=\begin{pmatrix}0&1&1&0&0\\1&0&1&1&0\\1&1&0&1&1\\0&1&1&0&1\\0&0&1&1&0\end{pmatrix}$$
The handshake lemma
AHL 3.15Handshake lemma:$\displaystyle\sum_i \deg(i)=2E$, where $E$ is the number of edgesEdge count:$E=\tfrac12\displaystyle\sum_i \deg(i)$ — halve the degree sum
Every edge has two ends, and each end adds $1$ to a vertex's degree. Summing over all vertices therefore double-counts the edges:
For the graph in §2 the degrees are $2,3,4,3,2$, summing to $14=2\times 7$, confirming $E=7$ edges.
Counting walks with matrix powers
AHL 3.15Walks:$(A^k)_{ij}=$ the number of walks of length $k$ from vertex $i$ to vertex $j$Length 2:$(A^2)_{ij}$ counts the common neighbours of $i$ and $j$; $(A^2)_{ii}=\deg(i)$
Raising the adjacency matrix to a power counts routes through the network — a pure GDC task, no path-tracing by hand.
For the §2 graph, squaring gives
$$A^2=\begin{pmatrix}2&1&1&2&1\\1&3&2&1&2\\1&2&4&2&1\\2&1&2&3&1\\1&2&1&1&2\end{pmatrix}$$
so there are $(A^2)_{AD}=2$ walks of length 2 from $A$ to $D$ (via $B$ and via $C$). Cubing on the GDC gives $(A^3)_{AB}=5$ walks of length 3 from $A$ to $B$.
Minimum spanning tree: Kruskal & Prim
AHL 3.16Kruskal:sort every edge in ascending weight; add the cheapest edge that does not create a cycle; stop at $n-1$ edgesPrim:start at any vertex; repeatedly add the cheapest edge that joins a new vertex to the tree so far
A minimum spanning tree (MST) connects all $n$ vertices as cheaply as possible: it is a tree ($n-1$ edges, no cycles) of least total weight — the cheapest network that still reaches everywhere. Two algorithms both find it.
On the §2 graph, Kruskal takes $DE(2),AC(3),BC(4)$, rejects $AB(5)$ as a cycle, then adds $CD(7)$ to reach all five vertices: MST weight $=2+3+4+7=16$.
Shortest path: Dijkstra's algorithm
AHL 3.16Label:give each vertex a "best distance so far" from the start; the start begins at $0$Fix & relax:permanently fix the smallest un-fixed label, then update ("relax") each of its neighbours; repeat
Where the MST builds the cheapest whole network, Dijkstra's algorithm finds the single cheapest route between two chosen vertices in a weighted graph.
From $A$ in the §2 graph the shortest distance to $E$ is $11$, along $A\to C\to E$ ($3+8$) — cheaper than the direct-looking $A\to C\to D\to E=12$.
Chinese postman (route inspection)
AHL 3.16Eulerian circuit:if every vertex has even degree, such a route reuses no edge — length $=$ total edge weightTwo odd vertices:repeat the shortest path between the odd pair; route $=$ total weight $+$ that shortest distance
The Chinese postman problem asks for the shortest closed route that travels every edge at least once and returns to the start (think gritting every road, or inspecting every cable).
The §2 graph has odd vertices $B$ and $D$. The shortest $B$–$D$ path is $B\to C\to D=11$ (not the direct edge, weight $12$), and the edges total $41$, so the postman route is $41+11=52$.
Travelling salesman: upper & lower bounds
AHL 3.16Tour count:the complete graph $K_n$ has $\dfrac{(n-1)!}{2}$ distinct Hamiltonian cycles — far too many to check for large $n$Upper bound:nearest-neighbour — from the start, always go to the closest unvisited vertex, then return to the startLower bound:deleted-vertex — delete one vertex, find the MST of what remains, then add the two cheapest edges at the deleted vertex
The travelling-salesman problem (TSP) seeks the cheapest tour that visits every vertex exactly once and returns to the start — a Hamiltonian cycle of least weight. There is no quick exact method, so AI HL asks you to trap the answer between two bounds.
Synthesis: random walks & steady state
AHL 3.15Transition:from a degree-$d$ vertex, move to each neighbour with probability $\tfrac1d$ (e.g. from $A$, degree $2$: to $B$ or $C$, each $\tfrac12$)Steady state:$\pi_i=\dfrac{\deg(i)}{2E}=\dfrac{\deg(i)}{\sum_k \deg(k)}$ — the long-run share of time spent at vertex $i$
Graph theory feeds straight into the Markov-chain toolkit. Let a walker hop from its current vertex to a uniformly random neighbour. The transition matrix reads straight off the graph: from a vertex of degree $d$, each neighbour is reached with probability $\tfrac{1}{d}$.
For the §2 graph, $\pi=\dfrac{1}{14}(2,3,4,3,2)$, so the walker spends $\tfrac{4}{14}=\tfrac27$ of its time at the hub $C$ — the most-connected vertex, as expected.
Exam attack plan
All sections
| Question cue | What to do | Watch for |
|---|---|---|
| "Degree of a vertex" | Sum its row in $A$ (or count edge-ends) | A loop adds 2; directed $\Rightarrow$ in- vs out-degree |
| "How many edges?" | Handshake: $E=\tfrac12\sum\deg(i)$ | The degree sum must be even |
| "Walks of length $n$ from $i$ to $j$" | Compute $A^{n}$ on the GDC; read entry $(i,j)$ | Power $n$, not $n-1$; walks may repeat vertices |
| "Cheapest network / spanning tree" | Kruskal (sort edges) or Prim (grow from a vertex) | $n-1$ edges; never close a cycle |
| "Shortest route between two vertices" | Dijkstra: fix smallest label, relax neighbours | Least weight, not fewest edges |
| "Travel every road and return" | Chinese postman: total weight $+$ shortest odd-pair path | Repeated path may pass through another vertex |
| "Shortest tour of every vertex" | TSP: nearest-neighbour (UB) & deleted-vertex (LB) | Include the return leg; two edges at the deleted vertex |
| "Long-run share of a random walk" | Steady state $\pi_i=\deg(i)/2E$ | Degree-weighted; divide by $2E$, not $E$ |
Worked Example — AI HL-Style Travelling Salesman
Question (AI HL Paper 2 style — 6 marks)
A courier must start at hub $P$, visit hubs $Q$, $R$, $S$, $T$ exactly once, and return to $P$. The distances (km) between hubs are:
| P | Q | R | S | T | |
|---|---|---|---|---|---|
| P | – | 9 | 12 | 8 | 15 |
| Q | 9 | – | 7 | 11 | 6 |
| R | 12 | 7 | – | 10 | 13 |
| S | 8 | 11 | 10 | – | 5 |
| T | 15 | 6 | 13 | 5 | – |
(a) Use the nearest-neighbour algorithm from $P$ to find an upper bound for the shortest tour.
(b) By deleting vertex $P$, find a lower bound for the shortest tour.
Solution
- (a) Nearest-neighbour from $P$: the closest hub to $P$ is $S$ ($8$); from $S$ the closest unvisited is $T$ ($5$); from $T$ it is $Q$ ($6$); from $Q$ only $R$ remains ($7$); finally $R\to P$ ($12$). (M1) Tour $P\to S\to T\to Q\to R\to P$. (A1) Length $=8+5+6+7+12=38$ km, so the shortest tour is at most $\mathbf{38}$ km. (A1)
- (b) Delete $P$. On the remaining vertices $\{Q,R,S,T\}$ the minimum spanning tree (Kruskal) takes $ST(5),\,QT(6),\,QR(7)$, of weight $5+6+7=18$. (M1)(A1) Now add the two cheapest edges at $P$: $PS=8$ and $PQ=9$. Lower bound $=18+8+9=35$ km. (A1)
Examiner's note: quote both bounds together — $35\le$ shortest tour $\le 38$ km. Nearest-neighbour gives a genuine tour, so remember its return leg $R\to P$; the deleted-vertex method needs the two smallest edges at the deleted vertex, both incident to it. Different start or deleted vertices legitimately give different bounds — pick the highest lower bound and lowest upper bound to squeeze hardest, and keep the units (km).
Common Student Questions
- For walks of length $n$, do I raise the adjacency matrix to the power $n$ or $n-1$?
- What is the difference between Kruskal's and Prim's algorithms?
- When does a graph have an Eulerian circuit, and how does the Chinese postman use it?
- Which travelling-salesman method gives the upper bound and which the lower?
- What is the difference between Eulerian and Hamiltonian — and between a walk, trail and path?
For walks of length $n$, do I raise the adjacency matrix to the power $n$ or $n-1$?
What is the difference between Kruskal's and Prim's algorithms?
When does a graph have an Eulerian circuit, and how does the Chinese postman use it?
Which travelling-salesman method gives the upper bound and which the lower?
What is the difference between Eulerian and Hamiltonian — and between a walk, trail and path?
Your next step




