Algorithms and Data Structures · Junior · Question 53 of 75

What is the difference between depth-first (DFS) and breadth-first (BFS) traversal?

Explanation

DFS is usually written with recursion or a stack, BFS with a queue. Both visit every node once, O(V + E), but in a different order. Both work on trees and on general graphs.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 53

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz