Chapter 1, lesson 4 of 5
From Drawing to Data
Three ways to store a graph, what each is good at, and how to choose.
Written lesson
6 min read
Two programs run the same search on the same graph and return the same answer. One finishes almost instantly; the other takes about twenty thousand times longer. The graph has a million vertices and fifty million edges. The fast program needs about 51 million steps, the slow one about a trillion.
The code of the search is identical in both. The only difference is how the graph is stored. This lesson covers the three standard ways to store a graph, what each one is good at, and how to choose.
The edge list
So far every graph has lived in a drawing. A computer needs it as data. Here is the graph for this lesson: six vertices numbered 0 to 5, and seven edges.
The simplest way to store it is an edge list: write down the edges, one pair per line.
0 1
0 2
1 2
1 3
2 4
3 4
3 5
It is compact, and it is how graph files are usually saved and shared. Its weakness shows as soon as you ask a local question. Who are the neighbours of vertex 3? The answer (1, 4 and 5) is scattered through the list, so you have to read every line to find it.
The adjacency matrix
The second way is the adjacency matrix: a grid with one row and one column per vertex. Put a 1 where two vertices are joined and a 0 everywhere else.
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 0 | 0 |
| 2 | 1 | 1 | 0 | 0 | 1 | 0 |
| 3 | 0 | 1 | 0 | 0 | 1 | 1 |
| 4 | 0 | 0 | 1 | 1 | 0 | 0 |
| 5 | 0 | 0 | 0 | 1 | 0 | 0 |
The adjacency matrix: one cell per pair of vertices.
Three things to notice:
- Each edge fills two cells, because the grid is symmetric: row 1, column 3 and row 3, column 1. Seven edges give fourteen ones in thirty-six cells.
- Each row adds up to the degree of its vertex. Row 3 has three ones, and vertex 3 has degree 3. Adding all the rows gives 14, twice the number of edges: the handshake lemma, read off a table.
- The matrix answers one question instantly: are these two vertices joined? For 1 and 4, look at a single cell. It is 0, so no.
Its weakness is size. With n vertices the grid always has n² cells, no matter how few edges there are, and listing the neighbours of a vertex means scanning a whole row of n cells.
The adjacency list
The third way is the adjacency list: every vertex keeps a short list of its own neighbours.
0: 1 2
1: 0 2 3
2: 0 1 4
3: 1 4 5
4: 2 3
5: 3
The adjacency list: each vertex stores only its own neighbours.
Each edge appears twice, once at each end, so the lists hold fourteen entries in total. Listing the neighbours of a vertex takes exactly as long as it has neighbours: vertex 5 answers in one step. And the whole structure has size proportional to n + m (vertices plus edges), not n².
Sparse and dense
Which one should you use? That depends on how many edges the graph has compared with how many it could have. A simple graph on 6 vertices can have at most 6 × 5 / 2 = 15 edges; ours has 7 of them, which is fairly dense.
Real networks are nothing like that. Picture a social network with a million people, each with about a hundred friends:
| Storage | Size | At a byte per cell / four bytes per entry |
|---|---|---|
| Adjacency matrix | 10¹² cells | about 1 terabyte |
| Adjacency list | 10⁸ entries | about 400 megabytes |
A million people with a hundred friends each: a terabyte as a matrix, 400 MB as lists.
Almost every real graph looks like this: huge and sparse. Each vertex connects to a tiny fraction of the others, so almost every cell of the matrix would be a zero. This is also where the two programs from the introduction come from. A search visits each vertex and looks at its neighbours. With lists that costs about n + m = 1 million + 50 million steps; with a matrix, finding the neighbours of each of the million vertices means scanning a row of a million cells, n² = a trillion steps.
Building it in Python
In Python an adjacency list is simply a dictionary that maps each vertex to a list of its neighbours. Start with an empty list for every vertex, then add each edge at both ends:
edges = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 4), (3, 4), (3, 5)]
adj = {v: [] for v in range(6)}
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
degree = {v: len(adj[v]) for v in adj}
print(sum(degree.values())) # 14
Building the adjacency list and checking the degrees in Python.
The degree of a vertex is just the length of its list, and the degrees add up to 14, twice the seven edges. The handshake lemma makes a good one-line test that you built the lists correctly.
Choosing the storage
Choose the storage by the question you will ask most often:
| Question | Best storage |
|---|---|
| Are these two vertices joined? | Adjacency matrix (one cell) |
| Who are the neighbours of this vertex? | Adjacency list (one short list) |
| Go through every edge once | Edge list (one pass) |
For most real graphs the adjacency list is the default. It is what makes the fast graph algorithms in the rest of this course possible: breadth-first search, depth-first search and topological sort all run in time proportional to n + m because they read adjacency lists.
Key ideas
- An edge list stores the pairs. An adjacency matrix stores a grid of ones and zeros. An adjacency list stores the neighbours of each vertex.
- The matrix answers "are u and v joined?" instantly but always needs n² cells. The list is compact (size n + m) and fast for exploring.
- Most real graphs are sparse, so the adjacency list is usually the right choice.
Check yourself
1. An undirected simple graph with 10 edges is stored as an adjacency matrix. How many ones does the matrix contain?
2. A graph has 1,000 vertices and 1,500 edges. Would you store it as a matrix or as lists?
3. In a directed graph, is the adjacency matrix still symmetric?
Practice on the platform
The graph representation guide compares every format in detail, with the cost of each operation. In the visualizer you can import a graph as text in any of these formats: open the Library, choose Import, type in the adjacency list above and watch it become a drawing. Six vertices, seven edges: the same graph, ready for any algorithm on the platform.
Up next