An adjacency list is a graph representation in which each vertex stores a collection of its neighbors, and optionally the weight or label of each connecting edge. Among data structures in programming, it is the standard way to represent sparse graphs, because its memory use grows with O(V + E), the number of vertices plus the number of edges, rather than with the square of the vertex count.
Most graph algorithms taught and used in practice, including breadth-first search, depth-first search, Dijkstra’s shortest path and topological sorting, are described and analyzed assuming an adjacency list. The structure trades fast neighbor iteration for slower edge lookup compared with an adjacency matrix.
How an adjacency list stores a graph
The structure has two levels. The outer container is indexed by vertex, usually an array when vertices are numbered 0 to V minus 1, or a hash table when vertices are identified by names or arbitrary keys. Each entry holds an inner collection of neighbors, traditionally a linked list and today more often a dynamic array.
In a directed graph, an edge from u to v appears only in the list of u, so the lists contain E entries in total. In an undirected graph, each edge is stored twice, once in each endpoint’s list, giving 2E entries. For a weighted graph, each entry is a pair such as (neighbor, weight) or a small edge record.
The table below shows the main implementation variants and where each fits.
| Variant | Outer container | Inner container | Best suited for |
|---|---|---|---|
| Array of dynamic arrays | Array | Dynamic array | General use, good cache behavior |
| Array of linked lists | Array | Singly linked list | Frequent insertions, classic textbook form |
| Hash map of sets | Hash table | Hash set | Named vertices, fast edge existence checks |
| Compressed sparse row | Offset array | One flat neighbor array | Large static graphs, high performance |
Time and space complexity of common operations
The performance of an adjacency list depends on the degree of a vertex, written deg(v), which is the number of its neighbors. Operations that touch one vertex cost time proportional to its degree, which is small in sparse graphs.
| Operation | Typical cost | Comment |
|---|---|---|
| Add a vertex | O(1) amortized | Append a new empty list |
| Add an edge | O(1) amortized | Append to one list, or two if undirected |
| Check whether edge u-v exists | O(deg(u)) | Linear scan unless lists are sorted or hashed |
| Iterate over neighbors of v | O(deg(v)) | The operation graph traversals rely on |
| Remove an edge | O(deg(u)) | Find and delete the entry |
| Total memory | O(V + E) | Plus pointer or header overhead per list |
Because a full traversal visits every vertex once and every list entry once, breadth-first and depth-first search run in O(V + E) on an adjacency list. With a matrix the same traversal costs O(V squared), since every row must be scanned regardless of how many edges exist.
Adjacency list compared with adjacency matrix and edge list
The choice of representation is driven mainly by graph density, the ratio of actual edges to the maximum possible V(V minus 1). Road networks, social graphs and dependency graphs are typically sparse, while some similarity or distance graphs are dense.
| Property | Adjacency list | Adjacency matrix | Edge list |
|---|---|---|---|
| Memory | O(V + E) | O(V squared) | O(E) |
| Edge existence test | O(deg(u)) | O(1) | O(E) |
| Neighbor iteration | O(deg(v)) | O(V) | O(E) |
| Best density | Sparse graphs | Dense graphs | Edge-centric algorithms |
| Typical algorithms | BFS, DFS, Dijkstra | Floyd-Warshall, matrix methods | Kruskal’s algorithm |
As a rule of thumb, an adjacency list is preferable when E is far below V squared. For a graph of one million vertices with ten edges each, a list needs roughly ten million entries, while a bit matrix would need about 125 gigabytes, which is impractical.
Practical design choices
Real implementations adjust the basic structure to the workload. Sorting each neighbor list allows binary search for edge tests in O(log deg(u)) and speeds up set intersections used in triangle counting. Replacing lists with hash sets gives expected O(1) edge tests at the cost of more memory and worse cache locality.
- Directed graphs with reverse queries: keep both an outgoing and an incoming list per vertex, doubling memory but making predecessor lookups cheap.
- Static graphs: convert to compressed sparse row (CSR) format, which stores all neighbors in one contiguous array and an offset array of length V + 1.
- Multigraphs: allow repeated neighbor entries or store an edge count per pair.
- Edge attributes: store edge identifiers in the lists and keep attributes in a separate array to avoid duplication in undirected graphs.
Pitfalls and misconceptions
Errors with adjacency lists are usually about consistency and hidden costs, not about the idea itself. The table lists the most frequent problems.
| Mistake | Consequence | Remedy |
|---|---|---|
| Adding only one direction in an undirected graph | Traversals miss vertices | Insert both u-v and v-u, or wrap insertion in one function |
| Assuming O(1) edge lookup | Quadratic slowdowns in dense regions | Use sorted lists, hash sets or a matrix for hot queries |
| Removing a vertex without cleaning neighbors | Dangling references | Delete the vertex from every neighbor’s list |
| Using linked lists by default | Poor cache performance | Prefer dynamic arrays or CSR |
A common misconception is that adjacency lists are always more memory-efficient. For very dense graphs, per-entry overhead such as pointers and object headers can make a list larger than a compact bit matrix.
Explained without jargon
An adjacency list is a way for a computer to remember who is connected to whom. Picture a contact book where each person’s page lists only their friends. To find someone’s friends, you open their page and read the short list.
This saves considerable space when most people know only a few others. The trade-off is that checking whether two specific people are connected means scanning one of those lists instead of looking up a single box in a giant grid.
A typical use case
A logistics company plans delivery routes over a regional road network with about 200,000 intersections and 500,000 road segments. Each intersection connects to only two to five others, so the graph is extremely sparse.
The routing service stores the network as an adjacency list in compressed sparse row format, with each entry holding the neighboring intersection and the travel time in seconds. Dijkstra’s algorithm with a binary heap then computes shortest routes by iterating only over the few roads leaving each intersection.
The whole graph fits in a few dozen megabytes of memory, and typical route queries finish in milliseconds. An adjacency matrix for the same network would have required tens of billions of cells, most of them empty.