Mathwords logoMathwords

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 G=(V,E)G = (V, E), an adjacency list is the collection {Adj⁡(v):v∈V}\{\operatorname{Adj}(v) : v \in V\}. For a directed graph, Adj⁡(v)={u:(v,u)∈E}\operatorname{Adj}(v) = \{u : (v, u) \in E\}. For an undirected graph, Adj⁡(v)={u:{v,u}∈E}\operatorname{Adj}(v) = \{u : \{v, u\} \in E\}. In a multigraph or pseudograph, Adj⁡(v)\operatorname{Adj}(v) is a multiset, so repeated edges and loops appear as repeated entries. The structure uses Θ(∣V∣+∣E∣)\Theta(|V| + |E|) storage. An adjacency matrix always uses ∣V∣2|V|^2 entries.

Key Formula

∑v∈V∣Adj⁡(v)∣={∣E∣directed graph2∣E∣undirected graph\sum_{v \in V} |\operatorname{Adj}(v)| = \begin{cases} |E| & \text{directed graph} \\ 2|E| & \text{undirected graph} \end{cases}
Where:
  • Adj⁡(v)\operatorname{Adj}(v) = the list of vertices adjacent to (or, if directed, pointed to by) vertex v
  • ∣Adj⁡(v)∣|\operatorname{Adj}(v)| = length of that list: the out-degree (directed) or degree (undirected) of v
  • ∣E∣|E| = number of edges in the graph
  • VV = 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.
A:B, CB:DC:DD:EE:∅\begin{aligned} A&: B,\ C \\ B&: D \\ C&: D \\ D&: E \\ E&: \varnothing \end{aligned}
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.
2+1+1+1+0=5=∣E∣2 + 1 + 1 + 1 + 0 = 5 = |E|
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.
list: 5+5=10matrix: 52=25 entries, 20 of them 0\text{list: } 5 + 5 = 10 \qquad \text{matrix: } 5^2 = 25 \text{ entries, } 20 \text{ of them } 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 O(∣V∣+∣E∣)O(|V| + |E|) time on an adjacency list, compared with O(∣V∣2)O(|V|^2) 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|.

Related Terms