Skip to content

Concept module

Depth-First Search and Backtracking Paths

Keep the stack frontier, current depth, and branch order visible together so depth-first search feels like disciplined backtracking instead of random wandering.

The simulation shows one labeled graph with the current node, the stack frontier, and the visited nodes colored differently so the active branch stays readable. A readout card reports the traversal mode, current node, frontier size, visited count, and target, while a cue panel shows the frontier order and the current neighbor list. Depth-first search is running on the layered campus graph from A toward H. The start node is waiting on the frontier. The frontier currently holds 1 node, and 0 nodes have already been visited.

Interactive lab

Graph traversal bench

Keep one live graph, the current frontier, and the visited state visible together so breadth-first and depth-first search read like different process choices on the same structure.

Live graphDepth-first search on layered campusStack frontierAstartBCDEFGHtargetTraversal readoutLivemodeDepth-first searchgraphLayered campuscurrentreadyfrontier1visited0targetHTraversal readyThe stack frontier starts with the start node, then keeps the newest branch on top.Stack frontier keeps the next candidates visible while visited state prevents repeat work.Traversal cuesStack frontierAStart neighborhoodB, C

Controls

Layered campus
A
H
DFS

More tools

Secondary controls, alternate presets, and less-used toggles stay nearby without crowding the main bench.

Show

More presets

Presets

Time

0.00 s / 8.58 sLivePause to inspect a specific moment, then step or scrub through it.
0.00 s8.58 s

Predict -> manipulate -> observe

Keep the active prompt next to the controls so each change has an immediate visible consequence.

ObservationPrompt 1 of 2
Depth-first search often raises the current depth quickly because the newest branch stays on top of the frontier.

Graphs

Switch graph views without breaking the live stage and time link.

Current depth versus deepest claimed depth

Track how DFS raises the current depth quickly while it follows one branch before backtracking.

step: -0.69 to 9.27depth: -0.24 to 3.24
current depthdeepest claimed depth
Current depth versus deepest claimed depthTrack how DFS raises the current depth quickly while it follows one branch before backtracking.-0.691.84.296.789.27-0.240.631.52.373.24stepdepth
Hover or scrub to link the graph back to the stage.step / depth

Equation map

See each variable before you move it.

Select a symbol to highlight the matching control and the graph or overlay it most directly changes.

Graph layout
0

Switches among the bounded graph scenes on the same traversal bench.

Graph: Current depth versus deepest claimed depthGraph: New discoveries versus repeat skipsOverlay: Adjacency cueOverlay: Visited cue

Equations in play

Choose an equation to sync the active symbol, control highlight, and related graph mapping.

More tools

Detailed noticing prompts, guided overlays, and challenge tasks stay available without taking over the main bench.

Hide

What to notice

Keep the graph and one traversal graph visible together.

ObservationPrompt 1 of 2
Graph: Current depth versus deepest claimed depth
Depth-first search often raises the current depth quickly because the newest branch stays on top of the frontier.
Control: Traversal modeControl: Start nodeGraph: Current depth versus deepest claimed depthGraph: Visited nodes versus frontier sizeOverlay: Frontier cueOverlay: Visited cueEquationEquation

Guided overlays

Focus one overlay at a time to see what it represents and what to notice in the live motion.

3 visible

Overlay focus

Frontier cue

Show which claimed nodes are waiting next.

What to notice

  • The newest frontier node sits on top, so DFS dives into that branch first.

Why it matters

It turns a stack rule into something visible on the graph bench.

Control: Traversal modeControl: Start nodeGraph: Current depth versus deepest claimed depthGraph: Visited nodes versus frontier sizeEquationEquation
Depth-first search is running on the layered campus graph from A toward H. The start node is waiting on the frontier. The frontier currently holds 1 node, and 0 nodes have already been visited.
Equation detailsDeeper interpretation, notes, and worked variable context.

Branch depth rule

A newly discovered neighbor sits one edge deeper than the node that found it.

Graph layout 0 Start node 0 Target node 7

Stack frontier update

Depth-first search removes the newest frontier node, then places new discoveries on top of the frontier.

Start node 0 Traversal mode 1

Progress

Not startedMastery: NewLocal-first

Start exploring and Open Model Lab will keep this concept's progress on this browser first. No finished quick test, solved challenge, or completion mark is saved yet.

Let the live model runChange one real controlOpen What to notice

Try this setup

Jump to a named bench state or copy the one you are looking at now. Shared links reopen the same controls, graph, overlays, and compare context.

This bench opened from a setup link. Any new copy will reflect the state you see now.

Current bench

Layered campus DFS preset

This bench still matches one named preset, so the copied link will reopen that same starting point along with the current graph, overlays, and inspect context.

Open default bench

Saved setups

Premium keeps named exact-state study setups in your account while stable concept links stay public below.

Checking saved setup access.

This concept can keep using stable links while the saved-setups capability resolves for this browser.

Copy current setup

Stable concept and section links stay public below while exact-state setup sharing stays behind premium.

Stable links

Starter track

Step 5 of 60 / 6 complete

Algorithms and Search Foundations

Earlier steps still set up Depth-First Search and Backtracking Paths.

1. Sorting and Algorithmic Trade-offs2. Binary Search / Halving the Search Space3. Graph Representation and Adjacency Intuition4. Breadth-First Search and Layered Frontiers+2 more steps

Previous step: Breadth-First Search and Layered Frontiers.

Short explanation

What the system is doing

Depth-first search keeps the frontier organized like a stack. The newest claimed node is the next one to expand, so the search dives down one branch as far as it can before it has to return.

That stack behavior is easiest to trust when the graph, the frontier chips, and the depth graph all stay visible. The bench shows that DFS is not random wandering. It is a disciplined branch-first rule on the same graph that BFS uses.

Key ideas

01Depth-first search expands the newest waiting node first.
02DFS often raises the current depth quickly because it stays with one branch until it cannot continue.
03Backtracking happens when the top branch is exhausted and the frontier falls back to an older waiting node.

Worked example

Read the full frozen walkthrough.

Frozen walkthrough
Read the stack frontier directly from the layered-campus bench.

Live worked examples are available on Premium. You can still read the full frozen walkthrough on the free tier.

View plans
Frozen valuesUsing frozen parameters

On the layered campus graph, what changes when DFS starts from A?

Graph layout

Layered campus

Start node

A

Stack frontier

B below C

1. Read the first stack order

After A claims B and C, both nodes sit on the frontier, but the newest claim C is now on top.

2. Read the next branch move

Because DFS uses a stack frontier, C leaves next even though B was discovered first.

3. Name the branch-first consequence

DFS keeps following that newest branch until it runs out of new neighbors, then it backtracks to older waiting nodes like B.

DFS branch read

From A, DFS puts C on top of the stack frontier and dives there next.
The stack frontier is what makes DFS feel deep and path-like on the same graph that BFS explores layer by layer.

Common misconception

Depth-first search reaches the target fastest because it goes straight down one branch.

A deep branch is not the same thing as a short path. DFS can commit to a long detour before it notices a shallower alternative.

The point of DFS is the branch-first frontier rule, not guaranteed shortest routes.

Mini challenge

Build a DFS case where the current depth rises quickly before the target is reached.

Prediction prompt

Decide whether you need a layered graph or a hub-like graph before you switch presets.

Check your reasoning

Use a graph with a clear branch so DFS can keep the newest path on top for several steps.
Depth-first search dives fastest when one branch keeps discovering one new neighbor at a time.

Quick test

Reasoning

Question 1 of 2

Answer from the visible stack and branch behavior, not from memorized acronyms.

Why does DFS usually feel deeper before it feels wider on this bench?

Choose one answer to reveal feedback, then test the idea in the live system if a guided example is available.

Accessible description

The simulation shows one labeled graph with the current node, the stack frontier, and the visited nodes colored differently so the active branch stays readable.

A readout card reports the traversal mode, current node, frontier size, visited count, and target, while a cue panel shows the frontier order and the current neighbor list.

Graph summary

One graph tracks visited nodes against frontier size, a second tracks current depth against the deepest claimed depth, and a third compares new discoveries with repeat skips.

Together they show how depth-first search dives into a branch and then backtracks when that branch runs out.