BFS, DFS, topological sort, shortest-path, union-find. Graph problems show up disguised as ‘connectivity’, ‘dependency’, ‘maze’, ‘islands’. Recognize the graph; the algorithm is the easy part.
Level-order traversal, queue-based. Used for: shortest path in maze, word ladder, graph distance, level-order traversals on trees.
Stack-based or recursive. Used for: connectivity, cycle detection, topological sort, path-finding, flood fill, island counting.
DAGs only. Two ways: DFS-based (post-order reverse) or Kahn’s algorithm (in-degree). Used for: course scheduling, build systems, task ordering.
Dijkstra for non-negative weights, Bellman-Ford for negative weights, Floyd-Warshall for all-pairs. Knowing when to use which matters more than memorizing implementations.
Disjoint Set Union. Used for: Kruskal’s MST, dynamic connectivity, redundant connection detection, account merging.
Most graph problems aren’t explicitly graphs. Islands, mazes, dependency chains, word transformations — all graphs. Recognition first; algorithm second.
Most graph interview questions don’t say ‘graph’. They say ‘islands’ or ‘course schedule’. Here’s how to spot graphs fast.
| Use case | BFS | DFS |
|---|---|---|
| Shortest path in unweighted graph | ✅ Use this | ❌ Don’t |
| All connected components | ✅ Works | ✅ Often easier (recursion) |
| Detect cycle in undirected graph | ✅ Works | ✅ Cleaner |
| Topological sort | ❌ Use Kahn’s (different from BFS) | ✅ Post-order DFS |
| Path with state (visited set) | ✅ Layer-by-layer | ✅ Backtrack natural |
| Island counting | ✅ Works | ✅ Most common choice |
Union-find (Disjoint Set Union) is the underrated graph algorithm. Three operations: find(x), union(x, y), is-same-set(x, y). With path compression and union by rank, all operations are effectively O(1) (technically O(α(n))) amortized.
Full deep-dive on graph algorithms: Graph algorithms — complete pattern guide and graph interview questions guide.
Most graph problems aren't labeled 'graph'. Watch for these cues: grid + adjacency (islands, mazes), dependency chains (course schedule), connectivity questions (friend circles), state transformations (word ladder), or any 'shortest/longest path' question. The graph is usually implicit.
Adjacency lists for sparse graphs (most interview problems). Adjacency matrices for dense graphs or when you need O(1) edge existence checks. Most FAANG problems use sparse graphs, so default to adjacency lists. For grid problems, the grid itself is the graph — no separate representation needed.
BFS for shortest path in unweighted graphs (only BFS guarantees minimum distance per level). DFS for everything else — connectivity, cycle detection, topological sort, path-finding with state. Default to DFS unless you need shortest path.
Important enough to know cold. Comes up in ~10-15% of graph interview rounds at FAANG. You need the priority-queue implementation memorized, plus understanding of when it fails (negative weights → use Bellman-Ford instead).
Yes. PhantomCode's coach grades your graph recognition speed — how fast you identify the underlying graph from a problem statement. The drills also cover the algorithm selection step (BFS vs DFS vs Dijkstra vs union-find), which is where many candidates choose wrong.
Voice-paced practice with graph-recognition coaching. Same coach that runs in your real coding interview.
Start graph drillsDownload now — invisible, undetectable, and works on every platform. Plans start at $19.