Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Depth-First Search

Depth-first search (DFS) is a graph traversal algorithm that follows one branch as far as it goes before backing up to try the next, using a stack (often the recursion call stack) to remember the way back. It visits every reachable vertex and edge once in O(V + E) time, and underpins cycle detection, topological sort, and connectivity.

Picture walking a maze with a ball of string. At every fork you follow one passage, paying out string behind you; when you hit a dead end, you rewind the string back to the last fork and try the next untried passage. The string is the call stack, and rewinding it is backtracking, the second half of DFS.

How Depth-First Search Works

Recursive DFS consists of three actions: mark and record the current node; for each unvisited neighbour, recurse into it; when the neighbour loop finishes, return. Every descent is paired with a return to the caller, and the deepest frame on the call stack is always the node currently being explored.

This lesson runs in two modes, and the animated code updates to match. In Tree mode, there is one path to every node so a revisit cannot occur; the displayed tree code carries no visited set and only appends, recurses, and returns. In Graph mode, edges can reconverge or form cycles, so visited guards the recursion: each call marks its node on entry, and the neighbour loop recurses only into unmarked nodes. An edge pointing to an already-visited node is classified rather than followed: if the target is still on the call stack (an ancestor), it is a back edge, indicating a cycle; if the target has already finished, it is a forward edge when discovered inside the current call's subtree (a descendant) and a cross edge otherwise. DFS skips all three.

Depth-First Search Step by Step

The default configuration is Graph mode with the Cyclic preset starting at node 0, a 6-node directed graph containing cycles: 0 → [1, 3], 1 → [2], 2 → [0, 4], 3 → [4, 5], 4 → [5], 5 → [1]. With cycles present, the visited guard is what keeps the search finite.

  1. dfs(0). Mark 0 (#1). Stack: [0]. First neighbour 1 is unvisited, so recurse.
  2. dfs(1). Mark 1 (#2). Stack: [0, 1]. Recurse into 2.
  3. dfs(2). Mark 2 (#3). Stack: [0, 1, 2]. First neighbour is 2 → 0, but 0 is visited and still on the stack: a back edge, indicating a cycle. Skip it (following it would make dfs(0) recurse forever). Next neighbour 4 is unvisited, so recurse.
  4. dfs(4). Mark 4 (#4). Stack: [0, 1, 2, 4]. Recurse into 5.
  5. dfs(5). Mark 5 (#5). Stack: [0, 1, 2, 4, 5], the deepest point of the search. Its only neighbour is 5 → 1, and 1 is on the stack: another back edge, skipped. Node 5 finishes and returns, unwinding the stack 5 → 4 → 2 → 1 back up to node 0.
  6. dfs(3). Back in 0's loop, its second neighbour 3 is unvisited. Mark 3 (#6). Stack: [0, 3]. Edges 3 → 4 and 3 → 5 both point to nodes already finished and off the stack (cross edges), so both are skipped. Node 3 finishes and returns to 0; 0 finishes and the search ends.

Visit order: 0, 1, 2, 4, 5, 3. Four edges were skipped, for two distinct reasons. 2 → 0 and 5 → 1 are back edges pointing to an ancestor still on the call stack; following either would loop forever. 3 → 4 and 3 → 5 are cross edges pointing to nodes whose work has already finished; they are redundant rather than cyclic. Distinguishing still on the stack from already finished is how DFS detects cycles, which an acyclic tree cannot show.

The other presets isolate individual behaviors. Tree mode (Balanced or Skewed) removes cycles: with one path to every node, no visited check runs, every edge is a tree edge, and only depth-first order remains. On the balanced 7-node tree (0 → [1, 2], 1 → [3, 4], 2 → [5, 6]), DFS visits 0, 1, 3, 4, 2, 5, 6, finishing node 1's entire subtree before touching sibling 2 (contrasting with BFS's level-by-level 0, 1, 2, 3, 4, 5, 6). The graph Simple preset keeps a single cross edge and no back edge, showing the visited guard without cycles. Use those presets to separate traversal order from cycle handling; the default shows both together.

On the canvas, the focus ring slides down an edge during descent and back up on return, while the call-stack panel on the right grows and shrinks frame by frame in sync. Nodes are faint blue until reached, orange while on the stack (the current call at full strength, waiting ancestors dimmed), and green once finished; a skipped back, forward, or cross edge flashes red when rejected. Nodes turn green as the stack unwinds.

Time and Space Complexity

CaseTimeSpace
AllO(V + E)O(V)

Each vertex is entered once (enforced by the visited mark) and each edge is examined once from its tail. Space covers the call stack plus, in graph mode, the visited set. The stack holds one frame per node on the current path from the start, reaching O(V) on a single long chain (the shape where DFS uses maximum stack space while BFS's queue stays small). The visited set stores one mark per reached node, O(V) on any graph. On the stack side, DFS and BFS hit their O(V) worst case on opposite graph shapes.

Edge Cases

  • Single node, no edges. dfs(start) marks the node, finds no neighbours, and returns immediately.
  • Unreachable nodes. DFS visits only nodes reachable from the start along directed edges (or the start's connected component in an undirected graph); remaining nodes stay unvisited. Covering the full graph requires restarting from each still-unvisited node.
  • Cycles. The visited check turns a cycle's closing edge into a skipped back edge so recursion terminates.
  • Deep, chain-like input. The call stack grows as tall as the path. In production code, recursive DFS can overflow the runtime stack on deep chains, so an explicit stack replaces recursion.

Common Mistakes

  1. Dropping the visited check on a graph. Any cycle causes infinite recursion. Only a tree allows the guard-free version shown in Tree mode.
  2. Marking visited too late. Mark the node on entry to dfs(node) before iterating over neighbours; marking after the loop lets a cycle re-enter the node while its call is still active.
  3. Forgetting that backtracking is half the algorithm. DFS consists of descent and return; algorithms built on DFS (topological sort, finish times, articulation points) perform key work on the way back up.
  4. Assuming one "correct" order. Visit order depends on adjacency-list order: 0 → [1, 2] and 0 → [2, 1] produce different, equally valid DFS traversals.
  5. Confusing DFS with BFS. DFS uses a stack and BFS uses a queue; swapping the container turns depth-first traversal into breadth-first traversal.