Breadth-First Search (BFS)¶
Understanding graph traversal and shortest path search (used in the a-maze-ing project)
Table of Contents¶
- What is BFS?
- Graph Traversal
- Queue Concept
- Why BFS uses deque
- Layer-by-Layer Exploration
- Visual Example
- BFS Logic
- Visited Nodes
- Parent Tracking
- Path Reconstruction
- Complexity
- BFS vs Dijkstra
- Fly-in Connection
- Mental Model
1ī¸âŖ What is BFS?¶
BFS means:
Breadth-First Search¶
It is an algorithm used to:
- traverse graphs
- explore neighbors
- find shortest paths
- avoid infinite loops
BFS explores:¶
layer by layer
NOT:
deep first
2ī¸âŖ Graph Traversal¶
Imagine this graph:
start
/ | \
A B C
/ \
D goal
BFS explores:
start
â
A B C
â
D goal
It explores all neighbors before going deeper.
3ī¸âŖ Queue Concept¶
BFS uses:
FIFO Queue¶
FIFO means:
First In
First Out
Example¶
ADD A
ADD B
ADD C
Queue:
[A, B, C]
Remove:
A
because:
A entered first
4ī¸âŖ Why BFS uses deque¶
Python provides:
from collections import deque
deque means:
double-ended queue
Useful Operations¶
append()
popleft()
Example¶
from collections import deque
queue = deque()
queue.append("A")
queue.append("B")
queue.append("C")
print(queue.popleft())
Output:
A
5ī¸âŖ Layer-by-Layer Exploration¶
BFS explores nodes in waves.
Visual Example¶
LEVEL 0:
start
LEVEL 1:
A B C
LEVEL 2:
D goal
BFS always explores:
closest nodes first
6ī¸âŖ Visual Simulation¶
Imagine water spreading:
start
â
A B C
â
D goal
BFS spreads equally in all directions.
7ī¸âŖ BFS Logic¶
Core Idea¶
Explore neighbors first.
Pseudo Code¶
CREATE queue
CREATE visited set
CREATE parent dictionary
ADD start zone to queue
MARK start as visited
WHILE queue not empty:
current = queue.pop_left()
IF current == goal:
reconstruct path
RETURN path
FOR each connected zone:
IF not visited:
mark visited
save parent
add to queue
8ī¸âŖ Visited Nodes¶
BFS MUST track visited nodes.
Why?
Because graphs may contain loops.
Example¶
A -> B
â â
D <- C
Without visited tracking:
infinite loop
Visited Set¶
Example:
visited = {
"start",
"A",
"B"
}
9ī¸âŖ Parent Tracking¶
BFS usually stores:
where each node came from
Example¶
{
"A": "start",
"goal": "C"
}
This allows path reconstruction later.
đ Path Reconstruction¶
Once BFS reaches the goal:
go backwards using parents
Example¶
goal
â
C
â
start
Reverse result:
start -> C -> goal
1ī¸âŖ1ī¸âŖ Complexity¶
BFS complexity is usually:
O(V + E)
Where:
V = vertices (zones)
E = edges (connections)
1ī¸âŖ2ī¸âŖ BFS vs Dijkstra¶
BFS¶
Good for:
- unweighted graphs
- same movement costs
- shortest path in number of steps
Dijkstra¶
Good for:
- weighted graphs
- movement costs
- cheapest total route
Example¶
BFS thinks:
fewest movements
Dijkstra thinks:
lowest total cost
1ī¸âŖ3ī¸âŖ Fly-in Connection¶
Fly-in uses:
normal = 1
priority = 1
restricted = 2
blocked = impossible
This means:
not all paths cost the same
So:
BFS is useful to learn graph traversal
BUT:
Dijkstra fits Fly-in better
1ī¸âŖ4ī¸âŖ Mental Model¶
Think of BFS like:
water spreading equally
or:
exploring closest rooms first
Final Mental Image¶
BFS always asks:
"What is the closest unexplored node?"
NOT:
"What is the cheapest node?"
That is the BIG difference between:
BFS
vs
Dijkstra