Interactive explainer: adjacency matrix vs adjacency list — click graph nodes to see both representations update live

Click any node — watch both representations update live
Click a node to highlight its row/column in the matrix and its neighbors in the list.
Adjacency matrix
A[i][j] = 1 if edge exists, else 0
Adjacency list
Each node stores its neighbors

Try different graph types
Space used — matrix vs list

Python implementation

When to use which?
Adjacency matrix
Fast: check if edge (u,v) exists — O(1)
Fast: add or remove an edge — O(1)
Slow: find all neighbors — O(V)
Wastes space: O(V²) always, even if few edges
Best for: dense graphs, small V, quick edge lookup
Adjacency list
Space-efficient: O(V + E) only
Fast: iterate neighbors — O(degree)
Slower: check if edge exists — O(degree)
Harder: to remove specific edge
Best for: sparse graphs, large V, traversals