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 if there is an edge from vertex to vertex , and a 0 otherwise.
For a graph with vertices labeled , the adjacency matrix is the matrix where if and otherwise. For an undirected graph, is symmetric (). For a weighted graph, equals the weight of the edge rather than 1.
Key Formula
Where:
- = Entry in row i, column j of the adjacency matrix
- = Vertices i and j of the graph
- = The set of edges in the graph
How It Works
Label every vertex with an index from 1 to . Create an grid of zeros. For each edge connecting vertex to vertex , set . If the graph is undirected, also set . The diagonal entry is 1 only if vertex has a self-loop. Once built, you can use matrix operations on ; for instance, the entry counts the number of walks of length from vertex to vertex .
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.
Step 2: For each edge, set both symmetric entries to 1. Edge (1,2): set . Edge (2,3): set . Edge (1,3): set .
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 , representing the complete graph .
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, means an edge from to , but may be 0 if the reverse edge does not exist. Only undirected graphs guarantee .
