Discrete Mathsfor Engineers

Structures, proofs, and intuition

EN | PT

Depth-First Search (DFS)

Part 8 — DFS vs BFS

Series: Depth-First Search (DFS) Part 8 of 8

8. DFS vs BFS


Roadmap (what you will learn in this part):
  1. The core contrast between DFS and BFS
  2. Stack vs queue
  3. What BFS naturally computes
  4. What DFS naturally computes
  5. A side-by-side comparison
  6. When DFS is the right tool
  7. When BFS is the right tool
  8. Complexity comparison
  9. Common misconceptions
  10. Series summary and closing

DFS and BFS are the two fundamental graph traversal strategies. They both explore graphs in linear time, but they reveal different kinds of structure.

BFS is distance-oriented. DFS is structure-oriented.

Takeaway: If BFS is the language of layers, DFS is the language of recursive structure.

8.1 The Core Difference

The fundamental contrast is:

  • BFS explores the graph layer by layer.
  • DFS explores one path deeply before backtracking.

This difference changes not only the traversal order, but also the kind of information each algorithm naturally exposes.

8.2 Queue vs Stack

The data structure behind each traversal explains its behavior:

  • BFS uses a queue (FIFO).
  • DFS uses a stack (explicit or implicit via recursion).
Interpretation: FIFO preserves breadth; LIFO preserves depth.

8.3 What BFS Naturally Computes

BFS is the natural tool when we care about distance from a source.

  • shortest paths in unweighted graphs
  • layer structure
  • minimum number of moves
  • reachability by hop count
\( d[v] = \delta(s,v) \)

This is why BFS is the standard algorithm for shortest paths in unweighted graphs.

8.4 What DFS Naturally Computes

DFS is the natural tool when we care about graph structure.

  • discovery and finish times
  • ancestry relations
  • edge classification
  • cycle detection
  • topological sorting
  • strongly connected components

DFS turns traversal into a structural decomposition method.

8.5 Side-by-Side Comparison

Aspect BFS DFS
Main strategy Explore by layers Explore deeply, then backtrack
Primary data structure Queue Stack / recursion
Best for Distance and shortest paths Structure and decomposition
Shortest path in unweighted graphs Yes No
Cycle detection Possible, but not natural Natural and elegant
Topological sorting No Yes
Strongly connected components No Yes
Time complexity O(|V|+|E|) O(|V|+|E|)

8.6 When to Prefer DFS

Choose DFS when the problem asks for structural information such as:

  • Does the graph contain a cycle?
  • Can the graph be topologically sorted?
  • What are its strongly connected components?
  • Which vertices are ancestors/descendants in the exploration tree?
Rule of thumb: choose DFS when the problem is about the internal organization of the graph.

8.7 When to Prefer BFS

Choose BFS when the problem asks for distance-like information such as:

  • What is the shortest path in an unweighted graph?
  • What is the minimum number of steps?
  • Which vertices lie in layer 1, 2, 3, ... from a source?
Rule of thumb: choose BFS when the problem is about closeness to a source.

8.8 Complexity

With adjacency lists, both traversals run in:

\( O(|V| + |E|) \)

So the main difference is not asymptotic complexity, but the kind of information each traversal exposes.

8.9 Common Misconceptions

  • “DFS can also find shortest paths.” Not in general.
  • “BFS and DFS are interchangeable.” They are not; they solve different structural goals.
  • “DFS is only recursion.” DFS is a traversal principle; recursion is just one implementation style.

8.10 Series Summary

Across this DFS series, we studied:

  • the graph model and exploration problem
  • deep exploration and backtracking
  • the formal DFS algorithm
  • discovery and finish times
  • edge classification
  • cycle detection and topological sorting
  • strongly connected components
Final takeaway: DFS is one of the most powerful structural tools in graph theory and algorithms.

Series Complete

This closes the Depth-First Search series. You now have both the formal algorithmic view and the structural perspective needed to apply DFS in theoretical and engineering contexts.