Graphs are the pattern chapter where interviewers stop asking “can you code” and start asking “can you model.” Almost nothing about a graph problem is handed to you in a convenient shape — you’re usually given a list of pairs, a grid of characters, or a set of dependency rules, and the first real skill being tested is recognizing that what you’re looking at is a graph, then choosing a representation that makes the rest of the problem easy.
Graphs are the pattern chapter where interviewers stop asking “can you code” and start asking “can you model.” Almost nothing about a graph problem is handed to you in a convenient shape — you’re usually given a list of pairs, a grid of characters, or a set of dependency rules, and the first real skill being tested is recognizing that what you’re looking at is a graph, then choosing a representation that makes the rest of the problem easy. Once you’ve done that, the traversal itself is often the easy part.
Reach for the techniques in this article when you notice a handful of recurring signals. A problem describes entities with relationships between them — accounts that share identifiers, cities connected by roads, tasks that depend on other tasks — that’s graph language, whether or not the word “graph” ever appears. A grid where you can move between neighboring cells and you care about connectivity or regions is a graph in disguise, with cells as nodes and adjacency as edges. Prerequisite phrasing — “you must finish A before B” — is close to a giveaway for topological sort. Questions about whether elements belong to the same group, whether adding a connection creates a cycle, or whether two elements are “connected” in some abstract sense point at Union-Find. And when a problem asks about the structural integrity of a network — single points of failure, critical connections, articulation points — you’re in DFS low-link territory.
These techniques do not apply when the relationships are strictly hierarchical and single-parent, like a filesystem or a binary tree with no cross-links — that’s simpler tree recursion, covered in the companion book. They also don’t apply when what you actually need is the shortest weighted path or the cheapest way to connect everything — those are related but distinct problems that get their own chapter next, because they usually require a heap rather than plain BFS/DFS to get the ordering right.
this article covers three toolkits: general traversal (DFS/BFS) for reachability and structure questions, topological sort for ordering under dependency constraints, and Union-Find (disjoint set) for online connectivity questions. All three show up constantly, often in combination, and mastering when to pull out which one is most of the battle.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithmic Edge.
Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
