Space of an adjacency list — Aptitude Question
Space of an adjacency list: A graph has V vertices and E edges. How much space does its adjacency list… Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Easy
- Time target: 60 seconds
Question
A graph has V vertices and E edges. How much space does its adjacency list representation take?
Options
- O(E)
- O(V)
- O(V + E)
- O(V²)
Show the answer and the worked solution
Correct answer: C. O(V + E)
Worked solution
- The representation keeps one list header per vertex, which costs O(V) even for isolated vertices.
- Every edge contributes one list node in a directed graph, or one in each endpoint's list in an undirected graph, which costs O(E).
- Adding the two gives O(V + E), which beats the O(V²) adjacency matrix whenever the graph is sparse.
Answer: O(V + E).
The idea
Adjacency lists win on sparse graphs and on iterating a vertex's neighbours; adjacency matrices win when you must answer 'is there an edge from u to v?' in constant time.
Shortcut. One header per vertex plus one node per edge end: isolated vertices still need their headers and dense graphs still need their nodes, so neither term can be dropped.
Common trap. "O(E)" counts only the list nodes and forgets the V headers, which dominate when the graph has few edges.
All Data Structures & Algorithms questions · placement mock tests