Breadth-First Search (BFS)

GATE Relevance: High

Prerequisites

🎯 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)
Early Goal Test

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.

StepExpandFrontier (FIFO)Explored
0—[S]{}
1S[A, B]{S}
2A[B, C, D]{S, A}
3B[C, D, E]{S, A, B}
4C[D, E]{S, A, B, C}
5D[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

PropertyValueExplanation
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.
TimeO(bd)In the worst case, all nodes up to depth d: 1 + b + b² + … + bd = O(bd).
SpaceO(bd)The frontier at depth d can hold up to bd nodes. The explored set also stores O(bd) nodes.
⚠ Memory is the Bottleneck

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 dNodes (b=10)MemoryTime (at 10M nodes/sec)
2111111 KB< 1 ms
411,11111 MB1 ms
61,111,1111 GB0.1 sec
8108100 GB10 sec
10101010 TB17 min
1210121 PB28 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:

  1. Frontier: [A]. Expand A → generate B. Explored: {A}. Frontier: [B].
  2. 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.