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