Breadth-First Search - Exploring Layer by Layer
Published on Saturday, June 20, 2026
Breadth-First Search (BFS) is a graph and tree traversal algorithm that explores nodes level by level — visiting every neighbor of the current node before moving on to neighbors of neighbors. If you’ve ever wondered how GPS systems find the fewest-turns route, or how social networks compute “degrees of separation,” BFS is at the heart of it.
A Brief History
The algorithm was conceived by Konrad Zuse in 1945 as part of his work on the Plankalkül programming language — though his work remained unpublished for decades. It was independently described by Edward F. Moore in 1959 for solving maze problems (“how to find the shortest path out”). C. Y. Lee formalized it in 1961 for circuit routing. The term “breadth-first search” itself was popularized by Nilsson in 1971. Today BFS is a cornerstone algorithm taught in every computer science curriculum worldwide.
How It Works
BFS uses a queue (first-in, first-out) to track which nodes to visit next:
- Initialize: Add the starting node to the queue and mark it as visited.
- Dequeue: Remove the front node from the queue.
- Process: Do whatever you need with this node (check if it’s the target, collect its value, etc.).
- Enqueue neighbors: For each unvisited neighbor of the current node, mark it visited and add it to the back of the queue.
- Repeat: Continue until the queue is empty or the target is found.
The key invariant: nodes are processed in the order they were discovered. This guarantees that when you first reach a node, you’ve found the shortest path to it (in terms of number of edges).
Time Complexity
BFS visits every vertex and traverses every edge exactly once:
| Case | Complexity |
|---|---|
| All cases |
Where is the number of vertices (nodes) and is the number of edges. This is in the total size of the graph — as efficient as it gets for complete traversal.
Space Complexity
BFS requires space for the queue and the visited set. In the worst case (a very wide, shallow graph), the queue could hold all nodes at the widest level simultaneously. For a balanced binary tree with nodes, the last level has nodes — so the queue can grow to .
This is BFS’s main disadvantage compared to Depth-First Search, which uses space where is the tree height.
Advantages and Disadvantages
Advantages:
- Finds the shortest path in unweighted graphs — guaranteed by the level-by-level exploration
- Explores nodes in order of increasing distance from the source
- Works on both directed and undirected graphs
- Naturally produces level-order traversal for trees
Disadvantages:
- Higher memory usage than DFS — the queue can be very large for wide graphs
- Not well-suited for finding paths in weighted graphs (use Dijkstra’s instead)
- Less cache-friendly than DFS due to jumping between nodes at the same level
When to Use BFS
BFS is the right choice when:
- Shortest path in an unweighted graph — BFS guarantees the minimum number of edges between source and target
- Level-order traversal of a tree — useful for printing by depth, finding nodes at a specific depth
- Web crawling — explore pages in order of link distance from the start
- Social network analysis — find the shortest chain of connections between two people (“six degrees of separation”)
- Solving puzzles with minimum moves — sliding puzzles, Rubik’s cube (where each state is a graph node)
- Testing if a graph is bipartite — a 2-coloring problem solvable by BFS
For deep, narrow problems where the target is far down a single path, consider Depth-First Search instead — it uses less memory and gets to deep nodes faster.
In conclusion, BFS is one of the most important graph algorithms in computer science. Its level-by-level exploration makes it the definitive algorithm for shortest-path problems in unweighted graphs. The queue-based approach is intuitive once you see it: process what you found first, before chasing new discoveries.