Breadth-First Search (BFS)
Prerequisites
- State Space Representation — Frontier, explored set, tree vs graph search
🎯 Learning Objectives
- Implement BFS using a FIFO queue
- Trace BFS execution step-by-step on example graphs
- Prove BFS is complete (if b is finite) and optimal (if step costs are uniform)
- Analyze time complexity O(bd) and space complexity O(bd)
- Understand why memory is the limiting factor for BFS
1. Intuition
BFS explores the search tree level by level. It first visits all nodes at depth 0 (the root), then all nodes at depth 1, then depth 2, and so on. This guarantees that the shallowest goal node is found first.
Think of it like a ripple spreading outward from a stone dropped in water—the wave front moves uniformly in all directions, covering the nearest points first.
2. BFS Algorithm
Tree Search Version
function BFS-TREE(problem):
node = Node(problem.INITIAL-STATE)
if problem.GOAL-TEST(node.STATE): return SOLUTION(node)
frontier = FIFO-Queue(node)
loop:
if frontier is empty: return FAILURE
node = frontier.DEQUEUE() // remove from front
for each child in EXPAND(problem, node):
if problem.GOAL-TEST(child.STATE): return SOLUTION(child)
frontier.ENQUEUE(child) // add to back
Graph Search Version
function BFS-GRAPH(problem):
node = Node(problem.INITIAL-STATE)
if problem.GOAL-TEST(node.STATE): return SOLUTION(node)
frontier = FIFO-Queue(node)
explored = {}
loop:
if frontier is empty: return FAILURE
node = frontier.DEQUEUE()
add node.STATE to explored
for each child in EXPAND(problem, node):
if child.STATE not in explored AND child not in frontier:
if problem.GOAL-TEST(child.STATE): return SOLUTION(child)
frontier.ENQUEUE(child)
BFS applies the goal test when a node is generated (not when expanded). This is valid because all nodes at depth d are generated before any node at depth d+1 is expanded. This saves time—we don’t need to wait until the goal node reaches the front of the queue.
3. Step-by-Step Trace
Consider this graph (edges with unit cost):
S
/ \
A B
/ \ \
C D E
|
G (goal)
Adjacency: S→{A,B}, A→{C,D}, B→{E}, D→{G}. All edges cost 1.
| Step | Expand | Frontier (FIFO) | Explored |
|---|---|---|---|
| 0 | — | [S] | {} |
| 1 | S | [A, B] | {S} |
| 2 | A | [B, C, D] | {S, A} |
| 3 | B | [C, D, E] | {S, A, B} |
| 4 | C | [D, E] | {S, A, B, C} |
| 5 | D | [E, G] | {S, A, B, C, D} |
| 6 | — | G generated → GOAL TEST PASS! | — |
Solution: S → A → D → G (cost = 3). BFS found the shallowest goal at depth 3.
4. Properties of BFS
| Property | Value | Explanation |
|---|---|---|
| Complete? | Yes (if b is finite) | BFS will eventually reach every depth, so it will find a goal if one exists at any finite depth. |
| Optimal? | Yes (if step cost = 1) | BFS finds the shallowest goal first. If all steps cost the same, shallowest = cheapest. Not optimal with variable step costs—use UCS instead. |
| Time | O(bd) | In the worst case, all nodes up to depth d: 1 + b + b² + … + bd = O(bd). |
| Space | O(bd) | The frontier at depth d can hold up to bd nodes. The explored set also stores O(bd) nodes. |
BFS must store the entire frontier in memory. With b=10 and d=10, BFS needs to store approximately 1010 = 10 billion nodes. At ~1 KB per node, that’s 10 terabytes of memory. BFS is practical only for small to moderate search depths.
| Depth d | Nodes (b=10) | Memory | Time (at 10M nodes/sec) |
|---|---|---|---|
| 2 | 111 | 111 KB | < 1 ms |
| 4 | 11,111 | 11 MB | 1 ms |
| 6 | 1,111,111 | 1 GB | 0.1 sec |
| 8 | 108 | 100 GB | 10 sec |
| 10 | 1010 | 10 TB | 17 min |
| 12 | 1012 | 1 PB | 28 hours |
5. More Worked Examples
Example 2: BFS on a Cyclic Graph
Graph: A→B, B→C, C→A, B→D(goal). Start: A.
Graph search BFS:
- Frontier: [A]. Expand A → generate B. Explored: {A}. Frontier: [B].
- Expand B → generate C, D. C not in explored. D not in explored. Goal test D → GOAL!
Path: A → B → D. Cost = 2. Note: C→A would be discarded in graph search since A is in explored.
Example 3: BFS Finds Shallowest, Not Cheapest
Graph: S→A (cost 1), S→B (cost 100), A→G (cost 100), B→G (cost 1). Goal: G.
BFS expansion (FIFO): S, then A and B generated. Expand A → generate G. Goal test G → found! Path: S→A→G, cost=101.
But the optimal path is S→B→G, cost=101. Wait—in this case they’re equal. Let’s change: S→A (cost 10), A→G (cost 10), S→B (cost 1), B→G (cost 1). BFS still finds S→A→G (depth 2, cost 20) before or at the same time as S→B→G (depth 2, cost 2). Both are at the same depth, so which BFS finds depends on tie-breaking. With alphabetical ordering, A before B, so BFS returns cost 20—not optimal when step costs differ!
Summary
- BFS uses a FIFO queue and explores nodes level by level.
- Complete when branching factor b is finite.
- Optimal only when all step costs are equal (unit cost).
- Time and space: both O(bd)—exponential.
- Memory is the practical bottleneck, not time.
- Early goal test (at generation) is correct and efficient for BFS.
References
- Russell & Norvig, AIMA, Chapter 3.4.1.