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
- Crawling web pages, finding friends-of-friends and solving mazes in games
Common mistake
Forgetting to mark vertices as visited, causing infinite loops.
How to learn this
- Read the key idea and say it in your own words.
- Work through the worked example, then try a similar problem.
- 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
- Tautologies and Contradictions – Always true or always false statements.
- Logical Equivalence – Statements with identical truth tables.
- De Morgan’s Laws – Negation swaps AND and OR.
- Converse and Contrapositive – A conditional equals its contrapositive.
- Quantifiers – “For all” and “there exists”.
- Negating Quantified Statements – Flip the quantifier and negate the predicate.