Skip to interview prep
Apexed

28. Adjacency matrix vs adjacency list: which should you use?

EasyGraph algorithms

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

An adjacency matrix uses O(V²) space and answers "is there an edge u→v?" in O(1); it suits dense graphs. An adjacency list uses O(V + E) space and iterates neighbours efficiently; it suits sparse graphs, which most real graphs are. BFS and DFS cost O(V + E) with lists but O(V²) with a matrix.

Example program

Adjacency matrix vs adjacency list: which should you use — Data structures & algorithms interview question — Apexed