Search

Search papers and lessons

Adjacency Matrix

Level 2 · 10 min read

An adjacency matrix records a graph as a grid: entry (i, j) is 1 when vertices i and j are joined, so questions about edges become lookups and matrix products count walks.

On this page

  • Colour = topic mix
  • Discrete math
  • Linear algebra
  • Sign in to see your progress

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 ii, column jj is 1 when ii and jj 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

A=(0110101111010110).A = \begin{pmatrix} 0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 1 \\ 1 & 1 & 0 & 1 \\ 0 & 1 & 1 & 0 \end{pmatrix}.

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

Definition: Adjacency matrix

Let G=(V,E)G = (V, E) be a graph with vertices v1,…,vnv_1, \dots, v_n in a fixed order. Its adjacency matrix is the n×nn \times n matrix AA with entries

Aij={1if {vi,vj}∈E,0otherwise.A_{ij} = \begin{cases} 1 & \text{if } \{v_i, v_j\} \in E, \\ 0 & \text{otherwise.} \end{cases}

For a simple undirected graph, AA is symmetric and has zeros on the diagonal, because there are no self-loops.

The symbol AijA_{ij} is the entry in row ii and column jj. The index ii 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, Aij=1A_{ij} = 1 when there is an arrow from viv_i to vjv_j, so AA need not be symmetric. For a weighted graph, AijA_{ij} 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:

∑j=1nAij=deg⁡(vi).\sum_{j=1}^{n} A_{ij} = \deg(v_i).

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 kk is a sequence of vertices in which each step follows an edge, and it may revisit vertices. The entry (Ak)ij(A^k)_{ij} is the number of walks of length kk from viv_i to vjv_j.

Here is the reason for k=2k = 2. Matrix multiplication sums over a middle vertex:

(A2)ij=∑m=1nAimAmj.(A^2)_{ij} = \sum_{m=1}^{n} A_{im} A_{mj}.

The term AimAmjA_{im} A_{mj} is 1 exactly when there is a step from viv_i to vmv_m and then from vmv_m to vjv_j. Summing over mm counts every such two-step walk. The same argument repeated kk times gives the general statement.

Two consequences follow immediately. The diagonal of A2A^2 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 ∑k≥0Ak\sum_{k \ge 0} A^k has a positive entry at (i,j)(i, j) exactly when there is some walk from viv_i to vjv_j, which is exactly when the graph is connected between them.

Matrix-vector products sum over neighbours

Let x\mathbf{x} be a column vector with one number per vertex, such as a label, a temperature or a feature. Then

(Ax)i=∑jAijxj=∑j adjacent to ixj.(A\mathbf{x})_i = \sum_{j} A_{ij} x_j = \sum_{j \text{ adjacent to } i} x_j.

Multiplying by AA 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 D−1AD^{-1} A, where DD is the diagonal matrix of degrees.

Symmetry and eigenvalues

For an undirected graph, AT=AA^T = A. 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 AA and the degree matrix as L=D−AL = D - A.

Worked examples

Example
Counting walks from A to D

Use the four-vertex graph with the matrix AA above, rows and columns ordered A, B, C, D.

  1. Compute A2A^2 entry (A,D)(A, D). Row A is (0,1,1,0)(0, 1, 1, 0) and column D is (0,1,1,0)(0, 1, 1, 0). The products are 0,1,1,00, 1, 1, 0, so (A2)AD=2(A^2)_{AD} = 2.
  2. List the walks to check it. Each walk has two steps through a middle vertex mm 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.
  3. Check the diagonal. (A2)AA=2(A^2)_{AA} = 2, which is the degree of A, since A has neighbours B and C.
  4. Check another entry. (A2)BD(A^2)_{BD} is the dot product of row B, (1,0,1,1)(1, 0, 1, 1), with column D, (0,1,1,0)(0, 1, 1, 0). The products are 0,0,1,00, 0, 1, 0, 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: (A2)AD=2(A^2)_{AD} = 2 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.

Example
A directed link graph

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 Aij=1A_{ij} = 1 when page ii links to page jj.

A=(011001100).A = \begin{pmatrix} 0 & 1 & 1 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix}.
  1. Out-degrees are the row sums: page 1 has 2, page 2 has 1, page 3 has 1.
  2. 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).
  3. 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.
  4. Entry (A2)13(A^2)_{13} counts two-step walks from page 1 to page 3: 1 to 2 to 3 is one. Entry (A2)11(A^2)_{11} 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 n×nn \times n grid, so its cost depends on the number of vertices, not the number of edges. That is the main tradeoff.

SituationWhat goes wrongBetter choice
Large sparse graph (a road map, a social network)Most entries are 0, so the matrix wastes storage and multiplication workSparse matrix or adjacency list, which stores only the nonzero entries
Need to check one edge quicklyA list requires a search through neighboursThe matrix, with an O(1)O(1) lookup
Dense graph, almost every pair joinedA list costs as much as the matrix and is slower to indexThe matrix
Want linear algebra (spectral methods, neighbour averaging)A list does not support matrix products directlyThe matrix, often in a normalised form
Vertices change often (adding and removing nodes)Resizing an n×nn \times n matrix is expensiveAn 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 AA 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 AA 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 AijA_{ij} of an undirected graph's adjacency matrix?Show answer

AijA_{ij} is 1 when vertices viv_i and vjv_j 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 (A2)AC(A^2)_{AC}.Show answer

With the order A, B, C the matrix is

A=(010101010).A = \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix}.

Row A is (0,1,0)(0, 1, 0) and column C is (0,1,0)(0, 1, 0). Their dot product is 0+1+0=10 + 1 + 0 = 1, so (A2)AC=1(A^2)_{AC} = 1. The one two-step walk is A–B–C.

QuestionWhy: why does AxA\mathbf{x} sum neighbour values, and what does that imply for averaging?Show answer

Entry ii of AxA\mathbf{x} is ∑jAijxj\sum_j A_{ij} x_j. Only the jj with Aij=1A_{ij} = 1 contribute, and those are exactly the neighbours of viv_i, 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 1000×1000=1061000 \times 1000 = 10^6 entries. An undirected adjacency list stores each road in both directions, so 2×2500=50002 \times 2500 = 5000 neighbour entries. The list is about 200 times smaller. Use the matrix only if the graph is dense, meaning many of the 10610^6 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.

Sources