Playground / Depth-First Search

Go deep, then backtrack

Depth-First Search

Interactive lab

Try it: Depth-First Search

How depth-first search explores one path as far as it can before backtracking, using the call stack of recursive calls.

How it works

  1. Visit the start node and mark it.
  2. For each neighbour in order, if it is unvisited, recursively visit it — the current call waits.
  3. When a node has no unvisited neighbours, its call returns: backtrack.
  4. Nodes never reached are not connected to the start.

Default run (32 steps): Start DFS at A: follow one path as deep as it goes before backing up. … DFS finished. Visit order: A → B → C → F → E → D → G. Unreachable from A: H.

Simplified: Fixed 8-node undirected graph; you choose which edges exist and can drag nodes around (the layout never affects the algorithm).

Educational simulation

Loading the simulation…