Graphs & Trees

Depth-First Search

Explores as far as possible along each branch before backtracking.

ABCDEFG

Step 1 of 15

Push start node "A" onto the stack.

Stack (top → bottom)

A

Visited

none yet
Algorithm
push s onto stack
while stack not empty:
u ← pop()
if u not visited:
mark u as visited
for each neighbor v of u:
push v onto stack
// DFS complete

Legend

Active
On stack
Visited
1 / 15Speed