Lesson 100

DFS vs BFS

Stack vs Queue · Deep vs Wide

1:00

How depth-first search and breadth-first search differ by one data structure, and when to reach for each.

By the end, you can

  • Explain the single code difference between DFS and BFS and why it changes the visit order.
  • Trace the DFS and BFS visit order on a small graph by hand.
  • State that both algorithms have O(V + E) time complexity.
  • Compare the space complexity of DFS (O(h) on a tree) and BFS (O(width)) and explain when DFS uses less memory.
  • Explain why BFS guarantees the shortest path in an unweighted graph, and why DFS does not.
  • Identify which scenarios call for BFS and which call for DFS using the decision framework.
  • Describe why Dijkstra is needed when edges carry different weights.
  • Explain the cycle trap and state the correct time to mark a node as visited.
Up next in Searching & Sorting
Questions or feedback?