Breadth-First Search (BFS)
Use the queue to follow discovery by distance in edges from the start vertex.
Core Intuition & How It Works
Breadth-First Search (BFS) explores a graph outward layer by layer using a FIFO queue. Because it visits all vertices at distance d before any at distance d + 1, BFS guarantees shortest paths in unweighted graphs.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(V + E) |
| Space Complexity | O(V) |
| Topic Chapter | Chapter 11: Graph Traversal & Connectivity |
| Difficulty Level | Intermediate |
Key Pattern to Recognize
Mark each vertex as visited the moment you enqueue it—not when you dequeue it—to prevent duplicate queue entries.
Common Pitfalls & Edge Cases
Marking nodes visited on pop instead of push, which can enqueue the same node multiple times and blow up memory/time.
Real-World & Technical Interview Applications
Unweighted shortest paths, word ladders, social degree-of-separation queries, and multi-source grid diffusion.