Breadth-First Search (BFS) works like water ripples expanding in a quiet pond! It visits nodes level-by-level (Layer 0 âž” Layer 1 âž” Layer 2) using a Queue (First-In, First-Out).
How it Works Step-by-Step:
1Join the Queue: Start at the root node (Level 0) and add it to the waiting line (Queue).
2Process First Person in Line: Dequeue the front node from the queue (curr = queue.shift()).
3Invite All Direct Friends: Look at all direct neighbors of curr. Add any unvisited friends to the back of the queue.
4Finish Level-by-Level: Repeat until the queue is completely empty!
📢 Real-World Analogy: Social Media Post Ripple Effect
Imagine posting news on social media. First, your 1st-degree direct friends see it (Level 1). Then, your friends reshare it so their friends see it (Level 2 1st-degree friends-of-friends). Everyone at distance 1 gets notified before anyone at distance 2!