download

Adjacency list or matrix: which one for this graph?

Algorithms~10 minReviewed 1 Nov 2026Free · No sign-in

Question

You need to store two undirected graphs: a road or a link works both ways.

  1. A city map for a delivery app: a 100 × 100 grid of crossroads, 10,000 in total. Each crossroads has a road to its neighbour north, south, east and west, where one exists. The app runs BFS to find routes.
  2. A data centre: 40 servers with 700 direct links, out of 780 possible pairs. A scheduler asks "are servers i and j linked?" thousands of times a second.

For each graph, pick an adjacency list or an adjacency matrix. Compare the memory, the edge check and listing a node's neighbours.

Show hints (3)
  1. "COUNT THE ROADS"

    Count the roads. Each row of 100 crossroads has 99 roads between them, and so does each column. Compare that with 10,000 × 10,000 matrix cells.

  2. "WHAT BFS NEEDS"

    BFS keeps asking: who are this node's neighbours? How long does that take with a list, and with a matrix row of 10,000 cells?

  3. "DENSE GRAPHS"

    In the data centre, almost every pair is linked, and a 40 × 40 matrix is tiny. What does it give you for "are i and j linked?"

Show checked explanation

City map: an adjacency list. Its 19,800 roads need 39,600 list entries, against 100 million matrix cells, and BFS reads only real neighbours. Data centre: a matrix. It has only 1,600 cells, most pairs are linked anyway, and "are i and j linked?" is one O(1) lookup.

"SOLUTION · PYTHON"
roads = [(0, 1), (0, 2), (1, 2), (2, 3)]  # a tiny map, 4 crossroads
n = 4

adj = [[] for _ in range(n)]
matrix = [[0] * n for _ in range(n)]
for a, b in roads:
    adj[a].append(b)
    adj[b].append(a)  # undirected: store both ends
    matrix[a][b] = matrix[b][a] = 1

print(adj)
print(matrix[1][3], 3 in adj[1])  # is there a road 1-3?
print(sum(map(len, adj)), n * n)  # entries stored
"OUTPUT"
[[1, 2], [0, 2], [0, 1, 3], [2]]
0 False
8 16

Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.

Walkthrough

On the tiny map, the list keeps 8 entries: each of the 4 roads appears twice, once from each end. The matrix keeps 16 cells, one for every pair.

The city grid has 99 roads in each of its 100 rows, and the same in each column. That is 19,800 roads, so the list holds 39,600 entries. A matrix holds 10,000 × 10,000 = 100,000,000 cells, and only 39,600 of them are 1, about 0.04%. On 64-bit CPython, a list of lists stores an 8-byte pointer per cell. One row of 10,000 cells takes about 80 KB, so the whole matrix takes about 800 MB.

For V nodes and E edges:

taskadjacency listadjacency matrix
memoryO(V + E)O(V²)
is u–v an edge?O(deg u), O(1) average with setsO(1)
neighbours of uO(deg u)O(V)
remove an edgeO(deg u + deg v), O(1) average with setsO(1)
BFS or DFSO(V + E)O(V²)

Here deg u is the number of neighbours of u: at most 4 on the city map.

For the city, BFS is the main job. With a list, it reads each crossroads' 2 to 4 neighbours. With a matrix, it scans a row of 10,000 cells for every crossroads: 100 million checks for one search.

In the data centre, the list would hold 1,400 entries and the matrix 1,600 cells: about the same. But the matrix answers "are i and j linked?" with one index lookup.

Use a list for sparse graphs, where E is much smaller than V². Use a matrix for small or dense graphs, or when the edge check is the hot path.

Common mistakes

  • Saying that removing an edge from an adjacency list is O(1). adj[u].remove(v) scans the list: O(deg u). Use sets if you remove edges often.
  • Storing an undirected road only once. With just adj[a].append(b), crossroads 3 gets no neighbours on the tiny map, and BFS from 3 finds no roads at all.
  • Building the matrix as [[0] * n] * n. That repeats one row object n times, so matrix[0][1] = 1 also sets matrix[3][1]. Build each row with [[0] * n for _ in range(n)].

Follow-up questions

  • "The roads now have travel times. What changes?" In a list, use a dict per node: {neighbour: minutes}. In a matrix, store the time in the cell. If a time can be 0, then 0 can no longer mean "no road". Use math.inf or None for a missing road.
  • "How do you remove a crossroads completely?" With sets in an undirected list, remove it from each neighbour's set, then delete its entry: O(deg) on average. In a directed graph, incoming edges sit in other nodes' lists, so you scan them all: O(V + E). In a matrix, rebuild without its row and column, O(V²), or mark it deleted.

What a strong answer covers

Counts the edges before choosing. Gives memory as O(V + E) against O(V²), with the real numbers for the grid. Knows the matrix wins the edge check and the list wins neighbour scans.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from Crushing the Technical Interview (Keith Henning, 2025); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.