Sub topic 11
Space and Time Trade-offs: Choosing a Representation
This chapter assumes you know the edge list, adjacency matrix, and adjacency list from the earlier chapters. It also assumes introductory Big-O and basic Python. Throughout, V is the number of vertices and E is the number of edges.
What You Compare
Three costs decide most choices:
- Space: memory used to store the graph.
- Edge lookup: is there an edge from u to v?
- Neighbor iteration: list all vertices adjacent to u.
Graph density links these costs. A graph with E close to V² is dense. A graph with E close to V is sparse. Most real graphs, such as road maps and social networks, are sparse.
The Comparison Table
| Operation | Edge list | Adjacency matrix | Adjacency list |
|---|---|---|---|
| Space | O(E) | O(V²) | O(V + E) |
| Edge lookup (u, v) | O(E) | O(1) | O(deg(u)) |
| Iterate neighbors of u | O(E) | O(V) | O(deg(u)) |
| Add an edge | O(1) | O(1) | O(1) |
| Iterate all edges | O(E) | O(V²) | O(V + E) |
The adjacency list lookup cost is O(deg(u)) because you scan one vertex's neighbor list. If you store neighbors in a set or dictionary instead, the average lookup drops to O(1).
# Neighbors as sets: O(1) average lookup, still O(deg(u)) iteration
graph = {
"A": {"B", "C"},
"B": {"A", "D"},
"C": {"A", "D"},
"D": {"B", "C"},
}
print("B" in graph["A"]) # True
print("D" in graph["A"]) # FalseWhere the Matrix Wastes Space
A matrix always allocates V² cells. With 100,000 vertices, that is 10 billion cells, even if each vertex has only 5 neighbors. The adjacency list stores about 100,000 + 500,000 entries instead. Iterating neighbors also hurts. In a matrix you scan a full row of V cells to find 5 neighbors.
The matrix pays off when the graph is dense, or when V is small and you need constant-time lookups. A dense graph with 1,000 vertices uses 1,000,000 cells, which is fine.
placeholderDecision Flow
Rules of Thumb
- Default to the adjacency list. It suits sparse graphs and traversals such as breadth-first search.
- Choose the matrix when the graph is dense, V is small, or your algorithm asks "is there an edge?" many times. Algorithms that fill in all pairs of vertices also fit a matrix.
- Choose the edge list when you only store edges, read them once, or sort them by weight.
- Convert between forms when a workload changes. Building an adjacency list from an edge list takes O(V + E).
Common Errors
- Using a matrix for a million-vertex sparse graph. It runs out of memory before the algorithm starts.
- Calling
(u, v) in edgeson an edge list inside a loop. This makes the whole algorithm O(E) per check. - Forgetting that an undirected edge occupies two entries in an adjacency list and two cells in a matrix.
- Storing neighbors in a list and then testing membership repeatedly on high-degree vertices.
Exercise
A graph has 50,000 vertices and 200,000 edges. Your algorithm runs breadth-first search from many start points. Which representation do you choose, and what is the approximate space cost compared with a matrix?
Show answer
Use an adjacency list. It needs about V + E = 250,000 entries (twice E for an undirected graph, so roughly 450,000). A matrix needs V² = 2.5 billion cells. BFS also iterates neighbors, which costs O(deg(u)) in a list but O(V) in a matrix.
Exercise
Your program must answer ten million "is there an edge between u and v?" queries on a graph with 800 vertices. Which representation fits, and why?
Show answer
An adjacency matrix. It has 640,000 cells, which is small, and each lookup is O(1). An adjacency list with set neighbors would also give O(1) average lookups, but the matrix is simpler and avoids hashing.