LearnViz Math
Advanced ⛓ Discrete Math & Number Theory Topic 59 of 103

Breadth- and Depth-First Search

Systematic graph traversal.

Key idea

Systematic graph traversal.

In depth

BFS explores level by level using a queue and finds the shortest paths in unweighted graphs. DFS goes as deep as possible first using a stack or recursion, useful for cycle detection and topological order. Both visit every vertex in O(V + E) time.

Worked example

BFS from a vertex visits all neighbours at distance 1, then distance 2.

Real-world uses

Common mistake

Forgetting to mark vertices as visited, causing infinite loops.

How to learn this

  1. Read the key idea and say it in your own words.
  2. Work through the worked example, then try a similar problem.
  3. Use the interactive lesson’s visual and mini-quiz to check the pattern.

Frequently asked questions

What is Breadth- and Depth-First Search?

Systematic graph traversal. It is a topic at the advanced level in Discrete Math & Number Theory.

Where is Breadth- and Depth-First Search used in real life?

Real-world examples: Crawling web pages, finding friends-of-friends and solving mazes in games.

What should I learn before Breadth- and Depth-First Search?

Make sure you are comfortable with Shortest Paths (Dijkstra), the previous topic in Discrete Math & Number Theory.

What is a common mistake with Breadth- and Depth-First Search?

Forgetting to mark vertices as visited, causing infinite loops.

How can I practise Breadth- and Depth-First Search?

Open the interactive lesson, play with the visual, then answer the three-question mini-quiz. Getting two right marks the topic complete.

What comes after Breadth- and Depth-First Search?

Move on to Planar Graphs next.

Related advanced topics in Discrete Math & Number Theory