Adjacency List — Definition, Formula & Example
An adjacency list is a way to store a graph by giving each vertex its own list of the vertices it connects to. Unlike an adjacency matrix, it records only the edges that actually exist. It never stores a 0 for a missing edge.
For a graph , an adjacency list is the collection . For a directed graph, . For an undirected graph, . In a multigraph or pseudograph, is a multiset, so repeated edges and loops appear as repeated entries. The structure uses storage. An adjacency matrix always uses entries.
Key Formula
Where:
- = the list of vertices adjacent to (or, if directed, pointed to by) vertex v
- = length of that list: the out-degree (directed) or degree (undirected) of v
- = number of edges in the graph
- = the set of vertices
Worked Example
Problem: A directed graph has vertices A, B, C, D, E and edges A→B, A→C, B→D, C→D, D→E. Build its adjacency list. Then compare its size with the 5 × 5 adjacency matrix.
Step 1: Create one list per vertex. For each edge u→v, append v to the list of u. Vertex E has no outgoing edges, so its list is empty.
Step 2: Check the total. In a directed graph, the list lengths must add up to the number of edges. You can read each out-degree directly, such as 2 for A. An in-degree takes more work. You find it by scanning every list. D appears in the lists of B and C, so its in-degree is 2.
Step 3: Compare storage. The list holds 5 vertex headers plus 5 entries. The matrix holds every possible pair, and most of those entries are 0.
Answer: The adjacency list is A: B, C; B: D; C: D; D: E; E: (empty). It stores 10 items, compared with the matrix's 25 entries, 20 of which are zeros.
Why It Matters
In a data structures or algorithms course, breadth-first search and depth-first search run in time on an adjacency list, compared with on a matrix. Road maps and social networks are sparse: each vertex has far fewer neighbors than the total number of vertices. For graphs like these, lists are the default choice. A matrix is better when you need to answer "is there an edge from u to v?" in constant time, or when the graph is dense.
Common Mistakes
Mistake: Recording each edge of an undirected graph in only one vertex's list.
Correction: An undirected edge {u, v} belongs in both lists: put v in Adj(u) and u in Adj(v). As a check, the list lengths should total 2|E|, not |E|.
