INTERMEDIATE · 10 MIN · FREE LESSON
Depth-first search
Explore depth-first search through a visual, step-by-step model.
Loading interactive lesson…
INTERMEDIATE · 10 MIN · FREE LESSON
Explore depth-first search through a visual, step-by-step model.
Loading interactive lesson…
Enter {"n":5,"edges":[[0,1,2],[1,2,3],[0,3,1],[3,4,2]],"directed":false,"start":0,"target":4}. Vertices are 0 through n−1.
Mark the start when adding it to the frontier.
Mark each reached vertex once and inspect its outgoing edges. BFS uses FIFO order and gives unweighted shortest-hop distance; DFS follows one branch with a recursive call stack.
Measured reads + comparisons, seed 42. Plotted sample sizes respect each lesson’s explicit input limits.
Keyboard: Space play/pause · ←/→ step · R restart · 1–5 speed. Reduced motion disables transitions.
Explore a corridor fully before returning to its junction.
Finish a branch before returning. Edit the graph JSON to add a cycle, isolate a vertex or change an edge weight. Nodes are numbered from zero.
Predict the next frontier decision or edge operation before revealing it. Compare a connected graph with the same graph after removing an important edge.
O(V+E) time and O(V) traversal workspace. Each vertex is entered once and each outgoing edge is considered once. Recursive activations retain the current branch until it is fully explored. The counters count teaching operations; animation duration is not a CPU benchmark.
The four references show the core operation. Adjacency-list inputs use zero-based vertex indices. Weighted references use (neighbor,weight) pairs or explicit edge triples. Validate the graph assumptions before calling standalone code.
Use for reachability, finish orders and structural graph analysis.
DFS discovery depth is not generally a shortest-path distance.
Check the graph assumptions, invariant and boundary conditions below.
MAKE IT STICK
Progress saves on this device and merges with your account after sign-in.
0 / 5000 · Saved on this device; synced when signed in.