Skip to content
IB Diploma ProgrammeSL & HL

Adjacency matrices and walks

Mathematics: Applications and Interpretation SL & HL Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Adjacency matrices and walks notes as a printable PDF.

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

Read the notes

The full Adjacency matrices and walks notes as text: skim, search, and jump between subtopics.

~16 min read

1. Adjacency matrices

A graph has vertices joined by edges. An adjacency matrix records a graph in a square matrix, with the vertices listed in a fixed order.

The entry in row i and column j is the number of edges from vertex i to vertex j. In a directed graph, row i lists the edges leaving vertex i and column j lists the edges entering vertex j.

In an undirected graph an edge works both ways, so the matrix is symmetric about the leading diagonal. The same vertex order is used for the rows and the columns.

boldsymbolA = (a_ij), a_ij = number of edges from vertex i to vertex j

Worked example 13 marks

An undirected graph has four vertices P, Q, R and S. The edges are PQ, PR, QR and RS. Each edge is used once.

(a) Write down the adjacency matrix, with the vertices in the order P, Q, R, S.

(b) Explain why the matrix is symmetric.

  1. 1

    (a) A1: row P is (0, 1, 1, 0) and row Q is (1, 0, 1, 0) [1]

  2. 2

    A1: row R is (1, 1, 0, 1) and row S is (0, 0, 1, 0), so boldsymbolA = beginpmatrix0&1&1&0 1&0&1&0 1&1&0&1 0&0&1&0endpmatrix [1]

  3. 3

    (b) R1: the graph is undirected, so an edge from i to j is also an edge from j to i and a_ij = a_ji [1]

2. Walks and powers of the adjacency matrix

A walk follows edges one after another from a start vertex to an end vertex. Its length is the number of edges it uses. A walk may use the same edge or vertex more than once.

Given an adjacency matrix boldsymbolA, the (i, j)th entry of boldsymbolA^(k) gives the number of k length walks from i to j. Read the row for the start vertex and the column for the end vertex.

The reason is in the product. Each term a_ima_mj counts the walks of length 2 that go through vertex m, and the sum adds them over every m.

(boldsymbolA^(k))_ij = number of walks of length k from i to j

(boldsymbolA²)_ij = sum_m a_im a_mj

Worked example 13 marks

An undirected graph has vertices P, Q, R and S in that order, and adjacency matrix boldsymbolA = beginpmatrix0&1&1&0 1&0&1&0 1&1&0&1 0&0&1&0endpmatrix. Given that boldsymbolA² = beginpmatrix2&1&1&1 1&2&1&1 1&1&3&0 1&1&0&1endpmatrix:

(a) Calculate the number of walks of length 3 from P to R.

(b) Write down the walk of length 2 from S to S.

  1. 1

    (a) M1: the number is the (1, 3) entry of boldsymbolA³ = boldsymbolA²boldsymbolA, which is row P of boldsymbolA² times column R of boldsymbolA: (2, 1, 1, 1) · (1, 1, 0, 1) [1]

  2. 2

    A1: 2 + 1 + 0 + 1 = 4 walks [1]

  3. 3

    (b) A1: the (4, 4) entry of boldsymbolA² is 1, and the walk is S, R, S [1]

3. Walks of at most k and fewer than k edges

To count walks of at most k edges, add the matrices for each length. The (i,j) entry of boldsymbolA + boldsymbolA² + ⋯ + boldsymbolA^(k) counts the walks of length 1, 2, up to k from i to j.

Walks of fewer than k edges stop one power earlier, at boldsymbolA^(k-1).

Read the wording. "At most 3" includes length 3. "Fewer than 3" does not. Add only the entry you need when only one pair of vertices is asked for.

at most k: boldsymbolA + boldsymbolA² + ⋯ + boldsymbolA^(k)

fewer than k: boldsymbolA + boldsymbolA² + ⋯ + boldsymbolA^(k-1)

Worked example 14 marks

For the graph with vertices P, Q, R, S in that order, boldsymbolA = beginpmatrix0&1&1&0 1&0&1&0 1&1&0&1 0&0&1&0endpmatrix, boldsymbolA² = beginpmatrix2&1&1&1 1&2&1&1 1&1&3&0 1&1&0&1endpmatrix and boldsymbolA³ = beginpmatrix2&3&4&1 3&2&4&1 4&4&2&3 1&1&3&0endpmatrix.

(a) Determine the number of walks of length at most 3 from P to S.

(b) Determine the number of walks of length fewer than 3 from Q to Q.

  1. 1

    (a) M1: add the (1, 4) entries of boldsymbolA, boldsymbolA² and boldsymbolA³: 0 + 1 + 1 [1]

  2. 2

    A1: 2 walks [1]

  3. 3

    (b) M1: fewer than 3 means lengths 1 and 2, so add the (2, 2) entries of boldsymbolA and boldsymbolA²: 0 + 2 [1]

  4. 4

    A1: 2 walks, Q, P, Q and Q, R, Q [1]

4. Weighted adjacency tables

A weighted adjacency table has the layout of an adjacency matrix. The entry for vertices i and j is the weight of the edge between them. Weights could be costs, distances or lengths of time.

Where there is no edge, leave a dash. An undirected graph gives a table that is symmetric about the leading diagonal.

The weight of a walk is the sum of the weights of the edges it uses. Powers of a weighted table do not count walks. For that, replace every weight by 1 to get the adjacency matrix and then take powers.

weight of a walk = sum of the weights of its edges

Worked example 14 marks

Four depots A, B, C and D are joined by roads. The distances in kilometres are AB = 4, AC = 7, BC = 2 and CD = 5. There are no other roads.

(a) Construct the weighted adjacency table.

(b) Find the total distance of the walk A, B, C, D.

  1. 1

    (a) A1: a 4 × 4 table with rows and columns in the order A, B, C, D, and a dash on the leading diagonal [1]

  2. 2

    A1: the entries 4, 7, 2, 5 in the correct places, each also in its mirror position: beginarrayc|cccc & A & B & C & D hline A & - & 4 & 7 & - B & 4 & - & 2 & - C & 7 & 2 & - & 5 D & - & - & 5 & - endarray [1]

  3. 3

    (b) M1: add the weights of AB, BC and CD: 4 + 2 + 5 [1]

  4. 4

    A1: 11 km [1]

5. The transition matrix of a graph

A transition matrix boldsymbolT turns a graph into probabilities. A walker moves along one of the edges leaving its vertex, and each edge is equally likely unless the question says otherwise.

Column j is for vertex j. The entry T_ij is the probability of moving from vertex j to vertex i, so every column adds to 1. If d edges leave vertex j, each non-zero entry in column j is 1/d.

A graph is strongly connected if there is a walk from every vertex to every other vertex. The walker never gets stuck. The graph may be undirected or directed.

T_ij = probability of moving from vertex j to vertex i

T_ij = 1/d_j if there is an edge from j to i, otherwise 0

Worked example 13 marks

A walker moves around the undirected graph with edges PQ, PR, QR and RS. At each step it moves along one of the edges at its vertex, each equally likely. Write down the transition matrix boldsymbolT, with the vertices in the order P, Q, R, S.

  1. 1

    M1: the number of edges at each vertex is P: 2, Q: 2, R: 3, S: 1 [1]

  2. 2

    A1: column P is (0, 1/2, 1/2, 0) and column Q is (1/2, 0, 1/2, 0) [1]

  3. 3

    A1: column R is (1/3, 1/3, 0, 1/3) and column S is (0, 0, 1, 0), so boldsymbolT = beginpmatrix0&1/2&1/3&0 1/2&0&1/3&0 1/2&1/2&0&1 0&0&1/3&0endpmatrix [1]

6. Google PageRank

The Google PageRank algorithm is an example of a transition matrix for a simple graph. Web pages are the vertices and links are directed edges. A user clicks a link at random from the page they are on.

The long-term probabilities boldsymbols of being on each page are found as in AHL 4.19. They satisfy boldsymbolTboldsymbols = boldsymbols, and the entries of boldsymbols add to 1. A larger entry ranks the page higher.

The equation boldsymbolTboldsymbols = boldsymbols alone has many solutions. The condition that the entries add to 1 picks the one answer.

boldsymbolTboldsymbols = boldsymbols

s₁ + s₂ + ⋯ + sn = 1

Worked example 16 marks

A website has three pages, A, B and C. Page A links to pages B and C. Page B links to page C. Page C links to page A. A user on a page clicks one of its links at random, each equally likely.

(a) Write down the transition matrix boldsymbolT, with the pages in the order A, B, C.

(b) Find the long-term probability of being on each page.

(c) State which pages rank highest.

  1. 1

    (a) A1: column A is (0, 1/2, 1/2) and column B is (0, 0, 1) [1]

  2. 2

    A1: column C is (1, 0, 0), so boldsymbolT = beginpmatrix0&0&1 1/2&0&0 1/2&1&0endpmatrix [1]

  3. 3

    (b) M1: boldsymbolTboldsymbols = boldsymbols gives s_A = s_C, s_B = 1/2 s_A and s_C = 1/2 s_A + s_B [1]

  4. 4

    M1: s_A + s_B + s_C = 1, so s_A + 1/2 s_A + s_A = 1 and s_A = 2/5 [1]

  5. 5

    A1: boldsymbols = (2/5, 1/5, 2/5) [1]

  6. 6

    (c) A1: pages A and C rank equal highest, with probability 2/5 each. Page B ranks lowest [1]

Recap

  • An adjacency matrix has entry a_ij = the number of edges from vertex i to vertex j. An undirected graph gives a symmetric matrix.
  • The (i, j) entry of boldsymbolA^(k) is the number of k length walks from i to j.
  • At most k edges: boldsymbolA + ⋯ + boldsymbolA^(k). Fewer than k edges: stop at boldsymbolA^(k-1).
  • A weighted adjacency table holds costs, distances or times. The weight of a walk is the sum of its edge weights. Replace weights by 1 before taking powers.
  • Transition matrix: T_ij is the probability of moving from j to i. Each column adds to 1.
  • PageRank: solve boldsymbolTboldsymbols = boldsymbols with the entries of boldsymbols adding to 1. A larger entry ranks higher.

End-of-chapter exercise

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

  1. A directed graph has adjacency matrix boldsymbolA. The entry in row 3 and column 1 is 2. State what this entry tells you about the graph.1 mark
    Answer
    1. There are 2 edges from vertex 3 to vertex 1.

    Trap: Reading it as column to row, which reverses the direction of the edges.

  2. A directed graph has adjacency matrix boldsymbolA = beginpmatrix0&1&1 0&0&1 1&0&0endpmatrix. How many walks of length 2 are there from vertex 1 to vertex 3? A. 0 B. 1 C. 2 D. 51 mark
    Answer
    1. The answer is B. boldsymbolA² = beginpmatrix1&0&1 1&0&0 0&1&1endpmatrix, and its (1, 3) entry is 1: the walk is 1, 2, 3.
    2. A is the (3, 1) entry of boldsymbolA², which reverses start and end. C is the number of edges leaving vertex 1, which are walks of length 1. D is the sum of all the entries of boldsymbolA².

    Trap: Reading the entry for the wrong direction, or the matrix boldsymbolA in place of boldsymbolA².

  3. boldsymbolA is the adjacency matrix of a graph. The number of walks of length fewer than 4 from one vertex to another is found from the entries of which matrix? A. boldsymbolA³ B. boldsymbolA + boldsymbolA² + boldsymbolA³ C. boldsymbolA + boldsymbolA² + boldsymbolA³ + boldsymbolA⁴ D. boldsymbolA⁴1 mark
    Answer
    1. The answer is B. Lengths 1, 2 and 3 are fewer than 4.
    2. A counts only walks of length exactly 3. C includes length 4. D counts only walks of length exactly 4.

    Trap: Treating "fewer than 4" as "at most 4".

  4. Do not use a calculator. An undirected graph has three vertices, each joined to both of the others by one edge. Its adjacency matrix is boldsymbolA = beginpmatrix0&1&1 1&0&1 1&1&0endpmatrix. (a) Calculate boldsymbolA². (b) Determine the number of walks of length 2 from vertex 1 to vertex 1.3 marks
    Answer
    1. (a)
    2. boldsymbolA² = beginpmatrix2&1&1 1&2&1 1&1&2endpmatrix [2].
    3. (b)
    4. The (1, 1) entry is 2: the walks are 1, 2, 1 and 1, 3, 1 [1].

    Trap: Writing 0 on the diagonal because no edge joins a vertex to itself. A walk can leave a vertex and come back.

  5. A one-way system joins three squares, numbered 1, 2 and 3. Roads go from square 1 to square 2, from square 2 to square 3, from square 3 to square 1 and from square 3 to square 2. (a) Write down the adjacency matrix boldsymbolA. (b) Use a graphic display calculator to find the number of walks of length 4 from square 3 to square 2. (c) Determine the number of walks of length at most 4 from square 3 to square 2.5 marks
    Answer
    1. (a)
    2. boldsymbolA = beginpmatrix0&1&0 0&0&1 1&1&0endpmatrix [1].
    3. (b)
    4. The (3, 2) entry of boldsymbolA⁴ is 2 [2].
    5. (c)
    6. The (3, 2) entries of boldsymbolA, boldsymbolA², boldsymbolA³ and boldsymbolA⁴ are 1, 1, 1 and 2 [1].
    7. Total 1 + 1 + 1 + 2 = 5 walks [1].

    Trap: Reading row 2 and column 3 instead. That counts the walks from square 2 to square 3, which gives 1 in (b) and 3 in (c).

  6. The table gives the time in minutes to travel directly between three stations X, Y and Z. A dash means there is no direct train. (a) Find the total time for the walk X, Y, Z, Y. (b) A student squares the table to find the number of walks of length 2. Explain why this does not work. | | X | Y | Z | | --- | --- | --- | --- | | X | – | 12 | – | | Y | 12 | – | 9 | | Z | – | 9 | – |4 marks
    Answer
    1. (a)
    2. 12 + 9 + 9 = 30 minutes [2].
    3. (b)
    4. The entries are times, not numbers of edges, so the product adds up times and does not count walks [1].
    5. Replace each time by 1 to get the adjacency matrix, then square that [1].

    Trap: Using a weighted table in the place of an adjacency matrix.

  7. Do not use a calculator. A website has three pages, 1, 2 and 3. Page 1 links to pages 2 and 3. Page 2 links to page 3. Page 3 links to pages 1 and 2. A user on a page clicks one of its links at random, each equally likely. Write down the transition matrix boldsymbolT.3 marks
    Answer
    1. The number of links leaving each page is 2, 1 and 2 [1].
    2. Column 1 is (0, 1/2, 1/2), column 2 is (0, 0, 1) and column 3 is (1/2, 1/2, 0) [1].
    3. boldsymbolT = beginpmatrix0&0&1/2 1/2&0&1/2 1/2&1&0endpmatrix [1].

    Trap: Writing the probabilities along the rows. Each column must add to 1, so a matrix whose rows add to 1 is the transpose of the one wanted.

  8. Four towns, 1, 2, 3 and 4, are joined by roads 1–2, 1–3, 1–4 and 3–4. A tourist in a town chooses one of the roads from it at random, each equally likely. (a) Write down the transition matrix boldsymbolT. (b) State the probability of moving from town 3 to town 1.4 marks
    Answer
    1. (a)
    2. The numbers of roads at each town are 3, 1, 2 and 2 [1].
    3. boldsymbolT = beginpmatrix0&1&1/2&1/2 1/3&0&0&0 1/3&0&0&1/2 1/3&0&1/2&0endpmatrix [2].
    4. (b)
    5. 1/2 [1].

    Trap: Dividing every column by the number of towns instead of the number of roads at that town.

  9. A website has three pages. Page 1 links to pages 2 and 3. Page 2 links to page 1. Page 3 links to pages 1 and 2. A user on a page clicks one of its links at random, each equally likely. (a) Write down the transition matrix boldsymbolT. (b) Find the long-term probability of being on each page. (c) Rank the pages, highest first.6 marks
    Answer
    1. (a)
    2. boldsymbolT = beginpmatrix0&1&1/2 1/2&0&1/2 1/2&0&0endpmatrix [2].
    3. (b)
    4. boldsymbolTboldsymbols = boldsymbols gives s₃ = 1/2 s₁ and s₂ = 1/2 s₁ + 1/2 s₃ = 3/4 s₁ [1].
    5. s₁ + s₂ + s₃ = 1, so 9/4 s₁ = 1 and s₁ = 4/9 [1].
    6. boldsymbols = (4/9, 1/3, 2/9) [1].
    7. (c)
    8. Page 1, then page 2, then page 3 [1].

    Trap: Ranking by the number of links into a page. Pages 1 and 2 each have two links in, which gives a tie that the probabilities settle.

  10. A small airline flies one-way routes between four airports, numbered 1 to 4. It flies from 1 to 2, from 1 to 3, from 2 to 3, from 2 to 4, from 3 to 4 and from 4 to 1. (a) Write down the adjacency matrix boldsymbolA. (b) Determine the number of round trips that start and end at airport 1 and use fewer than 4 flights. (c) A pilot at an airport takes one of the routes from it at random, each equally likely. Write down the transition matrix boldsymbolT. (d) Use a graphic display calculator to find the long-term probability of the pilot being at each airport. Hence rank the airports, highest first.9 marks
    Answer
    1. (a)
    2. boldsymbolA = beginpmatrix0&1&1&0 0&0&1&1 0&0&0&1 1&0&0&0endpmatrix [1].
    3. (b)
    4. Fewer than 4 flights means walks of length 1, 2 and 3 [1].
    5. The (1, 1) entries of boldsymbolA, boldsymbolA² and boldsymbolA³ are 0, 0 and 2 [1].
    6. 0 + 0 + 2 = 2 round trips: 1, 2, 4, 1 and 1, 3, 4, 1 [1].
    7. (c)
    8. The numbers of routes leaving airports 1, 2, 3 and 4 are 2, 2, 1 and 1 [1].
    9. boldsymbolT = beginpmatrix0&0&0&1 1/2&0&0&0 1/2&1/2&0&0 0&1/2&1&0endpmatrix [1].
    10. (d)
    11. Solve boldsymbolTboldsymbols = boldsymbols with s₁ + s₂ + s₃ + s₄ = 1 [1].
    12. boldsymbols = (4/13, 2/13, 3/13, 4/13) ≈ (0.308, 0.154, 0.231, 0.308) [1].
    13. Airports 1 and 4 equal highest, then airport 3, then airport 2 [1].

    Trap: Adding boldsymbolA⁴ for "fewer than 4", which gives 3 round trips, or ranking by the number of routes in, which puts airports 3 and 4 first.

Say this, not that

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

Loses the markGets the mark
The (i, j) entry of boldsymbolA³ is the (i, j) entry of boldsymbolA cubed.The (i, j) entry of boldsymbolA³ is the number of 3 length walks from i to j.
Walks of fewer than 3 edges: boldsymbolA + boldsymbolA² + boldsymbolA³.Fewer than 3 stops at boldsymbolA². At most 3 is boldsymbolA + boldsymbolA² + boldsymbolA³.
Square the weighted table to count the walks of length 2.Replace each weight by 1 to get the adjacency matrix, then square it.
Each row of the transition matrix adds to 1.Each column adds to 1, and T_ij is the probability of moving from j to i.

Frequently asked questions

How do I write the adjacency matrix of a graph?
An adjacency matrix has entry a_ij equal to the number of edges from vertex i to vertex j, with the vertices listed in the same fixed order for rows and columns. For an undirected graph with edges PQ, PR, QR and RS, row P is (0, 1, 1, 0). Each edge works both ways, so the matrix is symmetric about the leading diagonal.
How do I count the walks of a given length between two vertices?
The (i, j) entry of boldsymbolA^(k) is the number of walks of length k from i to j. Read the row for the start vertex and the column for the end vertex. For the same graph, the walks of length 3 from P to R are row P of boldsymbolA² times column R of boldsymbolA, which is (2, 1, 1, 1) · (1, 1, 0, 1) = 4 walks.
What is the difference between walks of at most k edges and walks of fewer than k edges?
For walks of at most k edges, add the matrices boldsymbolA + boldsymbolA² + ⋯ + boldsymbolA^(k), because "at most 3" includes length 3. For walks of fewer than k edges, stop one power earlier, at boldsymbolA^(k-1). Read the wording carefully. Walks of fewer than 3 edges from Q to Q total 0 + 2 = 2 walks.
How do I find the total weight of a walk in a weighted graph?
The weight of a walk is the sum of the weights of the edges it uses. Set out the graph as a weighted adjacency table, with a dash where there is no edge and each weight mirrored across the leading diagonal for an undirected graph. For depots A, B, C and D with AB = 4, BC = 2 and CD = 5, the walk A, B, C, D has total distance 4 + 2 + 5 = 11 km.
How do I write the transition matrix for a walker moving around a graph?
The transition matrix boldsymbolT has entry T_ij equal to the probability of moving from vertex j to vertex i, so every column adds to 1. If d edges leave vertex j, each non-zero entry in column j is 1/d. For the walker on PQ, PR, QR and RS, column R is (1/3, 1/3, 0, 1/3), because R has three edges.
What mistake do students make when they use powers of an adjacency matrix?
The (i, j) entry of boldsymbolA³ is not the (i, j) entry of boldsymbolA cubed. It is the number of 3 length walks from i to j, found by matrix multiplication. Likewise, a weighted table must have each weight replaced by 1 to give the adjacency matrix before any powers are taken, because powers of a weighted table do not count walks.

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