TL;DR
An adjacency matrix stores a graph as a grid of numbers. The row and column of each vertex name the same list of vertices, and the entry in row , column is 1 when and are joined by an edge, and 0 when they are not. Edge questions turn into table lookups, and graph questions turn into matrix algebra.
The matrix is also the bridge between graphs and linear algebra. Multiplying it by itself counts paths, and multiplying it by a vector sums values over neighbours.
Where you'll see it: in graph neural networks, which average features over each node's neighbours, in spectral clustering, and in any program that needs to ask "are these two nodes connected?" in constant time.
Intuition
A graph as a table
Take the four-vertex graph from the graph lesson: vertices A, B, C and D, with edges A–B, A–C, B–C, C–D and B–D.
Drawing the graph as a diagram is clear for four vertices and hopeless for four million. A table is the alternative. List the vertices once along the top and once down the side, and mark each pair:
flowchart LR A --- B A --- C B --- C C --- D B --- D
Each row answers one question: "who is joined to this vertex?" Each column answers the same question from the other side. For an undirected graph the table is symmetric, because if A is joined to B then B is joined to A.
Writing it out with vertices in the order A, B, C, D gives
Row B reads 1, 0, 1, 1: B is joined to A, C and D, and not to itself. That is the adjacency matrix.
Why use a matrix at all
A list of edges also describes the graph, and for a sparse network it is more compact. The matrix earns its place for two reasons. A single lookup tells you whether two vertices are joined, with no search. And matrix operations, which are well understood and fast on hardware, give you graph computations for free. The rest of this lesson is about the second reason.
The math
Definition
Let be a graph with vertices in a fixed order. Its adjacency matrix is the matrix with entries
For a simple undirected graph, is symmetric and has zeros on the diagonal, because there are no self-loops.
The symbol is the entry in row and column . The index runs over the vertices in the order you chose, and that choice only permutes rows and columns together, so the same graph gives matrices that differ by a relabelling. The eigenvalues do not depend on the order, which is why the matrix is a property of the graph and not of the labels.
Two variants change one thing each. For a directed graph, when there is an arrow from to , so need not be symmetric. For a weighted graph, is the weight of the edge, and 0 means no edge.
Degrees are row sums
In the undirected case, each row adds up to the degree of its vertex:
For the example, the rows sum to 2, 3, 3 and 2, which matches the degrees in the graph lesson. A directed graph has two versions: row sums give out-degrees and column sums give in-degrees.
Matrix products count walks
This is the most important fact in the lesson. A walk of length is a sequence of vertices in which each step follows an edge, and it may revisit vertices. The entry is the number of walks of length from to .
Here is the reason for . Matrix multiplication sums over a middle vertex:
The term is 1 exactly when there is a step from to and then from to . Summing over counts every such two-step walk. The same argument repeated times gives the general statement.
Two consequences follow immediately. The diagonal of counts closed walks of length 2 from each vertex back to itself, which is the degree, because each neighbour gives one out-and-back walk. And has a positive entry at exactly when there is some walk from to , which is exactly when the graph is connected between them.
Matrix-vector products sum over neighbours
Let be a column vector with one number per vertex, such as a label, a temperature or a feature. Then
Multiplying by replaces each vertex's value with the sum of its neighbours' values. This is one step of message passing, the operation at the heart of graph neural networks. Averaging instead of summing means dividing each row by its degree, which is the same as multiplying by the matrix , where is the diagonal matrix of degrees.
Symmetry and eigenvalues
For an undirected graph, . A real symmetric matrix has real eigenvalues and a full set of orthogonal eigenvectors. That is why spectral methods work for undirected graphs, and it is the starting point for the graph Laplacian, which is built from and the degree matrix as .
Worked examples
Use the four-vertex graph with the matrix above, rows and columns ordered A, B, C, D.
- Compute entry . Row A is and column D is . The products are , so .
- List the walks to check it. Each walk has two steps through a middle vertex with edges to A and D. The middle vertex must be a neighbour of A (B or C) and a neighbour of D (B or C). Both work, giving A–B–D and A–C–D.
- Check the diagonal. , which is the degree of A, since A has neighbours B and C.
- Check another entry. is the dot product of row B, , with column D, . The products are , so the sum is 1. The only two-step walk from B to D is B–C–D, since B–A–D is not a walk (A and D are not joined).
Interpretation: says there are two ways to reach D from A in exactly two steps. The matrix counted them without listing paths, which is the whole point.
Three pages link as follows: page 1 links to pages 2 and 3, page 2 links to page 3, and page 3 links back to page 1. Use when page links to page .
- Out-degrees are the row sums: page 1 has 2, page 2 has 1, page 3 has 1.
- In-degrees are the column sums: page 1 has 1 (from page 3), page 2 has 1 (from page 1), page 3 has 2 (from pages 1 and 2).
- The matrix is not symmetric, since page 1 links to page 2 but page 2 does not link to page 1. This is the directed case.
- Entry counts two-step walks from page 1 to page 3: 1 to 2 to 3 is one. Entry is 1, from 1 to 3 to 1.
Interpretation: the out-degree row sums are what a page's visitors can follow next, and the column sums are how many pages point to each page. Ranking algorithms for the web work from exactly these two sums.
Tradeoffs and failure modes
The matrix is a fixed grid, so its cost depends on the number of vertices, not the number of edges. That is the main tradeoff.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Large sparse graph (a road map, a social network) | Most entries are 0, so the matrix wastes storage and multiplication work | Sparse matrix or adjacency list, which stores only the nonzero entries |
| Need to check one edge quickly | A list requires a search through neighbours | The matrix, with an lookup |
| Dense graph, almost every pair joined | A list costs as much as the matrix and is slower to index | The matrix |
| Want linear algebra (spectral methods, neighbour averaging) | A list does not support matrix products directly | The matrix, often in a normalised form |
| Vertices change often (adding and removing nodes) | Resizing an matrix is expensive | An adjacency list |
Two failure modes come up often. First, the matrix depends on the vertex order only up to relabelling, so comparing two matrices needs a matching of their vertices, not a direct subtraction. Second, for self-loops or multiple edges, the simple 0 and 1 entries are not enough, and the matrix must store counts or weights instead.
History
Describing a graph by a table of which vertices are joined is an old and natural idea, and the matrix became a standard tool in graph theory as linear algebra and graph theory grew together. The spectral study of graphs, which asks what the eigenvalues of reveal about structure, belongs to that tradition, as does the graph Laplacian.
The matrix became central to machine learning recently. Graph neural networks, including the graph convolutional network of Kipf and Welling in 2016, use to mix each node's features with its neighbours' features, which is the message-passing step described above.
Check yourself
QuestionRecall: what is the entry of an undirected graph's adjacency matrix?Show answer
is 1 when vertices and are joined by an edge, and 0 otherwise. For a simple undirected graph it is also 0 on the diagonal, since there are no self-loops.
QuestionComputation: a graph has vertices A, B, C with edges A–B and B–C. Write its adjacency matrix and compute .Show answer
With the order A, B, C the matrix is
Row A is and column C is . Their dot product is , so . The one two-step walk is A–B–C.
QuestionWhy: why does sum neighbour values, and what does that imply for averaging?Show answer
Entry of is . Only the with contribute, and those are exactly the neighbours of , so the result is a sum over neighbours. To average instead of sum, divide each row by its degree. That changes the sum into a mean, and it is the normalisation that keeps values from growing with the number of neighbours.
QuestionApplied: a road network has 1,000 junctions and 2,500 roads, and you want to store it. Compare the storage of the adjacency matrix and an adjacency list.Show answer
The matrix has entries. An undirected adjacency list stores each road in both directions, so neighbour entries. The list is about 200 times smaller. Use the matrix only if the graph is dense, meaning many of the possible roads actually exist. A road network is sparse, so the list is the better choice, though the matrix would still support fast edge lookups and matrix methods if they were needed.