James Ashton MathsFREE MATHEMATICS. OPEN TO EVERYONE.
HomeIB DiplomaA levelAbout

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.

AHL 3.14AHL 3.15HL onlyPaper 1Paper 2GDC allowed≈ 55 min to work through
Before you start: matrix multiplication and powers (Matrices) and steady states (Eigenvalues & eigenvectors).

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 Ak to count walks of length k, and sums of powers for walks of length up to kAHL 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.

The big idea

A graph keeps only what matters about a network: what is connected to what. Put the connections in a matrix A, and something remarkable happens: the entries of A2 count the two-step routes, the entries of A3 count the three-step routes, and so on.

2.1 The language of graphs

ABCDE

Graph G

TermMeaning
Adjacent verticesjoined by an edge (in G, B and D are adjacent; A and D are not)
Adjacent edgesedges that share a vertex
Degree of a vertexthe number of edge-ends at it (in G: A 2, B 3, C 3, D 3, E 1)
Simple graphno loops and no multiple edges between the same pair of vertices
Complete graph Knevery pair of the n vertices is joined by exactly one edge
Connectedthere is a route between every pair of vertices
Weighted grapheach edge carries a number: a distance, cost or time
Subgrapha graph made from some of the vertices and edges of another
Treea connected graph with no cycles; a tree with n vertices has n−1 edges

Each edge has two ends, so the sum of the degrees is twice the number of edges (in G: 2+3+3+3+1=12=2×6). 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 i, column j is the number of edges from i to j. 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).

Worked example 1Paper 1
Write down the adjacency matrix of graph G.
With rows and columns in the order A,B,C,D,E: (0110010110110100110100010)
Each row adds up to the degree of that vertex.

2.3 Counting walks

Walks and matrix powers Not in booklet: learn it

A walk is any sequence of edges, each starting where the last one ended; vertices and edges may repeat. The (i,j) entry of Ak is the number of walks of length k from i to j. The (i,j) entry of A+A2+…+Ak counts the walks of length at most k.

Worked example 2Paper 1
For graph G, find the number of walks of length 3 from A to D, and the number of walks from A to D of length at most 3.
A3=(2552254561554612662321130)
From the GDC.
Row A, column D: 2 walks (ABCD and ACBD).
(A+A2+A3)AD=0+2+2=4
Worked example 3 (faded)Paper 1
The adjacency matrix of a graph has A4 with 7 in row 2, column 5. Interpret this.
There are 7 walks of length 4 from vertex 2 to vertex 5  ← you work this out

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 T has, in column j, the probabilities of moving from vertex j to each vertex: 1out-degree of j for each link. Each column adds to 1. The steady state 𝐬 (with T𝐬=𝐬, 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.

PQRS

Four web pages and their links

Worked example 4Paper 2
For the four web pages shown, write down the transition matrix and find the steady state. Rank the pages.
T=(000.50.50.50000.5100.5000.50)
Columns are "from" P,Q,R,S; rows are "to".
Tn𝐬0 for large n, or the eigenvector for λ=1: 𝐬=(0.2860.1430.3810.190)
Any starting distribution settles to the same 𝐬.
Ranking: R, P, S, Q.
On your TI-Nspire Calculator tricks →

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 λ=1 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 A2 or A3 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 A+A2+A3.
  • 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.

FluencyP1
A simple graph has degrees 3, 3, 2, 2, 2. How many edges does it have?
122=6
FluencyP2
How many edges does the complete graph K6 have?
6×52=15
FluencyP3
Explain why no graph can have degrees 3, 3, 3, 2, 2.
The sum, 13, is odd, but it must be twice the number of edges.
FluencyP4
A tree has 9 vertices. How many edges does it have?
8
Problem solvingP5
For graph G, find the number of walks of length 2 from B back to B, and list them.
3: BAB, BCB, BDB.
Problem solvingP6
In the web example, page Q gains a link to S. Explain how the transition matrix changes.
Column Q becomes 0.5 to R and 0.5 to S; every other column is unchanged.

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[7 marks]

The graph H is shown.PQRST

  • (a)State, with a reason, whether H is simple.[1]
  • (b)Write down the adjacency matrix of H.[2]
  • (c)Find the sum of the degrees of the vertices of H.[1]
  • (d)Find the number of walks from P to T with fewer than five edges.[3]
(a)
not simple, because there are two edges joining S and TR1
(b)
(0110010110110100110200020)A2
A1 for one or two errors.
(c)
14A1
(d)
A+A2+A3+A4(M1)
0+0+4+4(A1)
8A1
Question 2Paper 1[6 marks]

The directed graph D is shown.ABCD

  • (a)Write down the adjacency matrix, M, of D, with rows representing the start of each edge.[2]
  • (b)Write down the in-degree of B.[1]
  • (c)Explain why D is strongly connected.[1]
  • (d)Find the number of walks of length 5 from A to C.[2]
(a)
(0101001010010100)A2
(b)
2A1
(c)
for example, A→D→B→C→A visits every vertex following the arrowsR1
(d)
(M5)AC(M1)
2A1
Question 3Paper 2[9 marks]

Four web pages, K, L, M and N, 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.KLMN

  • (a)Write down the transition matrix T, with columns representing the current page.[3]
  • (b)Explain why the graph is strongly connected.[1]
  • (c)Find the steady-state vector of T.[3]
  • (d)Hence rank the pages, and comment on page N.[2]
(a)
(0010.50.50000.50.500.500.500)A3
A2 for three correct columns, A1 for two.
(b)
for example, K→L→N→M→KR1
(c)
Tn𝐬0 for large n, or eigenvector for λ=1(M1)
(0.3810.1900.3330.0952)A2
(d)
K, M, L, NA1
N ranks lowest because only L links to it, and L splits its visitors between two pagesR1

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 2025 · TZ2 · Paper 1Q8Drawing a graph from its adjacency matrix; closed walks of length 4★★☆
Nov 2025 · Paper 1Q12Simple and connected; walks with fewer than four edges★★☆
May 2023 · TZ2 · Paper 1Q8A directed road network and its adjacency matrix★★☆
May 2024 · TZ1 · Paper 2Q8Walks in a directed graph, then a bird moving between posts (transition matrix)★★★
Nov 2023 · Paper 2Q5Boat routes between islands as a graph★★★
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