Matrices for graphs, digraphs and networks
See how adjacency matrices turn a graph into numbers in Year 12 VCE General Mathematics. A drawing of roads, ferry routes or one-way streets is easy to read by eye, but a computer needs a table. An adjacency matrix gives every vertex a row and a column, and each entry counts the links between a pair, much as a planner might store the daily coach services between Geelong and the towns of the south-west.
You will practise writing matrices for graphs with loops and multiple edges, and for digraphs where rows mean from and columns mean to. You will read degrees, in-degrees and out-degrees, isolated vertices and edge totals straight from a matrix, and draw the graph a matrix describes, ready to explore walks, trails and paths next.
Theory
An adjacency matrix records a graph as a grid of numbers, one row and one column per vertex. This Year 12 General Mathematics guide for students in Victoria shows how to write the matrix of a graph or a directed graph, deal with loops and multiple edges, read degrees and edge counts from it, and draw the graph a matrix describes.
An adjacency matrix stores a graph as a square grid of numbers. List the vertices in an order, and use the same order for the rows and the columns. The entry in row \(u\) and column \(v\) is the number of edges joining \(u\) and \(v\). A \(0\) means there is no edge, and an entry of \(2\) or more shows multiple edges.
For an undirected graph each edge is counted from both ends, so the matrix is symmetric: it is a mirror image across the leading diagonal (top left to bottom right). The diagonal holds the loops. Here a loop counts \(1\) in its diagonal entry. (Some books count a loop as \(2\), so always check which rule a question uses.) A simple graph has only \(0\)s on the diagonal.
For a directed graph (digraph), the rows are the vertices an arc goes from and the columns are the vertices it goes to. An arc \(P \to Q\) puts a \(1\) in row \(P\), column \(Q\), but not in row \(Q\), column \(P\), so a digraph’s matrix is usually not symmetric.
A matrix can also describe a network in context: an entry might count the bus services or roads from one town to another. A row of \(0\)s belongs to an isolated vertex.
| \(A\) | \(B\) | \(C\) | \(D\) | |
|---|---|---|---|---|
| \(A\) | 1 | 1 | 0 | 1 |
| \(B\) | 1 | 0 | 2 | 1 |
| \(C\) | 0 | 2 | 0 | 1 |
| \(D\) | 1 | 1 | 1 | 0 |
| from \ to | \(P\) | \(Q\) | \(R\) | \(S\) |
|---|---|---|---|---|
| \(P\) | 0 | 1 | 0 | 1 |
| \(Q\) | 0 | 0 | 1 | 0 |
| \(R\) | 1 | 0 | 0 | 1 |
| \(S\) | 0 | 1 | 0 | 0 |
For an undirected graph, the entry in row \(u\), column \(v\) is:
With a loop counted \(1\) on the diagonal, the degree of a vertex is its row sum plus its diagonal entry:
Each edge appears once on or above the diagonal, so:
For a digraph (rows = from, columns = to):
How to build an adjacency matrix (and go back again)
- Fix the order. Write the vertices across the top and down the side in the same order (usually alphabetical).
- Fill one row at a time. For the row vertex, count the edges to each column vertex. Put the number of loops on the diagonal (a loop counts \(1\)). For a digraph, count only arcs leaving the row vertex.
- Check. An undirected matrix must be symmetric. Total the entries on and above the diagonal and compare with the number of edges.
- Matrix to graph. Draw one vertex per row. Work through the entries on and above the diagonal (all entries for a digraph): draw that many edges, a loop for each diagonal count, and arrows from row to column for a digraph. Then rebuild the matrix from your drawing to check it.
Go along each row and put a \(1\) under every vertex the row vertex is joined to.
| \(\text{row } A\) | \(:\) | \(0, 1, 0, 0, 1 \text{ joined to } B,\ E\) |
| \(\text{row } B\) | \(:\) | \(1, 0, 1, 1, 1 \text{ joined to } A,\ C,\ D,\ E\) |
| \(\text{row } C\) | \(:\) | \(0, 1, 0, 1, 0 \text{ joined to } B,\ D\) |
| \(\text{row } D\) | \(:\) | \(0, 1, 1, 0, 1 \text{ joined to } B,\ C,\ E\) |
| \(\text{row } E\) | \(:\) | \(1, 1, 0, 1, 0 \text{ joined to } A,\ B,\ D\) |
The adjacency matrix is \(\begin{bmatrix}0 & 1 & 0 & 0 & 1\\ 1 & 0 & 1 & 1 & 1\\ 0 & 1 & 0 & 1 & 0\\ 0 & 1 & 1 & 0 & 1\\ 1 & 1 & 0 & 1 & 0\end{bmatrix}\).
Check: there are no loops, so the diagonal is all \(0\), and the matrix is symmetric. The entries above the diagonal add to \(7\), the number of edges.
Two tracks join \(K\) and \(L\), so that entry is \(2\). The circuit at \(M\) is a loop.
(i) \(\begin{bmatrix}0 & 2 & 1 & 1\\ 2 & 0 & 1 & 0\\ 1 & 1 & 1 & 1\\ 1 & 0 & 1 & 0\end{bmatrix}\)
| \(\deg(M)\) | \(=\) | \(\text{row sum} + \text{diagonal entry}\) |
| \(\) | \(=\) | \((1 + 1 + 1 + 1) + 1\) |
| \(\) | \(=\) | \(5\) |
| \(\text{tracks}\) | \(=\) | \((0 + 2 + 1 + 1) + (0 + 1 + 0) + (1 + 1) + 0\) |
| \(\) | \(=\) | \(7\) |
(ii) \(\deg(M) = 5\): the loop adds \(2\) and the tracks to \(K\), \(L\) and \(N\) add \(3\). Adding the entries on and above the diagonal gives \(7\) tracks.
Each arrow puts a \(1\) in the row it leaves and the column it enters.
(i) \(\begin{bmatrix}0 & 1 & 1 & 0\\ 0 & 0 & 1 & 1\\ 0 & 0 & 0 & 1\\ 1 & 1 & 0 & 0\end{bmatrix}\)
| \(\text{out of } U\) | \(=\) | \(\text{row } U \text{ sum}\) |
| \(\) | \(=\) | \(0 + 0 + 1 + 1\) |
| \(\) | \(=\) | \(2\) |
| \(\text{into } V\) | \(=\) | \(\text{column } V \text{ sum}\) |
| \(\) | \(=\) | \(1 + 1 + 0 + 0\) |
| \(\) | \(=\) | \(2\) |
| \(\text{tracks}\) | \(=\) | \(2 + 2 + 1 + 2\) |
| \(\) | \(=\) | \(7\) |
(ii) \(2\) tracks leave \(U\) and \(2\) arrive at \(V\). (iii) There are \(7\) tracks. The matrix is not symmetric: \(T \to U\) is a track, but \(U \to T\) is not.
Use only the entries on and above the diagonal, so that each edge is drawn once.
| \(\text{row } A\) | \(:\) | \(AB,\ AC\) |
| \(\text{row } B\) | \(:\) | \(\text{loop at } B,\ BD,\ BD\) |
| \(\text{row } C\) | \(:\) | \(CD\) |
| \(\text{edges}\) | \(=\) | \(2 + 3 + 1\) |
| \(\) | \(=\) | \(6\) |
| \(\deg(B)\) | \(=\) | \((1 + 1 + 0 + 2) + 1\) |
| \(\) | \(=\) | \(5\) |
The graph has \(6\) edges and \(\deg(B) = 5\). No row is all \(0\)s and every vertex can be reached from \(A\), so the graph is connected. Check: rebuilding the matrix from the drawing gives the same matrix.
Common pitfalls
Frequently asked questions
What is an adjacency matrix?
It is a square table of numbers that describes a graph. The rows and columns are the vertices in the same order, and each entry is the number of edges joining the row vertex to the column vertex.
How do you show a loop in an adjacency matrix?
A loop goes on the leading diagonal, in the row and column of its vertex. In this course a loop counts 1 in that entry. Some textbooks count it as 2, so read the question carefully.
How do you find the degree of a vertex from an adjacency matrix?
Add the entries in its row, then add the diagonal entry once more, because each loop adds 2 to the degree but only 1 to the matrix. With no loops, the degree is just the row sum.
Why is the adjacency matrix of an undirected graph symmetric?
An edge joining A and B can be read from either end, so it is counted in row A, column B and again in row B, column A. That makes the matrix a mirror image across the leading diagonal.
How do you write the adjacency matrix of a directed graph?
Use the rows for the vertex an arc starts from and the columns for the vertex it goes to. Each arc adds 1 to one entry only, so the matrix is usually not symmetric. A row sum gives the arcs out and a column sum gives the arcs in.