tree.svg
Current
Visited
In Queue
Unvisited
Explore BFS with a queue data structure and level-by-level exploration. Watch how the algorithm expands outward from the start node, visiting all neighbors before moving deeper. Step through the execution to see the complete picture.
Breadth-First Search (BFS) is a graph traversal algorithm that explores nodes layer by layer, visiting all neighbors before moving to the next level. It uses a queue — a FIFO (First-In-First-Out) data structure — to maintain the frontier of unexplored nodes. BFS is fundamental for finding shortest paths, level-order traversals, and connectivity analysis in unweighted graphs.
BFS on Tree — Example 1
[ ]
[ ]
BFS on Graph — Example 2
[ ]
[ ]
Algorithm & Pseudocode
| Aspect | Complexity |
|---|---|
| Time | O(V + E) |
| Space (Queue) | O(V) — queue stores frontier |
| Visited Set | O(V) — prevents revisiting |
Implementation