LearnViz CompSci
Home / Learn / Algorithms / Breadth-First Search

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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Breadth-First Search?
Exploring a graph level by level using a queue. It belongs to Algorithms and is an intermediate-level topic.
Where do people use Breadth-First Search?
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.
What is the most common mistake with Breadth-First Search?
Marking nodes visited only when dequeued, rather than when enqueued, allows duplicates and blows up the queue.
What should I learn before this topic?
Start with N-Queens Problem, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz. You can also explore Graph Traversal in the Visual Lab.
Is Breadth-First Search hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Breadth-First Search?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Breadth-First Search?
N-Queens Problem, Depth-First Search, Backtracking are closely connected. The full list is in the related topics section of this page.
What should I study after Breadth-First Search?
Continue with Depth-First Search, the next topic in Algorithms.

Related topics