Breadth-First Search
BFS explores a graph level by level, visiting all neighbors before moving to the next level. Uses a queue data structure.
IntermediateTime: O(V + E)Space: O(V)
Take QuizStep 1 / 0
Current Step
Click Play to start the visualization
## How Breadth-First Search Works
BFS explores a graph level by level. It starts at the root and explores all neighbors before moving to the next level of neighbors.
### Algorithm Steps:
1. Start at the source node and mark it visited
2. Add source to a queue
3. While queue is not empty:
- Dequeue a node
- For each unvisited neighbor, mark visited and enqueue
4. Repeat until all reachable nodes are visited
### Key Characteristics:
- **Uses Queue**: First-In-First-Out (FIFO) data structure
- **Shortest Path**: Finds shortest path in unweighted graphs
- **Level Order**: Visits nodes level by level
Time & Space Complexity
| Case | Time Complexity |
|---|---|
| Best | O(V + E) |
| Average | O(V + E) |
| Worst | O(V + E) |
| Space | O(V) |
Practice Problems
10 problems81 total points
1
BFS Level Order Traversal
Beginner5 pts
2
Maximum Depth of Tree
Beginner5 pts
3
Number of Islands
Beginner5 pts
4
Shortest Path in Binary Matrix
Intermediate8 pts
5
Rotting Oranges
Intermediate8 pts
6
Word Ladder
Advanced10 pts
7
Minimum Knight Moves
Advanced10 pts
8
Open the Lock
Advanced10 pts
9
Shortest Path with Alternating Colors
Advanced10 pts
10
Bus Routes
Advanced10 pts