Mathwords logoMathwords

Adjacency Matrix — Definition, Formula & Examples

An adjacency matrix is a square matrix used to represent a graph, where each entry indicates whether a pair of vertices is connected by an edge. A 1 (or the edge weight) appears in position (i,j)(i, j) if there is an edge from vertex ii to vertex jj, and a 0 otherwise.

For a graph G=(V,E)G = (V, E) with n=Vn = |V| vertices labeled v1,v2,,vnv_1, v_2, \ldots, v_n, the adjacency matrix AA is the n×nn \times n matrix where Aij=1A_{ij} = 1 if (vi,vj)E(v_i, v_j) \in E and Aij=0A_{ij} = 0 otherwise. For an undirected graph, AA is symmetric (Aij=AjiA_{ij} = A_{ji}). For a weighted graph, AijA_{ij} equals the weight of the edge rather than 1.

Key Formula

Aij={1if (vi,vj)E0otherwiseA_{ij} = \begin{cases} 1 & \text{if } (v_i, v_j) \in E \\ 0 & \text{otherwise} \end{cases}
Where:
  • AijA_{ij} = Entry in row i, column j of the adjacency matrix
  • vi,vjv_i, v_j = Vertices i and j of the graph
  • EE = The set of edges in the graph

How It Works

Label every vertex with an index from 1 to nn. Create an n×nn \times n grid of zeros. For each edge connecting vertex ii to vertex jj, set Aij=1A_{ij} = 1. If the graph is undirected, also set Aji=1A_{ji} = 1. The diagonal entry AiiA_{ii} is 1 only if vertex ii has a self-loop. Once built, you can use matrix operations on AA; for instance, the entry (Ak)ij(A^k)_{ij} counts the number of walks of length kk from vertex ii to vertex jj.

Worked Example

Problem: Construct the adjacency matrix for an undirected graph with vertices {1, 2, 3} and edges {(1,2), (2,3), (1,3)}.
Step 1: Start with a 3×3 zero matrix since there are 3 vertices.
A=(000000000)A = \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix}
Step 2: For each edge, set both symmetric entries to 1. Edge (1,2): set A12=A21=1A_{12} = A_{21} = 1. Edge (2,3): set A23=A32=1A_{23} = A_{32} = 1. Edge (1,3): set A13=A31=1A_{13} = A_{31} = 1.
A=(011101110)A = \begin{pmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 1 & 1 & 0 \end{pmatrix}
Step 3: Verify: each row sum equals the degree of that vertex. Every vertex connects to the other two, so each row sums to 2.
Answer: The adjacency matrix is A=(011101110)A = \begin{pmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 1 & 1 & 0 \end{pmatrix}, representing the complete graph K3K_3.

Why It Matters

Adjacency matrices are foundational in algorithms courses for implementing graph traversals (BFS, DFS) and shortest-path algorithms. They also appear in network science, where eigenvalues of the adjacency matrix reveal structural properties like connectivity and clustering in social or biological networks.

Common Mistakes

Mistake: Using a symmetric matrix for a directed graph.
Correction: For directed graphs, Aij=1A_{ij} = 1 means an edge from ii to jj, but AjiA_{ji} may be 0 if the reverse edge does not exist. Only undirected graphs guarantee A=ATA = A^T.