Graph Depth First Search Cheat Sheet

You follow one path as far as it goes. You back up and try the next branch when the path has no unvisited nodes left.

Graph Depth First Search cheat sheet preview

Unlock the full sheet below.

When to use it

  • You need to know if two nodes connect, or how many connected groups there are.
  • The input is a graph or a grid. In a grid, each cell is a node.
  • You only care whether a path exists. The order you visit nodes in doesn't matter.

What's on the sheet

The sheet has a code template you can reuse for every Graph Depth First Search problem. It's split into three steps so you can see which part changes from problem to problem. It also shows how to spot the pattern, how fast it runs, the mistakes people often make and the variations you'll see in interviews.

How to get it

You can unlock it with a free account by solving 2 of this pattern's problems.

Preview the Two Pointers sample to see a finished sheet first.