Lesson 59 of 86IntermediatePro Interactive Lesson

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

MetricComplexity
Time ComplexityO(V + E)
Space ComplexityO(V)
Topic ChapterChapter 11: Graph Traversal & Connectivity
Difficulty LevelIntermediate

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.

Unlock the full course

All 86 lessons across 15 chapters

View pricing

Delete account

This permanently deletes your account and ends any Pro access.