LearnThatStack Ace your next interview
Graphs · question
Question 5 of 60

How do an adjacency list and an adjacency matrix store a graph?

beginner
← All Graphs questions
Re-explain
Visual ↓

An adjacency list keeps, for each vertex, a list of its neighbors. An adjacency matrix keeps a grid with one row and one column per vertex. The cell at row i, column j marks whether an edge joins those two vertices.

list:   A -> [B, C]
matrix: A row = [0, 1, 1, 0]

These two layouts dominate, and they trade memory for lookup speed. A list uses space proportional to O(V + E), which is tiny when edges are few. A matrix always uses O(V^2), even for a nearly empty graph.

Lookup speed favors the matrix. Checking whether a specific edge exists is O(1) in a matrix but needs a scan of the list.

Most real graphs are sparse, so adjacency lists are the default choice. Use the matrix only when the graph is small or dense, or when you test single edges constantly.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Graphs One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
One graph as an adjacency list and an adjacency matrix
Graph4 vertices, 3 edges
ABCD
Adjacency listone list per vertex
A[B, C]
Adjacency matrix1 = edge, 0 = no edge
ABCDABCD
C's neighbor listis D a neighbor of C?
AD
1step

An adjacency list keeps each vertex's neighbors, the vertices one edge away. Vertex A is in focus first. Its neighbors are B and C.

1 / 7

The matrix checks an edge in one read but stores a cell for every pair of vertices. The list stores only real edges but must scan neighbors to check one.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • When would you choose the matrix despite its O(V^2) memory? Name small or dense graphs and frequent single-edge checks, and compare V^2 cells to V + E entries for a real-sized sparse graph.
  • How does the representation change what BFS or DFS costs? With a list each neighbor list is read once, so O(V + E). With a matrix every row is scanned in full, so O(V^2).
  • How would you store edge weights and directed edges in each layout? List entries become neighbor-weight pairs. A matrix cell holds the weight, with a no-edge marker that isn't a valid weight. A directed matrix isn't symmetric.
  • Can you get fast edge checks without paying for a full matrix? Keep each vertex's neighbors in a hash set for O(1) average lookup, or sort the lists and binary search. Note the extra memory or upkeep.
  • Why can plain adjacency lists be slow on huge graphs, and what layout helps? Separate per-vertex lists scatter across memory, so traversal chases pointers. CSR packs all neighbors into one array indexed by offsets.

What you can say

  1. Both record which vertices connect to which. A list gives each vertex its own list of neighbors, and a matrix is a V by V grid where each cell says whether an edge exists.
  2. So if A connects to B and C, the list stores A pointing to B and C, and A's row in the matrix has a 1 in the B and C columns.
  3. The real difference is memory. A list takes O(V + E), so a graph with few edges stays small, while a matrix takes O(V^2) no matter how few edges there are.
  4. The matrix wins on lookup. Checking one specific edge is a single cell read, O(1), while a list has to scan that vertex's neighbors.
  5. Most real graphs are sparse, so I default to a list and only use a matrix when the graph is small or dense, or when I'm testing single edges all the time.

Weak answers to avoid

  • Describes the layouts but skips the costs The question is really about the trade-off. Give O(V + E) against O(V^2) space, and O(1) against a list scan for checking one edge.
  • Says the matrix is always faster Only single-edge checks are faster. Listing a vertex's neighbors scans a whole row, so BFS on a matrix costs O(V^2) instead of O(V + E).
  • Picks the matrix by default Most real graphs are sparse, so most of the V^2 cells are zeros. Default to a list and say what makes a matrix worth it: small, dense, or constant edge checks.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Graphs cheatsheet.

← Back to all Graphs questions
Pro · $10/mo

51 of 60 Graphs answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime