Skip to content

Chapter 76. Adjacency Matrices

An adjacency matrix is a matrix representation of a finite graph. It records which pairs of vertices are joined by edges.

Let

G=(V,E) G = (V,E)

be a graph with vertex set

V={v1,v2,,vn}. V = \{v_1, v_2, \ldots, v_n\}.

The adjacency matrix of GG, denoted A(G)A(G), is the n×nn \times n matrix

A(G)=(aij), A(G) = (a_{ij}),

where

aij={1,if vi is adjacent to vj,0,otherwise. a_{ij} = \begin{cases} 1, & \text{if } v_i \text{ is adjacent to } v_j, \\ 0, & \text{otherwise.} \end{cases}

For a simple graph, the diagonal entries are zero, since a vertex has no loop to itself. For an undirected graph, the matrix is symmetric, because adjacency does not depend on order: if viv_i is adjacent to vjv_j, then vjv_j is adjacent to viv_i.

76.1 From Graphs to Matrices

The main purpose of the adjacency matrix is to translate graph structure into linear algebra.

A graph is a discrete object. It consists of vertices and edges. A matrix is an algebraic object. It can be added, multiplied, factored, diagonalized, and studied through eigenvalues.

The adjacency matrix gives a bridge between these two languages. Once a graph is written as a matrix, questions about adjacency, walks, connectivity, and symmetry can be studied with matrix operations.

Consider the graph with vertices

V={v1,v2,v3,v4} V = \{v_1,v_2,v_3,v_4\}

and edges

E={{v1,v2},{v1,v3},{v2,v3},{v3,v4}}. E = \{\{v_1,v_2\}, \{v_1,v_3\}, \{v_2,v_3\}, \{v_3,v_4\}\}.

Then the adjacency matrix is

A(G)=[0110101011010010]. A(G)= \begin{bmatrix} 0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 1 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}.

The first row says that v1v_1 is adjacent to v2v_2 and v3v_3, but not to v4v_4. The third row says that v3v_3 is adjacent to v1v_1, v2v_2, and v4v_4.

76.2 Labeling of Vertices

An adjacency matrix depends on an ordering of the vertices.

If the vertices are listed as

v1,v2,,vn, v_1, v_2, \ldots, v_n,

then row ii and column ii both correspond to viv_i. A different ordering of the same vertices gives a different matrix, but it represents the same graph.

For example, suppose the vertex labels are permuted. The rows and columns of the adjacency matrix are permuted in the same way. Thus the matrix changes by simultaneous row and column permutation.

This is why the adjacency matrix represents a labeled graph directly. For an unlabeled graph, the matrix represents the graph only up to relabeling.

76.3 Symmetry

For an undirected graph, adjacency is symmetric:

vivjvjvi. v_i \sim v_j \quad \Longleftrightarrow \quad v_j \sim v_i.

Therefore

aij=aji a_{ij} = a_{ji}

for all i,ji,j. Hence

A(G)T=A(G). A(G)^T = A(G).

Thus the adjacency matrix of an undirected graph is symmetric. This fact is fundamental in algebraic graph theory. A real symmetric matrix has real eigenvalues and admits an orthogonal basis of eigenvectors. Therefore many structural properties of undirected graphs can be studied spectrally.

For a directed graph, symmetry usually fails. If there is an arc from viv_i to vjv_j, there need not be an arc from vjv_j to viv_i. In that case, the adjacency matrix may satisfy

aijaji. a_{ij} \neq a_{ji}.

Thus directed graphs naturally lead to nonsymmetric matrices.

76.4 Diagonal Entries

In a simple graph, no vertex is adjacent to itself. Therefore

aii=0 a_{ii} = 0

for every ii. The diagonal of the adjacency matrix is zero.

If loops are allowed, then diagonal entries may be nonzero. A loop at viv_i is recorded in the entry aiia_{ii}. Different conventions are used. In some settings, a loop contributes 11 to aiia_{ii}. In degree counting for undirected graphs, a loop often contributes 22 to the degree. The convention must be stated clearly when loops are present.

76.5 Directed Graphs

Let DD be a directed graph with vertices

V={v1,v2,,vn}. V = \{v_1, v_2, \ldots, v_n\}.

The adjacency matrix of DD is the matrix

A(D)=(aij), A(D) = (a_{ij}),

where

aij={1,if there is an arc from vi to vj,0,otherwise. a_{ij} = \begin{cases} 1, & \text{if there is an arc from } v_i \text{ to } v_j, \\ 0, & \text{otherwise.} \end{cases}

The row index records the initial vertex. The column index records the terminal vertex.

For example, if the arcs are

v1v2,v2v3,v3v1, v_1 \to v_2,\quad v_2 \to v_3,\quad v_3 \to v_1,

then

A(D)=[010001100]. A(D)= \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{bmatrix}.

The entry in row 11, column 22 is 11, because there is an arc from v1v_1 to v2v_2. The entry in row 22, column 11 is 00, because there is no arc from v2v_2 to v1v_1.

76.6 Multigraphs

For a multigraph, two vertices may be joined by more than one edge. The adjacency matrix records the number of edges between the vertices.

Thus

aij=the number of edges joining vi and vj. a_{ij} = \text{the number of edges joining } v_i \text{ and } v_j.

For a directed multigraph,

aij=the number of arcs from vi to vj. a_{ij} = \text{the number of arcs from } v_i \text{ to } v_j.

This generalization replaces a 00-11 matrix by a matrix with nonnegative integer entries.

76.7 Weighted Graphs

For a weighted graph, each edge carries a numerical weight. The adjacency matrix may store these weights.

If wijw_{ij} is the weight of the edge between viv_i and vjv_j, then

aij={wij,if vi and vj are adjacent,0,otherwise. a_{ij} = \begin{cases} w_{ij}, & \text{if } v_i \text{ and } v_j \text{ are adjacent,} \\ 0, & \text{otherwise.} \end{cases}

For weighted directed graphs, aija_{ij} stores the weight of the arc from viv_i to vjv_j.

In weighted graphs, the value 00 needs care. If zero-weight edges are allowed, then the matrix entry 00 can no longer mean only “no edge.” In such cases, one may use a separate adjacency relation, a special symbol, or another convention.

76.8 Degree and Row Sums

For a simple undirected graph, the degree of viv_i is the number of vertices adjacent to viv_i. In the adjacency matrix, this is the sum of row ii:

deg(vi)=j=1naij. \deg(v_i) = \sum_{j=1}^{n} a_{ij}.

Since the matrix is symmetric, the same number is also the sum of column ii:

deg(vi)=j=1naji. \deg(v_i) = \sum_{j=1}^{n} a_{ji}.

For a directed graph, row sums and column sums have different meanings.

The out-degree of viv_i is

deg+(vi)=j=1naij. \deg^+(v_i) = \sum_{j=1}^{n} a_{ij}.

The in-degree of viv_i is

deg(vi)=j=1naji. \deg^-(v_i) = \sum_{j=1}^{n} a_{ji}.

Thus rows count outgoing arcs, and columns count incoming arcs.

76.9 Edge Count

For a simple undirected graph, each edge appears twice in the adjacency matrix: once as aij=1a_{ij}=1 and once as aji=1a_{ji}=1. Therefore

E=12i=1nj=1naij. |E| = \frac{1}{2}\sum_{i=1}^{n}\sum_{j=1}^{n} a_{ij}.

Equivalently,

i=1ndeg(vi)=2E. \sum_{i=1}^{n} \deg(v_i) = 2|E|.

This is the handshaking identity in matrix form.

For a directed graph, each arc appears once in the matrix. Therefore

E=i=1nj=1naij. |E| = \sum_{i=1}^{n}\sum_{j=1}^{n} a_{ij}.

76.10 Matrix Powers and Walks

The powers of the adjacency matrix count walks.

Let AA be the adjacency matrix of a graph GG. Then the (i,j)(i,j)-entry of AkA^k equals the number of walks of length kk from viv_i to vjv_j. This holds for directed and undirected graphs, with the direction respected in the directed case.

For k=1k=1, this is exactly the definition of adjacency. The entry aija_{ij} counts walks of length 11 from viv_i to vjv_j.

For k=2k=2,

(A2)ij==1naiaj. (A^2)_{ij} = \sum_{\ell=1}^{n} a_{i\ell}a_{\ell j}.

Each product aiaja_{i\ell}a_{\ell j} contributes 11 precisely when there is a walk

vivvj. v_i \to v_\ell \to v_j.

Thus (A2)ij(A^2)_{ij} counts all two-step walks from viv_i to vjv_j.

The same argument extends by induction to every positive integer kk.

76.11 Connectivity

Adjacency matrices can also detect connectivity.

For an undirected graph with nn vertices, two distinct vertices viv_i and vjv_j lie in the same connected component if and only if there exists some integer kk with

1kn1 1 \leq k \leq n-1

such that

(Ak)ij>0. (A^k)_{ij} > 0.

Equivalently, the matrix

A+A2++An1 A + A^2 + \cdots + A^{n-1}

has a positive (i,j)(i,j)-entry exactly when there is a walk from viv_i to vjv_j.

The upper bound n1n-1 is enough because if two vertices are connected, then there is a simple path between them, and a simple path in an nn-vertex graph has length at most n1n-1.

76.12 Triangles

The adjacency matrix gives a compact formula for counting triangles in a simple undirected graph.

A triangle is a cycle of length 33. The number of closed walks of length 33 beginning and ending at viv_i is the diagonal entry (A3)ii(A^3)_{ii}. Therefore the total number of closed walks of length 33 is

tr(A3). \operatorname{tr}(A^3).

Each triangle contributes 66 such walks: one for each choice of starting vertex and one for each direction around the triangle. Hence the number of triangles in a simple undirected graph is

tr(A3)6. \frac{\operatorname{tr}(A^3)}{6}.

This formula illustrates the power of matrix methods. A local graph pattern becomes a trace computation.

76.13 Isomorphism and Permutation Matrices

Adjacency matrices also express graph isomorphism.

Let GG and HH be graphs with adjacency matrices AGA_G and AHA_H. If GG and HH are isomorphic, then there is a permutation matrix PP such that

AH=PAGP1. A_H = P A_G P^{-1}.

Since permutation matrices satisfy

P1=PT, P^{-1} = P^T,

this is often written as

AH=PAGPT. A_H = P A_G P^T.

This equation says that relabeling vertices corresponds to permuting rows and columns in the same way. Therefore isomorphic graphs have adjacency matrices that differ only by a simultaneous permutation of rows and columns.

The converse also holds: if such a permutation matrix exists, then the graphs are isomorphic.

76.14 Spectrum of a Graph

For an undirected graph, the adjacency matrix is real and symmetric. Therefore its eigenvalues are real. These eigenvalues form the adjacency spectrum of the graph.

If

Ax=λx A x = \lambda x

for a nonzero vector xx, then λ\lambda is an eigenvalue of the graph and xx is an eigenvector.

The spectrum is an isomorphism invariant. If two graphs are isomorphic, then their adjacency matrices are related by a similarity transformation. Similar matrices have the same eigenvalues. Hence isomorphic graphs have the same adjacency spectrum.

The converse is false. Different non-isomorphic graphs may have the same spectrum. Such graphs are called cospectral graphs.

The spectrum still carries important information. It is related to regularity, bipartiteness, expansion, walks, and many extremal properties of graphs.

76.15 Regular Graphs

A graph is dd-regular if every vertex has degree dd. In matrix form, this means every row sum of AA is dd.

Let

1=[111]. \mathbf{1} = \begin{bmatrix} 1 \\ 1 \\ \vdots \\ 1 \end{bmatrix}.

If GG is dd-regular, then

A1=d1. A\mathbf{1} = d\mathbf{1}.

Thus dd is an eigenvalue of AA, with eigenvector 1\mathbf{1}.

This simple fact is the starting point for spectral graph theory. In a regular graph, the largest eigenvalue reflects the common degree, while the remaining eigenvalues describe finer structure.

76.16 Bipartite Graphs

If GG is bipartite with parts XX and YY, and the vertices are ordered so that all vertices of XX come first and all vertices of YY come second, then the adjacency matrix has block form

A=[0BBT0]. A = \begin{bmatrix} 0 & B \\ B^T & 0 \end{bmatrix}.

The zero blocks occur because there are no edges inside XX and no edges inside YY. All edges go between the two parts.

The matrix BB is called the biadjacency matrix. It records which vertices of XX are adjacent to which vertices of YY.

This block form is useful in matching theory, spectral graph theory, and the study of networks with two types of vertices.

76.17 Complete Graphs

The complete graph KnK_n has every pair of distinct vertices adjacent. Its adjacency matrix is

A(Kn)=JnIn, A(K_n) = J_n - I_n,

where JnJ_n is the n×nn \times n all-ones matrix and InI_n is the identity matrix.

Thus

A(Kn)=[011101110]. A(K_n)= \begin{bmatrix} 0 & 1 & \cdots & 1 \\ 1 & 0 & \cdots & 1 \\ \vdots & \vdots & \ddots & \vdots \\ 1 & 1 & \cdots & 0 \end{bmatrix}.

Every row has sum n1n-1, so KnK_n is (n1)(n-1)-regular.

76.18 Empty Graphs

The empty graph on nn vertices has no edges. Its adjacency matrix is the zero matrix:

A=0n×n. A = 0_{n \times n}.

All degrees are zero. All powers of AA are also zero. There are no walks of positive length between any pair of vertices.

This is the opposite extreme from the complete graph.

76.19 Paths and Cycles

The path graph PnP_n has vertices arranged in a line:

v1v2vn. v_1 - v_2 - \cdots - v_n.

Its adjacency matrix has ones immediately above and below the diagonal:

A(Pn)=[010010100100100010]. A(P_n)= \begin{bmatrix} 0 & 1 & 0 & \cdots & 0 \\ 1 & 0 & 1 & \cdots & 0 \\ 0 & 1 & 0 & \ddots & 0 \\ \vdots & \vdots & \ddots & \ddots & 1 \\ 0 & 0 & 0 & 1 & 0 \end{bmatrix}.

The cycle graph CnC_n has the same adjacent structure, with additional edges between v1v_1 and vnv_n. Thus its adjacency matrix has two extra ones in the corner positions:

a1n=an1=1. a_{1n} = a_{n1} = 1.

These matrices are sparse. Most entries are zero.

76.20 Storage and Computation

An adjacency matrix uses n2n^2 entries for a graph with nn vertices. This is efficient for dense graphs, where many possible edges are present. It may be wasteful for sparse graphs, where the number of edges is much smaller than n2n^2.

The main advantage is constant-time adjacency testing. To check whether viv_i and vjv_j are adjacent, one reads the entry aija_{ij}.

The main disadvantage is neighbor listing. To find all neighbors of viv_i, one must scan row ii, which takes time proportional to nn. In an adjacency list, listing neighbors takes time proportional to the degree of viv_i.

Thus the adjacency matrix is natural for dense graphs, algebraic methods, and spectral theory. The adjacency list is often better for large sparse graphs and traversal algorithms.

76.21 Summary

The adjacency matrix records the edge relation of a finite graph in matrix form.

For a simple graph,

aij=1 a_{ij}=1

when viv_i and vjv_j are adjacent, and

aij=0 a_{ij}=0

otherwise.

For undirected graphs, the matrix is symmetric. For directed graphs, the entry aija_{ij} records an arc from viv_i to vjv_j. For multigraphs, entries count parallel edges. For weighted graphs, entries may store weights.

The adjacency matrix supports several basic computations.

Graph propertyMatrix expression
Degree of viv_iRow sum jaij\sum_j a_{ij}
Number of edges in simple undirected graph12i,jaij\frac{1}{2}\sum_{i,j}a_{ij}
Walks of length kk from viv_i to vjv_jEntry (Ak)ij(A^k)_{ij}
Number of trianglestr(A3)/6\operatorname{tr}(A^3)/6
Relabeling verticesPAP1PAP^{-1}
RegularityA1=d1A\mathbf{1}=d\mathbf{1}

The importance of the adjacency matrix lies in this translation. It turns graph structure into linear algebra. This translation is the foundation of algebraic graph theory, spectral graph theory, and many computational methods for networks.