Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 Maths - General (Unit 3 & Unit 4) Graphs, networks and travelling problems

Matrices for graphs, digraphs and networks

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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.

Key idea. Read across a row to see where a vertex connects. Undirected: degree = row sum + diagonal entry (a loop adds \(2\) to the degree but only \(1\) to the matrix). Directed: row sum = arcs out, column sum = arcs in.
Graph with a loop and a double edgeSquare A B C D with diagonal B D, a loop at A and two edges joining B and C. A B C D
\(A\)\(B\)\(C\)\(D\)
\(A\)1101
\(B\)1021
\(C\)0201
\(D\)1110
Rows and columns in the order \(A, B, C, D\). Two edges join \(B\) and \(C\), so that entry is \(2\). The loop at \(A\) counts \(1\) on the diagonal.
Directed graphArcs P to Q, Q to R, R to P, R to S, S to Q and P to S. P Q R S
from \ to\(P\)\(Q\)\(R\)\(S\)
\(P\)0101
\(Q\)0010
\(R\)1001
\(S\)0100
A digraph: rows are \(\text{from}\), columns are \(\text{to}\). The arc \(P \to Q\) gives a \(1\) in row \(P\), column \(Q\), but row \(Q\), column \(P\) is \(0\).

For an undirected graph, the entry in row \(u\), column \(v\) is:

\[a_{uv} = \text{number of edges joining } u \text{ and } v, \qquad a_{uu} = \text{number of loops at } u\]
auv=edges joining u and v

With a loop counted \(1\) on the diagonal, the degree of a vertex is its row sum plus its diagonal entry:

\[\deg(u) = \text{row sum of } u + a_{uu}\]
deg(u)=row sum+auu

Each edge appears once on or above the diagonal, so:

\[\text{number of edges} = \text{sum of the entries on and above the diagonal}\]
e=∑auv,u≤v

For a digraph (rows = from, columns = to):

\[\text{arcs out of } u = \text{row sum}, \qquad \text{arcs into } u = \text{column sum}, \qquad \text{total arcs} = \text{sum of all entries}\]
out=row sum,in=column sum
Checks. An undirected graph’s matrix must be symmetric. A simple graph has \(0\)s on the diagonal and only \(0\)s and \(1\)s elsewhere. A complete graph has \(1\) in every entry off the diagonal.

How to build an adjacency matrix (and go back again)

  1. Fix the order. Write the vertices across the top and down the side in the same order (usually alphabetical).
  2. 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.
  3. Check. An undirected matrix must be symmetric. Total the entries on and above the diagonal and compare with the number of edges.
  4. 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.
Example 1 — Matrix of a simple graph
Write the adjacency matrix of the graph shown, with rows and columns in the order \(A, B, C, D, E\).
Solution

Go along each row and put a \(1\) under every vertex the row vertex is joined to.

Example 1 graphPentagon A B C D E with extra edges B E and B D. A B C D E
\(\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}\).

adjacency matrix, 5 by 5, symmetric

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.

Example 2 — Loops, multiple edges and degree
The graph shows walking tracks between four campsites in the Grampians. The track at \(M\) is a circuit that starts and finishes at \(M\). (i) Write the adjacency matrix (order \(K, L, M, N\); a loop counts \(1\)). (ii) Use it to find \(\deg(M)\) and the number of tracks.
Solution

Two tracks join \(K\) and \(L\), so that entry is \(2\). The circuit at \(M\) is a loop.

Walking tracks between four campsitesCampsites K, L, M, N. Two tracks join K and L; tracks LM, MN, KN, KM; a circular track at M. K L M N

(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\)
deg(M)=5

(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.

Example 3 — Matrix of a digraph
One-way mountain-bike tracks link four lookouts at Mount Buller. (i) Write the adjacency matrix with rows = from and columns = to, in the order \(T, U, V, W\). (ii) How many tracks leave \(U\), and how many arrive at \(V\)? (iii) How many tracks are there?
Solution

Each arrow puts a \(1\) in the row it leaves and the column it enters.

One-way bike tracks between four lookoutsArcs T to U, T to V, U to V, V to W, W to T, W to U and U to W. T U V W

(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\)
2,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.

Example 4 — Graph from a matrix
Draw the graph with adjacency matrix \(\begin{bmatrix}0 & 1 & 1 & 0\\ 1 & 1 & 0 & 2\\ 1 & 0 & 0 & 1\\ 0 & 2 & 1 & 0\end{bmatrix}\) (order \(A, B, C, D\); a loop counts \(1\)). Find the number of edges and \(\deg(B)\). Is the graph connected?
Solution

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\)
Graph drawn from the matrixA joined to B and C; a loop at B; two edges joining B and D; C joined to D. A B C D
e=6,deg(B)=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

Loops: 1 in the matrix, 2 in the degree. Here a loop counts \(1\) on the diagonal, but it still adds \(2\) to the degree. Watch out for using the row sum alone as the degree when the diagonal entry is not \(0\).
Rows are from, columns are to. For a digraph, watch out for filling in an arc in both directions. Only the entry in the row the arc leaves and the column it enters becomes \(1\).
Counting edges twice. In an undirected matrix every edge appears twice (once each side of the diagonal). Add the entries on and above the diagonal only, not the whole matrix.

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.