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 .
Homework
Section titled “Homework”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”.
Foundations
Section titled “Foundations”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 from A to D, and list them.
H2. (Foundations) A directed graph with vertices P, Q, R, S has adjacency matrix
- (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 , and use your GDC to find .
- (b) How many walks of length are there from J to N? List them.
- (c) How many walks of length start and finish at M? Describe them.
- (d) Explain why the number of walks of length 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 (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 flights. Show which powers of you add.
- (c) How many ways are there to start in Thunder Bay and be back in Thunder Bay after exactly 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.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 4.2 | 3.5 | – | – |
| B | 4.2 | – | 2.0 | – | 6.1 |
| C | 3.5 | 2.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 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 (order P, Q, R, S), using the convention that column gives the probabilities of moving out of room .
- (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.
Challenge
Section titled “Challenge”H7. (Challenge) An undirected multigraph has vertices A, B, C. There are edges joining A and B, edges joining B and C, and edge joining A and C, where is a positive integer. There are no loops.
- (a) Write down the adjacency matrix in terms of .
- (b) Show that there are walks of length from A to C.
- (c) Find an expression for the number of walks of length from A to C, and hence find the value of for which there are exactly 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 in row X, column X.
Sam is asked for the number of walks of length at most from W to X. Sam writes the matrix with rows for “to” and columns for “from”, calculates on a GDC, and reads row W, column X to get .
- (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 be the adjacency matrix of a simple undirected graph. Explain why every walk of length that starts and ends at the same vertex goes around a triangle.
- (b) Explain why each triangle gives exactly such closed walks, and hence why the number of triangles is one sixth of the sum of the diagonal entries of .
- (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 (order A, B, C, D) and check that each column adds to .
- (c) Find the exact steady state vector by solving equations, and check it with a high power of on your GDC.
- (d) Explain why page D gets a long-run proportion of , and why this means the steady state can’t be used to rank D fairly against the other pages.