A
B
C
D
E
F
Call Stack

The Maze Runner

Depth First Search (DFS) explores as deep as possible before backtracking. Think of solving a maze by sticking to the left wall continually.

Start at Root

We begin at 'A'. We add it to our Stack (to remember where we are) and mark it as visited.

Go Deep (Left)

From 'A', we choose the first neighbor 'B'. We push 'B' onto the stack and visit it.

Deeper Still

From 'B', we go to 'D'. We are now at a dead end (Leaf node).

Backtrack

No more neighbors at 'D'. We pop 'D' off the stack and return to 'B'.

Explore Neighbor

Back at 'B', we see another neighbor 'E'. Let's visit it.

Return to Root

E is a dead end. Backtrack to B. B is done. Backtrack to A.

The Right Side

Finally, we explore 'A's other neighbor: 'C'.

Finish Line

From 'C', we visit 'F'. All nodes visited!

Exhaustive Search

DFS visits every Vertex and Edge. It's powerful for finding paths, cycles, and solving puzzles.

Maze Solved.

DFS uses a Stack to dive deep. What happens if we use a Queue instead?

AlgoAnimator: Interactive Data Structures