Adjacency matrix, adjacency list, edge list
Store a graph as an adjacency matrix, an adjacency list or an edge list. Click an edge to see it in all three and compare their costs.
Before you start: Graphs: vertices, edges, directed, weighted, degree, paths.
The idea
One graph, three ways to store it. Edge 1-3 shows up in the picture, in the matrix and in the lists.
A program cannot see the drawing. It needs the edges in memory, and the layout decides which questions are fast.
Intuition
An edge list is just the edges, one pair per row. Six edges take six rows.
Input files and Kruskal's algorithm use edge lists. To find the neighbours of one vertex you must read every row.
An adjacency matrix has one row and one column per vertex. Cell (1, 3) is 1 because edge 1-3 exists.
An adjacency list keeps one list per vertex. List 1 holds the neighbours of 1: 0, 2 and 3.
For a directed graph, row u, column v means an arrow from u to v. Cell (0, 1) is 1, but cell (1, 0) is 0.
An undirected matrix is symmetric across its diagonal. A directed one usually is not.
Complexity
A matrix stores V² cells whatever the edges. Lists store V + 2E entries, far fewer when most pairs are not joined.
A graph with few edges per vertex is called sparse. Road maps and social networks are sparse, so they use lists.
The matrix answers 'is u-v an edge?' in O(1). Lists find all neighbours of u in O(deg u).
Pitfalls
An undirected edge must be stored in both directions. Without cell (3, 1), vertex 3 never finds its neighbour 1.
Listing the neighbours of 4 from a matrix reads the whole row: 5 cells for 1 neighbour. The list reads 1 entry.
Variations
For a weighted graph the matrix stores the weight, and – marks a missing edge. Each list entry stores a neighbour with its weight.
Code
Pseudocode
matrix ← n × n grid of 0
adj ← n empty lists
for each edge (u, v) in the edge list
matrix[u][v] ← 1, matrix[v][u] ← 1
append v to adj[u], append u to adj[v]
return matrix, adj
Python
def build(n, edges):
matrix = [[0] * n for _ in range(n)]
adj = [[] for _ in range(n)]
for u, v in edges:
matrix[u][v] = matrix[v][u] = 1
adj[u].append(v)
adj[v].append(u)
return matrix, adj
JavaScript
function build(n, edges) {
const matrix = Array.from({ length: n }, () => new Array(n).fill(0));
const adj = Array.from({ length: n }, () => []);
for (const [u, v] of edges) {
matrix[u][v] = matrix[v][u] = 1;
adj[u].push(v);
adj[v].push(u);
}
return { matrix, adj };
}
Java
void build(int n, int[][] edges) {
int[][] matrix = new int[n][n];
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
for (int[] e : edges) {
matrix[e[0]][e[1]] = matrix[e[1]][e[0]] = 1;
adj.get(e[0]).add(e[1]);
adj.get(e[1]).add(e[0]);
}
}
Practice problems (7)
Build an adjacency list Warm-up
The edge list 0-1, 1-2, 0-3, 1-3, 3-4, 2-4 is read from top to bottom. Each undirected edge u-v appends v to list u and u to list v.
Fill in each value before the step plays.
Memory cost Warm-up
A road map has 10,000 crossings (vertices) and 25,000 roads (undirected edges). About how many values does an adjacency matrix store, and how many does an adjacency list store?
Matrix to list Standard
matrix is an n × n adjacency matrix: matrix[u][v] is 1 when there is an edge from u to v, else 0. Return the adjacency list: entry u lists every v with matrix[u][v] = 1, in increasing order.
Edge list to matrix Standard
graph has vertices 0 to n - 1 and an edge list edges. Return its n × n adjacency matrix with 1 for an edge and 0 otherwise.
When graph.directed is false, an edge [u, v] sets both matrix[u][v] and matrix[v][u]. An edge listed twice is still a single 1.
Look up an edge Standard
The same graph is stored as a matrix and as adjacency lists. Two questions are asked: is 2-3 an edge, and is 4-1 an edge?
The matrix reads one cell. The list for u is read from left to right until v is found or the list ends.
Pick the representation Standard
A program stores a graph of 200 vertices where almost every pair is joined. It asks millions of times whether two given vertices are joined. Which representation fits best?
Degree from each representation Challenge
One undirected graph with vertices 0 to n - 1 and no self-loops arrives in one of three forms, named by kind:
"edges":datais an edge list, each edge[u, v]listed once."matrix":datais the n × n adjacency matrix."list":datais the adjacency list, one list per vertex.
Return the degree of every vertex.
Related topics
Next on the path: Implicit graphs: grids, states and puzzles