Graph Depth First Search Cheat Sheet
Follow one path in a graph as far as it goes, backtracking to try the next branch once you hit a dead end.

Earn the full sheet below.
When to reach for it
- Validates a connection, or counts connected groups.
- A grid is a graph too: cells as nodes, edges between them.
- Order doesn't matter here, just that a path exists.
What's on the sheet
The recursion is boilerplate. Step 3 shapes the graph.
- Visited Check. Mark a node visited on arrival, before recursing.
- Recurse. A hit bubbles True up; a dead end bubbles False.
- Graph Setup. Where problems differ: directed or undirected.
Watch out
- visited.add(curr) has to run before you recurse, or two nodes pointing at each other loop forever.
- Directed vs undirected is a judgment call: is there a dependency between nodes, like Course Schedule?
How to earn it
Solve half of the Graph Depth First Search problems on InterviewCrunch and the sheet is yours. Free accounts included. Members can download every sheet right away.
Want to see a finished sheet first? Preview the Two Pointers sample.
Every pattern has a sheet. See the full set.