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.
Key ideas
Section titled “Key ideas”The adjacency matrix
Section titled “The adjacency matrix”Label the vertices in a fixed order. The adjacency matrix has one row and one column for each vertex:
- Undirected graph: the entry in row , column is the number of edges joining vertex and vertex . The matrix is symmetric, and in a simple graph the main diagonal is all s. Each row adds up to the degree of that vertex (in a graph without loops).
- Directed graph: the entry in row , column is the number of arcs from vertex to vertex . The matrix usually isn’t symmetric. Row sums are out-degrees and column sums are in-degrees.
Walks and powers of the adjacency matrix
Section titled “Walks and powers of the adjacency matrix”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 .
If is the adjacency matrix, the entry in row , column of is the number of walks of length from vertex to vertex .
Why it works for : the entry of is , and each product counts the ways to go from to and then from to . Adding over every middle vertex counts every walk of length . Higher powers repeat the argument.
To count walks of length up to (lengths ), add the powers:
Read the wording carefully: “fewer than edges” means lengths , and , so you’d use . Use your GDC’s matrix functions to find powers and sums.
Weighted adjacency tables
Section titled “Weighted adjacency tables”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:
| A | B | C | D | |
|---|---|---|---|---|
| A | – | 12 | – | 20 |
| B | 12 | – | 9 | 15 |
| C | – | 9 | – | 7 |
| D | 20 | 15 | 7 | – |
There’s no direct road from A to C. Tables like this are the input for the minimum spanning tree and travelling salesman algorithms.
Transition matrices for a graph
Section titled “Transition matrices for a graph”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 describes these moves, using the IB convention from Markov chains:
So column describes the moves out of vertex : if vertex has edges (or out-arcs) leaving it, each of the vertices they lead to gets probability in column . Every column adds up to .
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 with , found by raising 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.
Worked examples
Section titled “Worked examples”Example 1: Writing an adjacency matrix
Section titled “Example 1: Writing an adjacency matrix”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:
The matrix is symmetric, as it must be for an undirected graph. The row sums give the degrees: A has degree , B , C and D .
Example 2: Counting walks in G
Section titled “Example 2: Counting walks in G”Using the matrix from Example 1, find:
- (a) the number of walks of length from A to D,
- (b) the number of walks of length from B back to B,
- (c) the number of walks of length at most from A to B.
Solution. With a GDC:
(a) Row A, column D of : there are walks. (Check: A to B to C to D, and A to C to B to D.)
(b) Row B, column B of : there are walks (B to A to B, B to C to B, B to D to B). On the diagonal, 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 , and :
Example 3: A directed graph
Section titled “Example 3: A directed graph”For the directed graph H in the figure:
- (a) Write down its adjacency matrix .
- (b) Find the number of walks of length 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:
(b) By GDC,
The row A, column A entry is . 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 .
- (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 in rows B and C. Page B links only to C. Page C links to A and D. Page D links only to A:
Check: each column adds to .
(c) On a GDC, for a large (say ) has every column close to the same vector. Or solve with the entries adding to :
Check: has first entry , 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.
Common mistakes
Section titled “Common mistakes”Mixing up rows and columns for a directed graph. In the adjacency matrix, row , column counts arcs from to . For the transition matrix, the IB convention flips this: column holds the probabilities of moving from . Write a “from/to” label on your matrix before filling it in.
Reading the wrong power. Walks of length come from , not . For “at most ” or “fewer than ”, add the right powers, starting from .
Thinking walks can’t repeat. Walks may revisit vertices and reuse edges, so B to A to B counts as a walk of length . That’s why can give large numbers.
Columns of the transition matrix that don’t add to 1. Each column splits probability equally over the edges leaving that vertex. If vertex B has three neighbours, each gets 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.
Practice
Section titled “Practice”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:
Row sums: P has degree , Q , R , S .
2. (Warm-up) An undirected graph with vertices P, Q, R, S has adjacency matrix
How many edges does the graph have? Is it a simple graph?
Solution
The entries add to , and each edge is counted twice (once in each direction), so there are edges.
It is not simple: the 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”)
Find the out-degree and in-degree of each vertex.
Solution
Out-degrees are the row sums: X , Y , Z .
In-degrees are the column sums: X , Y , Z .
4. (Core) For graph G in the figure, use your GDC to find the number of walks of length from A to C.
Solution
Row A, column C: there are walks of length .
5. (Core) For graph G, find the number of walks from A to D that use fewer than edges.
Solution
“Fewer than ” means lengths , and . Add the row A, column D entries of , and :
(There’s no edge AD; the walks of length 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 roads. Total: minutes.
(b) minutes, which is 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 (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 ); B and C each have three neighbours (each ):
(b) The row D, column A entry of is . Check by listing: A to B to D has probability , and A to C to D also , total .
(c) Solving (or using a high power of on a GDC):
So of the time at A and at D, and at B and at C. (Notice these are proportional to the degrees .)
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:
Let . From : the first row gives , the second gives . With :
So . Check the third row: .
9. (Challenge) Let be the adjacency matrix of a simple undirected graph. Explain why the diagonal entry in row of equals the degree of vertex .
Solution
The diagonal entry is . Because the graph is undirected, , so each term is . In a simple graph each is or , so . The sum is , the row sum, which is the degree of vertex . In walk language: each walk of length from back to goes out along one edge and straight back, so there’s one for each edge at .