An adjacency list is a data structure commonly used in graph theory.
How to build an adjacency list — template, and when to pick a list over a matrix
An adjacency list does roughly what an adjacency matrix does: it stores every edge of a graph. The difference is that a matrix wastes memory — with small data an adjacency matrix is still workable,
but once the data grows, a matrix usually runs out of time or memory, and that is when you switch to an adjacency list. To make the two representations easier to compare, let’s fix a small data set:
1 10 3 5 8 9
We mark the given points with 1 and leave the rest 0. (1) Adjacency matrix (fine for small, dense graphs)
| / | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
As you can see, three points force us to allocate a 10x10 two-dimensional array — a large waste of space.
Walking such a graph with big data can also run out of time. So what about an adjacency list?
(2) Adjacency list (fine for large, sparse graphs)
Still the same three points:
V1————>V10
V3————>V5
V8————>V9
One chain per vertex is all it takes, which saves a lot of memory. The picture below shows the detail:

Here is a general template for building an adjacency list:
| |
Reference: How to build an adjacency list — template, and when to pick a list over a matrix