Skip to content
Family Table Math
Auto

Adjacency Matrices

A drawing of a graph is great for people, but a computer (or your GDC) needs numbers. An adjacency matrix stores a graph as a square grid of numbers, and then ordinary matrix multiplication does something surprising: powers of the matrix count the routes through the graph. The same idea, turned into probabilities, is how search engines rank web pages.

Label the vertices in a fixed order. The adjacency matrix AA has one row and one column for each vertex:

  • Undirected graph: the entry in row ii, column jj is the number of edges joining vertex ii and vertex jj. The matrix is symmetric, and in a simple graph the main diagonal is all 00s. Each row adds up to the degree of that vertex (in a graph without loops).
  • Directed graph: the entry in row ii, column jj is the number of arcs from vertex ii to vertex jj. The matrix usually isn’t symmetric. Row sums are out-degrees and column sums are in-degrees.
Left: undirected graph G with vertices A, B, C, D and edges AB, AC, BC, BD, CD. Right: directed graph H with arcs A to B, A to C, B to C, C to A, C to D and D to A. A B C D A B C D graph G (undirected) graph H (directed)
The undirected graph G and the directed graph H used in the examples.

A walk is a sequence of edges where each edge starts at the vertex where the previous one ended. Vertices and edges may be repeated. The length of a walk is the number of edges in it, so A to B to A to C has length 33.

If AA is the adjacency matrix, the entry in row ii, column jj of AkA^k is the number of walks of length kk from vertex ii to vertex jj.

Why it works for k=2k = 2: the (i,j)(i, j) entry of A2A^2 is ai1a1j+ai2a2j+…a_{i1}a_{1j} + a_{i2}a_{2j} + \dots, and each product aimamja_{im}a_{mj} counts the ways to go from ii to mm and then from mm to jj. Adding over every middle vertex mm counts every walk of length 22. Higher powers repeat the argument.

To count walks of length up to kk (lengths 1,2,…,k1, 2, \dots, k), add the powers:

S=A+A2+A3+⋯+AkS = A + A^2 + A^3 + \dots + A^k

Read the wording carefully: “fewer than 44 edges” means lengths 11, 22 and 33, so you’d use A+A2+A3A + A^2 + A^3. Use your GDC’s matrix functions to find powers and sums.

For a weighted graph, a weighted adjacency table shows the weight of the edge joining each pair of vertices (a distance, cost or time), with a dash where there’s no edge. For example, travel times in minutes between four towns:

ABCD
A–12–20
B12–915
C–9–7
D20157–

There’s no direct road from A to C. Tables like this are the input for the minimum spanning tree and travelling salesman algorithms.

Imagine a walker who, at each step, leaves the current vertex along one of its edges, chosen at random with equal probabilities. The transition matrix TT describes these moves, using the IB convention from Markov chains:

Tij=probability of moving from vertex j to vertex iT_{ij} = \text{probability of moving from vertex } j \text{ to vertex } i

So column jj describes the moves out of vertex jj: if vertex jj has dd edges (or out-arcs) leaving it, each of the vertices they lead to gets probability 1d\tfrac{1}{d} in column jj. Every column adds up to 11.

The graph should be strongly connected (every vertex can reach every other), so the walker can never get stuck in one part of the graph. Then the long-run proportion of time spent at each vertex is the steady state vector s⃗\vec{s} with Ts⃗=s⃗T\vec{s} = \vec{s}, found by raising TT to a high power on a GDC or by solving equations.

This is the core of the Google PageRank idea: vertices are web pages, arcs are links, and pages where the random surfer spends the most time are ranked highest.

Write down the adjacency matrix of graph G in the figure, and use it to find the degree of each vertex.

Solution. Use the order A, B, C, D. A is joined to B and C; B to A, C and D; C to A, B and D; D to B and C:

A=(0110101111010110)A = \begin{pmatrix} 0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 1 \\ 1 & 1 & 0 & 1 \\ 0 & 1 & 1 & 0 \end{pmatrix}

The matrix is symmetric, as it must be for an undirected graph. The row sums give the degrees: A has degree 22, B 33, C 33 and D 22.

Using the matrix from Example 1, find:

  • (a) the number of walks of length 33 from A to D,
  • (b) the number of walks of length 22 from B back to B,
  • (c) the number of walks of length at most 33 from A to B.

Solution. With a GDC:

A2=(2112132112312112),A3=(2552545555452552)A^2 = \begin{pmatrix} 2 & 1 & 1 & 2 \\ 1 & 3 & 2 & 1 \\ 1 & 2 & 3 & 1 \\ 2 & 1 & 1 & 2 \end{pmatrix}, \qquad A^3 = \begin{pmatrix} 2 & 5 & 5 & 2 \\ 5 & 4 & 5 & 5 \\ 5 & 5 & 4 & 5 \\ 2 & 5 & 5 & 2 \end{pmatrix}

(a) Row A, column D of A3A^3: there are 22 walks. (Check: A to B to C to D, and A to C to B to D.)

(b) Row B, column B of A2A^2: there are 33 walks (B to A to B, B to C to B, B to D to B). On the diagonal, A2A^2 always shows the degree of a vertex in a simple graph: out and back along each edge.

(c) Add the entries in row A, column B of AA, A2A^2 and A3A^3:

1+1+5=7 walks1 + 1 + 5 = 7 \text{ walks}

For the directed graph H in the figure:

  • (a) Write down its adjacency matrix MM.
  • (b) Find the number of walks of length 33 from A back to A, and list them.

Solution. (a) Rows are “from” and columns are “to”, in the order A, B, C, D. The arcs are A to B, A to C, B to C, C to A, C to D and D to A:

M=(0110001010011000)M = \begin{pmatrix} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 1 \\ 1 & 0 & 0 & 0 \end{pmatrix}

(b) By GDC,

M3=(2111111011211011)M^3 = \begin{pmatrix} 2 & 1 & 1 & 1 \\ 1 & 1 & 1 & 0 \\ 1 & 1 & 2 & 1 \\ 1 & 0 & 1 & 1 \end{pmatrix}

The row A, column A entry is 22. The two walks are A to B to C to A, and A to C to D to A.

Example 4: Transition matrix and a ranking

Section titled “Example 4: Transition matrix and a ranking”

Graph H represents four web pages and the links between them. A surfer follows one of the links on the current page at random, each equally likely.

  • (a) Explain why H is strongly connected.
  • (b) Write down the transition matrix TT.
  • (c) Find the steady state vector and rank the pages.

Solution. (a) A to B to C to D to A is a closed route through all four vertices, so every page can be reached from every other.

(b) Work column by column, in the order A, B, C, D. Page A links to B and C, so column A has 12\tfrac{1}{2} in rows B and C. Page B links only to C. Page C links to A and D. Page D links only to A:

T=(00121120001210000120)T = \begin{pmatrix} 0 & 0 & \tfrac{1}{2} & 1 \\ \tfrac{1}{2} & 0 & 0 & 0 \\ \tfrac{1}{2} & 1 & 0 & 0 \\ 0 & 0 & \tfrac{1}{2} & 0 \end{pmatrix}

Check: each column adds to 11.

(c) On a GDC, TnT^{n} for a large nn (say n=50n = 50) has every column close to the same vector. Or solve Ts⃗=s⃗T\vec{s} = \vec{s} with the entries adding to 11:

s⃗=(13161316)≈(0.3330.1670.3330.167)\vec{s} = \begin{pmatrix} \tfrac{1}{3} \\ \tfrac{1}{6} \\ \tfrac{1}{3} \\ \tfrac{1}{6} \end{pmatrix} \approx \begin{pmatrix} 0.333 \\ 0.167 \\ 0.333 \\ 0.167 \end{pmatrix}

Check: Ts⃗T\vec{s} has first entry 12⋅13+1⋅16=13\tfrac{1}{2} \cdot \tfrac{1}{3} + 1 \cdot \tfrac{1}{6} = \tfrac{1}{3}, as required. In the long run the surfer is on pages A and C a third of the time each, and on B and D a sixth of the time each. So A and C rank equal first, then B and D equal third.

Mixing up rows and columns for a directed graph. In the adjacency matrix, row ii, column jj counts arcs from ii to jj. For the transition matrix, the IB convention flips this: column jj holds the probabilities of moving from jj. Write a “from/to” label on your matrix before filling it in.

Reading the wrong power. Walks of length 33 come from A3A^3, not A2A^2. For “at most kk” or “fewer than kk”, add the right powers, starting from A1A^1.

Thinking walks can’t repeat. Walks may revisit vertices and reuse edges, so B to A to B counts as a walk of length 22. That’s why AkA^k can give large numbers.

Columns of the transition matrix that don’t add to 1. Each column splits probability 11 equally over the edges leaving that vertex. If vertex B has three neighbours, each gets 13\tfrac{1}{3} in column B.

Using a weighted table as an adjacency matrix. The weights are distances or costs, not counts of edges. Don’t raise a weighted table to a power to count walks.

1. (Warm-up) A graph has vertices P, Q, R, S and edges PQ, PS, QR, QS and RS. Write down its adjacency matrix and the degree of each vertex.

Solution

In the order P, Q, R, S:

(0101101101011110)\begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 1 \\ 0 & 1 & 0 & 1 \\ 1 & 1 & 1 & 0 \end{pmatrix}

Row sums: P has degree 22, Q 33, R 22, S 33.

2. (Warm-up) An undirected graph with vertices P, Q, R, S has adjacency matrix

(0101102002011010)\begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 2 & 0 \\ 0 & 2 & 0 & 1 \\ 1 & 0 & 1 & 0 \end{pmatrix}

How many edges does the graph have? Is it a simple graph?

Solution

The entries add to 2+3+3+2=102 + 3 + 3 + 2 = 10, and each edge is counted twice (once in each direction), so there are 102=5\tfrac{10}{2} = 5 edges.

It is not simple: the 22 in row Q, column R means there are two edges joining Q and R.

3. (Warm-up) A directed graph with vertices X, Y, Z has adjacency matrix (rows “from”, columns “to”)

(011001100)\begin{pmatrix} 0 & 1 & 1 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix}

Find the out-degree and in-degree of each vertex.

Solution

Out-degrees are the row sums: X 22, Y 11, Z 11.

In-degrees are the column sums: X 11, Y 11, Z 22.

4. (Core) For graph G in the figure, use your GDC to find the number of walks of length 44 from A to C.

SolutionA4=(109910915149914159109910)A^4 = \begin{pmatrix} 10 & 9 & 9 & 10 \\ 9 & 15 & 14 & 9 \\ 9 & 14 & 15 & 9 \\ 10 & 9 & 9 & 10 \end{pmatrix}

Row A, column C: there are 99 walks of length 44.

5. (Core) For graph G, find the number of walks from A to D that use fewer than 44 edges.

Solution

“Fewer than 44” means lengths 11, 22 and 33. Add the row A, column D entries of AA, A2A^2 and A3A^3:

0+2+2=4 walks0 + 2 + 2 = 4 \text{ walks}

(There’s no edge AD; the walks of length 22 are A to B to D and A to C to D.)

6. (Core) Use the weighted adjacency table of travel times (in minutes) in the Key ideas.

  • (a) How many roads are there, and what is their total travel time?
  • (b) Find the time for the route A to B to C to D, and compare it with the direct road from A to D.
  • (c) Explain why the table is symmetric.
Solution

(a) Count the entries above the diagonal: AB, AD, BC, BD and CD, so 55 roads. Total: 12+20+9+15+7=6312 + 20 + 9 + 15 + 7 = 63 minutes.

(b) 12+9+7=2812 + 9 + 7 = 28 minutes, which is 88 minutes longer than the direct road (20 minutes).

(c) The roads are two-way, so the time from X to Y equals the time from Y to X: the entry in row X, column Y equals the entry in row Y, column X.

7. (Core) A walker moves randomly on graph G, choosing each edge at the current vertex with equal probability.

  • (a) Write down the transition matrix TT (order A, B, C, D).
  • (b) The walker starts at A. Find the probability that it is at D after two steps.
  • (c) Find the long-run proportion of time the walker spends at each vertex.
Solution

(a) A has neighbours B and C, D has neighbours B and C (each 12\tfrac{1}{2}); B and C each have three neighbours (each 13\tfrac{1}{3}):

T=(01313012013121213012013130)T = \begin{pmatrix} 0 & \tfrac{1}{3} & \tfrac{1}{3} & 0 \\ \tfrac{1}{2} & 0 & \tfrac{1}{3} & \tfrac{1}{2} \\ \tfrac{1}{2} & \tfrac{1}{3} & 0 & \tfrac{1}{2} \\ 0 & \tfrac{1}{3} & \tfrac{1}{3} & 0 \end{pmatrix}

(b) The row D, column A entry of T2T^2 is 13\tfrac{1}{3}. Check by listing: A to B to D has probability 12×13=16\tfrac{1}{2} \times \tfrac{1}{3} = \tfrac{1}{6}, and A to C to D also 16\tfrac{1}{6}, total 13\tfrac{1}{3}.

(c) Solving Ts⃗=s⃗T\vec{s} = \vec{s} (or using a high power of TT on a GDC):

s⃗=(0.20.30.30.2)\vec{s} = \begin{pmatrix} 0.2 \\ 0.3 \\ 0.3 \\ 0.2 \end{pmatrix}

So 20%20\% of the time at A and at D, and 30%30\% at B and at C. (Notice these are proportional to the degrees 2,3,3,22, 3, 3, 2.)

8. (Challenge) Three web pages X, Y, Z have links X to Y, X to Z, Y to Z and Z to X. A surfer follows a random link on each page.

  • (a) Show that the graph is strongly connected.
  • (b) Write down the transition matrix and find the exact steady state vector by solving equations.
Solution

(a) X to Y to Z to X is a closed route through all three pages, so each page can reach every other.

(b) Order X, Y, Z. Column X splits between Y and Z; column Y goes to Z; column Z goes to X:

T=(00112001210)T = \begin{pmatrix} 0 & 0 & 1 \\ \tfrac{1}{2} & 0 & 0 \\ \tfrac{1}{2} & 1 & 0 \end{pmatrix}

Let s⃗=(x,y,z)\vec{s} = (x, y, z). From Ts⃗=s⃗T\vec{s} = \vec{s}: the first row gives z=xz = x, the second gives y=12xy = \tfrac{1}{2}x. With x+y+z=1x + y + z = 1:

x+12x+x=1⇒x=25x + \tfrac{1}{2}x + x = 1 \quad\Rightarrow\quad x = \tfrac{2}{5}

So s⃗=(25,15,25)\vec{s} = \left(\tfrac{2}{5}, \tfrac{1}{5}, \tfrac{2}{5}\right). Check the third row: 12⋅25+15=25=z\tfrac{1}{2} \cdot \tfrac{2}{5} + \tfrac{1}{5} = \tfrac{2}{5} = z.

9. (Challenge) Let AA be the adjacency matrix of a simple undirected graph. Explain why the diagonal entry in row ii of A2A^2 equals the degree of vertex ii.

Solution

The diagonal entry is (A2)ii=ai1a1i+ai2a2i+…(A^2)_{ii} = a_{i1}a_{1i} + a_{i2}a_{2i} + \dots. Because the graph is undirected, ami=aima_{mi} = a_{im}, so each term is aim2a_{im}^2. In a simple graph each aima_{im} is 00 or 11, so aim2=aima_{im}^2 = a_{im}. The sum is ai1+ai2+…a_{i1} + a_{i2} + \dots, the row sum, which is the degree of vertex ii. In walk language: each walk of length 22 from ii back to ii goes out along one edge and straight back, so there’s one for each edge at ii.

Try these independently after the practice questions. Show your setup and explain your reasoning. Use exact answers where possible, and check that your answers fit the question. Homework solutions are not provided on the published site.

Unless a question says otherwise, list the vertices in alphabetical order, and for directed graphs use rows for “from” and columns for “to”.

H1. (Foundations) An undirected graph has vertices A, B, C, D, E and edges AB, AC, BD, CD, CE and DE.

  • (a) Write down its adjacency matrix.
  • (b) Use the matrix to find the degree of each vertex, and check that the degrees add to twice the number of edges.
  • (c) Without a GDC, find the number of walks of length 22 from A to D, and list them.

H2. (Foundations) A directed graph with vertices P, Q, R, S has adjacency matrix

(0101001011000010)\begin{pmatrix} 0 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 1 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \end{pmatrix}
  • (a) List all the arcs, and find the out-degree and in-degree of each vertex.
  • (b) Explain, using one specific pair of entries, why this matrix is not symmetric.
  • (c) Is the graph strongly connected? Justify your answer with a closed route or a vertex that can’t be reached.

H3. (Core) An undirected graph has vertices J, K, L, M, N and edges JK, JL, KL, KM, KN, LN and MN.

  • (a) Write down the adjacency matrix AA, and use your GDC to find A3A^3.
  • (b) How many walks of length 33 are there from J to N? List them.
  • (c) How many walks of length 33 start and finish at M? Describe them.
  • (d) Explain why the number of walks of length 33 from J to N must equal the number from N to J.

H4. (Core) A small airline (hypothetical) flies these one-way routes between Kelowna (K), Calgary (C), Regina (R) and Thunder Bay (T): K to C, C to K, K to R, C to R, R to T and T to C.

  • (a) Write down the adjacency matrix FF (order K, C, R, T).
  • (b) Use your GDC to find the number of ways to fly from Kelowna to Thunder Bay using at most 44 flights. Show which powers of FF you add.
  • (c) How many ways are there to start in Thunder Bay and be back in Thunder Bay after exactly 44 flights? List them.

H5. (Core) The weighted adjacency table shows the lengths, in kilometres, of trails joining five trailheads A to E in a park (hypothetical data). A dash means there is no direct trail.

ABCDE
A–4.23.5––
B4.2–2.0–6.1
C3.52.0–2.8–
D––2.8–3.0
E–6.1–3.0–
  • (a) How many trails are there, and what is their total length?
  • (b) Find the length of the route A to B to E and of the route A to C to D to E. Which is shorter, and by how much?
  • (c) Write down the (unweighted) adjacency matrix of the trail network, and use it to find the number of walks of length 22 from A to E.
  • (d) Explain why squaring the weighted table would not give useful information about walks.

H6. (Core) (IB style) A museum (hypothetical) has four rooms P, Q, R, S. Doorways join P and Q, P and R, P and S, and Q and R. A visitor wanders at random: every few minutes they leave the current room through one of its doorways, each doorway equally likely.

  • (a) Write down the transition matrix TT (order P, Q, R, S), using the convention that column jj gives the probabilities of moving out of room jj.
  • (b) The visitor starts in room S. Find the probability that they are in room S again after two moves, and the probability that they are in room P after three moves.
  • (c) Find the long-run proportion of time the visitor spends in each room. Give exact values.
  • (d) The museum wants to put its most popular exhibit where visitors spend the most time. Which room should it choose? Comment on how the answer relates to the doorways.

H7. (Challenge) An undirected multigraph has vertices A, B, C. There are kk edges joining A and B, 22 edges joining B and C, and 11 edge joining A and C, where kk is a positive integer. There are no loops.

  • (a) Write down the adjacency matrix in terms of kk.
  • (b) Show that there are 2k2k walks of length 22 from A to C.
  • (c) Find an expression for the number of walks of length 33 from A to C, and hence find the value of kk for which there are exactly 3030 such walks.

H8. (Challenge) A directed graph has vertices W, X, Y, Z and arcs W to X, W to Z, X to Y, Y to W, Y to Z, Z to X, and a loop at X (an arc from X back to itself). A loop is recorded as 11 in row X, column X.

Sam is asked for the number of walks of length at most 33 from W to X. Sam writes the matrix with rows for “to” and columns for “from”, calculates A2+A3A^2 + A^3 on a GDC, and reads row W, column X to get 22.

  • (a) Identify Sam’s two mistakes.
  • (b) Write down the correct adjacency matrix and find the correct answer.
  • (c) List the walks you counted, and say which of them use the loop.

H9. (Challenge) A triangle in a graph is a set of three vertices that are all joined to each other.

  • (a) Let AA be the adjacency matrix of a simple undirected graph. Explain why every walk of length 33 that starts and ends at the same vertex goes around a triangle.
  • (b) Explain why each triangle gives exactly 66 such closed walks, and hence why the number of triangles is one sixth of the sum of the diagonal entries of A3A^3.
  • (c) Use this to find the number of triangles in the graph in H3, and name them.

H10. (Challenge) Four web pages A, B, C, D have these links: A to B, A to C, B to A, B to C, C to A, D to A and D to C. A surfer follows a random link on the current page, each equally likely.

  • (a) Show that the graph is not strongly connected.
  • (b) Write down the transition matrix TT (order A, B, C, D) and check that each column adds to 11.
  • (c) Find the exact steady state vector by solving equations, and check it with a high power of TT on your GDC.
  • (d) Explain why page D gets a long-run proportion of 00, and why this means the steady state can’t be used to rank D fairly against the other pages.