Intermediate Topic 28 of 52 Data Structures
Adjacency List
Graph storage keeping each vertex's neighbors in a list.
How it works
An adjacency list gives each vertex a list of its neighbors, using O(V + E) memory. Iterating over a vertex's neighbors costs only its degree, which makes traversals such as BFS and DFS run in O(V + E). Checking whether a specific edge exists takes O(degree) unless neighbors are stored in a hash set. Weighted graphs store pairs of neighbor and weight.
Step-by-step walkthrough
- Each vertex stores a list of its neighbors.
- A lists B and C.
- A also lists C.
- C lists D, so edges are stored only where they exist.
- Memory is O(V + E), ideal for sparse graphs.
Where it is used
Sparse graphs such as road networks, social graphs, dependency graphs and web link graphs are stored as adjacency lists. Most graph libraries and competitive-programming solutions use this form.
Common pitfall
For undirected graphs each edge must be added in both lists, and forgetting one direction is a classic bug. Vertex numbering must match list indexes. Dense graphs waste time scanning long lists; an adjacency matrix may be better there. Avoid duplicate entries.
Where it fits in the course
Adjacency List is topic 28 of 52 in Data Structures, rated Intermediate. It follows Graphs. The next topic is Adjacency Matrix.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Adjacency List?
Where do people use Adjacency List?
What is the most common mistake with Adjacency List?
What should I learn before this topic?
How can I practice it?
Is Adjacency List hard to learn?
How long does it take to learn Adjacency List?
Which topics are related to Adjacency List?
What should I study after Adjacency List?
Related topics
- Graphs (Data Structures)
- Adjacency Matrix (Data Structures)
- Fenwick Trees (Data Structures)
- Disjoint Set Union (Data Structures)
- List Comprehensions (Programming Fundamentals)