Intermediate Topic 31 of 51 Algorithms
Breadth-First Search
Exploring a graph level by level using a queue.
How it works
Breadth-first search explores a graph level by level from a start vertex, using a queue to visit all neighbors at distance one before those at distance two, and so on. It marks vertices visited when enqueued, runs in O(V + E) and finds the shortest path in terms of number of edges in unweighted graphs.
Where it is used
Shortest paths in unweighted graphs, finding connected components, social-network distance, web crawlers, level-order tree traversal, flood fill and solving puzzles like word ladder or sliding tiles use BFS.
Common pitfall
Marking nodes visited only when dequeued, rather than when enqueued, allows duplicates and blows up the queue. BFS uses O(V) memory for the frontier, which can be large in wide graphs. It does not give shortest paths in weighted graphs; use Dijkstra there.
Where it fits in the course
Breadth-First Search is topic 31 of 51 in Algorithms, rated Intermediate. It follows N-Queens Problem. The next topic is Depth-First Search.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the Graph Traversal visualizer: BFS, DFS and Dijkstra on a weighted graph.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Breadth-First Search?
Where do people use Breadth-First Search?
What is the most common mistake with Breadth-First Search?
What should I learn before this topic?
How can I practice it?
Is Breadth-First Search hard to learn?
How long does it take to learn Breadth-First Search?
Which topics are related to Breadth-First Search?
What should I study after Breadth-First Search?
Related topics
- N-Queens Problem (Algorithms)
- Depth-First Search (Algorithms)
- Backtracking (Algorithms)
- Topological Sort (Algorithms)
- Search Engines (Emerging & Frontier Topics)
- Shortest Job First (Operating Systems)
- Binary Search Trees (Data Structures)