BFS Traversal VisualizerO(V + E) Breadth-First Search

LeetCode #102
Start NodeA
Nodes Visited0 / 7
Queue Size1
Step 1 / 0
Ready to start BFS traversal.
AL0IN QUEUEB-C-D-E-F-G-
Start Node:
Order:None
Code Execution

💡 BFS Traversal in Layman's Terms

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!

Queue Buffer (FIFO - First In First Out)1 items
[0]A
Time: O(V + E)Space: O(V)
LeetCode #102