Breadth-First Search: A Complete Solved Numerical Example

Scenario: Campus Network Packet Routing

The Objective: Simulate a network packet broadcasting from the server (S) to its final destination (G). Observe how the algorithm explores the network layer-by-layer, ensuring it finds the path with the fewest total hops, regardless of individual link quality.

Core Mechanics
  • The FIFO Queue: BFS strictly runs on "First-In, First-Out". You must pop the oldest node from the front to evaluate it, and push its new neighbors to the back.
  • Level-by-Level Expansion: It completely finishes exploring depth dd before ever touching depth d+1d+1. This guarantees the shortest path based on hop count, ignoring edge costs.
  • Never Look Back: Once a node enters the Closed List (Visited), ignore it forever. Because BFS expands outward like a ripple, the first time you discover a node is mathematically guaranteed to be the fastest route to it.
  • The Goal Test (The Final Pop): Even if you see the goal waiting in the Open List, do not stop! You can only declare victory when the goal node is officially popped from the front and becomes the Evaluating Node.

Step 1: Initial Map Configuration

The structure of our network. BFS explores nodes level by level, ensuring the shortest path in terms of hop count.

Start Node: SGoal Node: G
S
A
B
C
D
E
G

Step 2: Step-by-Step FIFO Queue Resolution

StepOpen List (Queue)Evaluating NodeClosed List (Visited)
1
[ S ]S[ ]
2
[ A, B ]A[ S ]
3
[ B, C, D ]B[ S, A ]
4
[ C, D, E ]C[ S, A, B ]
5
[ D, E, G ]D[ S, A, B, C ]
6
[ E, G ]E[ S, A, B, C, D ]
7
[ G ]G[ S, A, B, C, D, E ]
8
[ ]Goal Reached[ S, A, B, C, D, E, G ]

Goal Reached

S → A → C → G
Total Path Steps: 3

Final Takeaway

Notice how the strict First-In, First-Out (FIFO) queue forces the algorithm to explore the graph in completely flat layers. Even if multiple paths reach the goal in exactly 3 hops, BFS simply locks in the path that entered the queue first (S→A→C→G) and ignores the rest!