DSA Notes
Learn BFS traversal with queue-based implementation, level-order processing, shortest path in unweighted graphs, and real applications.
The Intuition
Imagine you drop a stone into a still pond. Ripples spread outward in circles — first reaching everything 1 meter away, then 2 meters, then 3 meters. BFS explores a graph the same way: it visits all vertices at distance 1 from the source, then all at distance 2, then distance 3, and so on.
This "level by level" exploration is what makes BFS perfect for finding the shortest path in unweighted graphs — it literally explores nodes in order of their distance from the source.
How BFS Works
BFS uses a queue (FIFO) to track which vertex to explore next:
- Start at the source vertex, mark it visited, add it to the queue
- Dequeue a vertex, process it
- Enqueue all unvisited neighbors (mark them visited)
- Repeat until the queue is empty
The key insight: because we process vertices in FIFO order, we guarantee that the first time we reach any vertex, we reached it via the shortest path.
Dry Run Example
Graph
0 --- 1 --- 4
| |
2 --- 3
BFS from vertex 0
Step 1: Queue=[0], Visited={0}
Step 2: Dequeue 0, enqueue neighbors 1,2
Queue=[1,2], Visited={0,1,2}
Step 3: Dequeue 1, enqueue unvisited neighbors 3,4
Queue=[2,3,4], Visited={0,1,2,3,4}
Step 4: Dequeue 2, neighbor 3 already visited
Queue=[3,4], Visited={0,1,2,3,4}
Step 5: Dequeue 3, all neighbors visited
Queue=[4], Visited={0,1,2,3,4}
Step 6: Dequeue 4, all neighbors visited
Queue=[], Done!
BFS Order: 0, 1, 2, 3, 4
Level 0: {0}
Level 1: {1, 2}
Level 2: {3, 4}
Python Implementation
C++ Implementation
Time and Space Complexity
- Time: O(V + E) — we visit every vertex once and check every edge once
- Space: O(V) — for the visited set and queue (in worst case, all vertices could be in queue)
Why BFS Gives Shortest Path
BFS processes nodes level by level. When we first discover a node, it is from the closest possible source because:
- All distance-1 nodes are queued before any distance-2 node
- All distance-2 nodes are processed before any distance-3 node
- Therefore, first discovery = shortest distance
This ONLY works for unweighted graphs (or graphs where all edges have equal weight). For weighted graphs, use Dijkstra's algorithm.
Applications of BFS
- Shortest path in unweighted graph (the primary use case)
- Level-order traversal of trees (BFS on a tree)
- Finding connected components (run BFS from each unvisited vertex)
- Bipartite checking (try to 2-color the graph level by level)
- Web crawling (discover pages layer by layer from a seed URL)
- Social network analysis (find degrees of separation)
- Puzzle solving (shortest sequence of moves in games like sliding puzzle)
Common Interview Patterns
- Grid BFS: Treat a 2D grid as a graph, each cell connects to 4 neighbors. Used for "shortest path in maze" problems.
- Multi-source BFS: Start BFS from multiple sources simultaneously (e.g., "rotting oranges" problem).
- 0-1 BFS: For graphs with edge weights 0 or 1, use a deque — push weight-0 edges to front, weight-1 to back. Still O(V+E).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Breadth-First Search (BFS) Algorithm.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, bfs, algorithm
Related Data Structures & Algorithms Topics