Graph theory & adjacency matrices
A graph is a set of dots (vertices) joined by lines (edges). Graphs model road maps, computer networks, friendships and the links between web pages. Storing a graph as a matrix lets the GDC count routes through it, and a transition matrix ranks web pages the way early search engines did.
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 vertex, edge, adjacent, degree, simple, complete, weighted, subgraph and treeAHL 3.14
- Use directed graphs, with in-degree and out-degreeAHL 3.14
- Decide whether a graph is connected, or a directed graph strongly connectedAHL 3.14
- Write down the adjacency matrix of a graph, and draw a graph from oneAHL 3.15
- Use to count walks of length , and sums of powers for walks of length up to AHL 3.15
- Use weighted adjacency tablesAHL 3.15
- Build the transition matrix for a strongly connected graph and use its steady state to rank verticesAHL 3.15
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.
A graph keeps only what matters about a network: what is connected to what. Put the connections in a matrix , and something remarkable happens: the entries of count the two-step routes, the entries of count the three-step routes, and so on.
2.1 The language of graphs
Graph
| Term | Meaning |
|---|---|
| Adjacent vertices | joined by an edge (in , and are adjacent; and are not) |
| Adjacent edges | edges that share a vertex |
| Degree of a vertex | the number of edge-ends at it (in : 2, 3, 3, 3, 1) |
| Simple graph | no loops and no multiple edges between the same pair of vertices |
| Complete graph | every pair of the vertices is joined by exactly one edge |
| Connected | there is a route between every pair of vertices |
| Weighted graph | each edge carries a number: a distance, cost or time |
| Subgraph | a graph made from some of the vertices and edges of another |
| Tree | a connected graph with no cycles; a tree with vertices has edges |
Each edge has two ends, so the sum of the degrees is twice the number of edges (in : ). It follows that the number of odd vertices is always even.
In a directed graph the edges have arrows. The out-degree of a vertex counts the arrows leaving it and the in-degree counts those arriving. A directed graph is strongly connected if you can travel, following the arrows, from every vertex to every other vertex.
2.2 Adjacency matrices
The adjacency matrix has a row and a column for each vertex; the entry in row , column is the number of edges from to . For an undirected graph it is symmetric. A loop counts 1 on the diagonal; a pair of parallel edges gives an entry of 2. For a directed graph, read rows as "from" and columns as "to" (unless the question says otherwise).
2.3 Counting walks
A walk is any sequence of edges, each starting where the last one ended; vertices and edges may repeat. The entry of is the number of walks of length from to . The entry of counts the walks of length at most .
2.4 Weighted tables and transition matrices
A weighted adjacency table stores the weight of each edge in place of the 1 (leave a blank or a dash where there is no edge). These tables feed the algorithms on the next page.
Imagine a surfer who, on each page, clicks one of its links at random. For a strongly connected graph, the transition matrix has, in column , the probabilities of moving from vertex to each vertex: for each link. Each column adds to 1. The steady state (with , entries adding to 1) gives the long-run share of time spent at each vertex: the bigger the share, the higher the page ranks. This is the idea behind Google’s original PageRank.
Four web pages and their links
On the TI-Nspire, store the adjacency matrix as a and type a^3 to count walks of length 3, or a+a^2+a^3 for "at most 3". For a transition matrix t, either compute t^50·[[0.25][0.25][0.25][0.25]] and check that t^51 gives the same, or use eigVc(t) and scale the eigenvector for so its entries add to 1.
3Explore it
This is graph G from the notes. Choose a start vertex, an end vertex and a length k. The readout counts the walks using powers of the adjacency matrix.
4Common mistakes & exam tips
Common mistakes
- Reading a directed adjacency matrix the wrong way round. Check what the rows mean in the question.
- Counting walks of length 2 or 3 from or alone when the question says "at most" or "fewer than".
- Building a transition matrix whose rows, not columns, add to 1 (unless the question uses that convention).
- Calling a directed graph strongly connected when some vertex can be reached but cannot be left.
- Forgetting that a loop adds 2 to the degree of its vertex.
Exam tips
- "Fewer than four edges" means lengths 1, 2 and 3: add .
- For "draw the graph" from a matrix, check every 1 above the diagonal is one edge.
- Justify "strongly connected" by writing a cycle (or routes) that visits every vertex following the arrows.
- Steady-state answers should add to 1; round to 3 s.f. only at the end.
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.
The graph is shown.
- (a)State, with a reason, whether is simple.[1]
- (b)Write down the adjacency matrix of .[2]
- (c)Find the sum of the degrees of the vertices of .[1]
- (d)Find the number of walks from to with fewer than five edges.[3]
| (a) | |
| not simple, because there are two edges joining and | R1 |
| (b) | |
| A2 | |
| A1 for one or two errors. | |
| (c) | |
| A1 | |
| (d) | |
| (M1) | |
| (A1) | |
| A1 | |
The directed graph is shown.
- (a)Write down the adjacency matrix, , of , with rows representing the start of each edge.[2]
- (b)Write down the in-degree of .[1]
- (c)Explain why is strongly connected.[1]
- (d)Find the number of walks of length 5 from to .[2]
| (a) | |
| A2 | |
| (b) | |
| A1 | |
| (c) | |
| for example, visits every vertex following the arrows | R1 |
| (d) | |
| (M1) | |
| A1 | |
Four web pages, , , and , link to each other as shown. A user moves from page to page by choosing one of the links on the current page at random.
- (a)Write down the transition matrix , with columns representing the current page.[3]
- (b)Explain why the graph is strongly connected.[1]
- (c)Find the steady-state vector of .[3]
- (d)Hence rank the pages, and comment on page .[2]
| (a) | |
| A3 | |
| A2 for three correct columns, A1 for two. | |
| (b) | |
| for example, | R1 |
| (c) | |
| for large , or eigenvector for | (M1) |
| A2 | |
| (d) | |
| , , , | A1 |
| ranks lowest because only links to it, and splits its visitors between two pages | R1 |
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 2025 · TZ2 · Paper 1 | Q8 | Drawing a graph from its adjacency matrix; closed walks of length 4 | ★★☆ |
| Nov 2025 · Paper 1 | Q12 | Simple and connected; walks with fewer than four edges | ★★☆ |
| May 2023 · TZ2 · Paper 1 | Q8 | A directed road network and its adjacency matrix | ★★☆ |
| May 2024 · TZ1 · Paper 2 | Q8 | Walks in a directed graph, then a bird moving between posts (transition matrix) | ★★★ |
| Nov 2023 · Paper 2 | Q5 | Boat routes between islands as a graph | ★★★ |
