Problems/

BFS vs DFS: When and Why

medium

Concept check — write, then compare

Compare breadth-first search (BFS) and depth-first search (DFS) for exploring a graph.

Your answer should cover:

  1. The order in which each visits nodes, and the data structure that produces that order.
  2. Which problems each is the right tool for (give at least two per traversal, with reasons).
  3. Their space behaviour — when does BFS's memory explode, and when does DFS's?
  4. Why BFS finds shortest paths in unweighted graphs but not weighted ones, and what handles the weighted case.
  5. Why a visited set is essential in graphs but unnecessary in trees.
Explaining out loud is the test — write as if teaching someone.