Depth-First Search (DFS)¶
Understanding recursive graph traversal and maze/path exploration (used in the a-maze-ing project).
Table of Contents¶
- What is DFS?
- Graph Traversal
- DFS Mentality
- Stack Behavior
- Recursive Exploration
- Visual Example
- DFS Logic
- Visited Nodes
- Recursive Backtracking
- Iterative DFS
- Complexity
- DFS vs BFS
- DFS in Maze Generation
- Fly-in Connection
- Mental Model
1๏ธโฃ What is DFS?¶
DFS means:
Depth-First Search¶
It is an algorithm used to:
- traverse graphs
- explore paths deeply
- generate mazes
- search recursive structures
DFS explores:¶
go as deep as possible first
ONLY after:
backtrack
2๏ธโฃ Graph Traversal¶
Imagine this graph:
start
/ | \
A B C
/ \
D goal
DFS might explore:
start
โ
A
โ
D
โ
backtrack
โ
B
โ
C
โ
goal
3๏ธโฃ DFS Mentality¶
DFS constantly thinks:
"Keep going deeper."
It prioritizes:
current branch exploration
before checking siblings.
4๏ธโฃ Stack Behavior¶
DFS behaves like:
Stack¶
A stack works like:
Last In
First Out
This is called:
LIFO¶
Example¶
ADD A
ADD B
ADD C
Stack:
[A, B, C]
Remove:
C
because:
C was added last
5๏ธโฃ Recursive Exploration¶
DFS is commonly implemented using:
recursion
Example¶
visit(node)
โ
visit(neighbor)
โ
visit(neighbor)
Recursion Flow¶
start
โ
A
โ
D
โ
end branch
โ
backtrack
6๏ธโฃ Visual Example¶
Graph:
start
/ \
A B
/ \
C D
DFS exploration:
start
โ
A
โ
C
โ
backtrack
โ
D
โ
backtrack
โ
B
7๏ธโฃ DFS Logic¶
Core Idea¶
Explore deeply before exploring widely.
Recursive Pseudo Code¶
FUNCTION dfs(node):
mark node as visited
FOR each neighbor:
IF neighbor not visited:
dfs(neighbor)
8๏ธโฃ Visited Nodes¶
DFS MUST track visited nodes.
Otherwise:
infinite recursion
may happen.
Example Loop¶
A -> B
โ โ
D <- C
Without visited tracking:
A -> B -> C -> D -> A -> ...
forever ๐
Visited Example¶
visited = {
"start",
"A",
"B"
}
9๏ธโฃ Recursive Backtracking¶
When DFS reaches:
end of branch
it:
returns backwards
This is called:
backtracking¶
Visual Example¶
start
โ
A
โ
C
โ
no neighbors
โ
backtrack to A
โ
explore D
๐ Iterative DFS¶
DFS can also be implemented without recursion.
Using:
stack
Iterative Pseudo Code¶
CREATE stack
CREATE visited set
PUSH start node
WHILE stack not empty:
current = stack.pop()
IF not visited:
mark visited
PUSH neighbors
1๏ธโฃ1๏ธโฃ Complexity¶
DFS complexity is usually:
O(V + E)
Where:
V = vertices
E = edges
1๏ธโฃ2๏ธโฃ DFS vs BFS¶
DFS¶
Explores:
deep first
BFS¶
Explores:
layer by layer
DFS Mentality¶
"Keep going deeper"
BFS Mentality¶
"Explore closest nodes first"
1๏ธโฃ3๏ธโฃ DFS in Maze Generation¶
DFS is EXTREMELY popular for:
maze generation¶
Because:
DFS naturally creates long corridors
Example¶
Your A-Maze-ing project uses:
Randomized DFS
to:
- carve paths
- explore cells
- backtrack
- generate perfect mazes
DFS Maze Flow¶
current cell
โ
random neighbor
โ
carve wall
โ
continue deeper
โ
no neighbors?
โ
backtrack
1๏ธโฃ4๏ธโฃ Fly-in Connection¶
DFS is useful in Fly-in for:
- graph traversal learning
- recursive exploration
- understanding pathfinding
- understanding backtracking
BUT:
DFS does NOT guarantee cheapest path
or:
shortest weighted route
That is why:
Dijkstra fits Fly-in better
1๏ธโฃ5๏ธโฃ Mental Model¶
Think of DFS like:
exploring a cave
You:
- keep moving deeper
- follow one tunnel
- only return when stuck
Final Mental Image¶
DFS always asks:
"How far can I go from here?"
NOT:
"What is the closest node?"
That is the BIG difference between:
DFS
vs
BFS