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.