All patterns
Graph Representation
Graphs
Intermediate
Interactive
Build adjacency lists from node-to-node relationships.
Time
O(V + E) build
Space
O(V + E)
ActiveComparingMatch / bestIn windowFrontierVisitedExcludedAnswer
No frames to display.
The idea
Graphs model arbitrary relationships with vertices and edges. An adjacency list stores each vertex's neighbors, providing the representation used by BFS, DFS, shortest paths, and connectivity algorithms.
Analogy
A reusable way to think about organizing and navigating structured data efficiently.
When to reach for it
- The relationships between values drive the algorithm.
- You need efficient traversal, lookup, or updates.
- An interview problem names or implies this structure.
Language-independent template
adj = map(node -> [])
for (u, v) in edges:
adj[u].append(v)
adj[v].append(u)Complexity — time O(V + E) build, space O(V + E).
Common mistakes
- Losing a pointer before saving its next target.
- Breaking the structure invariant during an update.
- Forgetting empty and single-node cases.
Practice progression
Canonical interview problems, ordered from foundation to advanced application.
Easy
Find Center of Star GraphLC 1791
Return the node incident to every edge in a star.
Medium
Clone GraphLC 133
Create a deep copy of every node and edge in a connected graph.
Hard
Find every edge whose removal disconnects the graph.
Variations
Traversal
Insertion and deletion
Search and reconstruction