Discrete Mathsfor Engineers

Structures, proofs, and intuition

EN | PT

Breadth-First Search (BFS)

Part 8 — BFS vs DFS

Series: Breadth-First Search (BFS) Part 8 of 8

8. BFS vs DFS


Roadmap (what you will learn in this part):
  1. The core difference: exploration order
  2. Data structures: queue vs stack (or recursion)
  3. What each traversal produces (trees + metadata)
  4. When BFS is the right tool
  5. When DFS is the right tool
  6. Complexity comparison
  7. Common pitfalls & engineering guidelines

BFS and DFS are the two fundamental graph traversals. They look similar in code, but their structural behavior is different: BFS expands by layers; DFS dives deep along a path.

This part gives you a practical mental model: how to choose the right one quickly.

8.1 Core Difference: Exploration Order

BFS: explores vertices by increasing distance from the source (layer by layer).
DFS: explores by going as deep as possible before backtracking.

The order is not cosmetic: it changes what you can guarantee. BFS guarantees shortest paths in unweighted graphs; DFS does not.

8.2 Queue vs Stack

The data structure dictates the traversal:

  • BFS uses a queue (FIFO) → preserves layers.
  • DFS uses a stack (LIFO) (explicit) or recursion (implicit call stack).
Rule of thumb: FIFO → “expand equally”; LIFO → “commit to a path”.

8.3 Side-by-Side Comparison

Aspect BFS DFS
Exploration Layer by layer Deep then backtrack
Data structure Queue (FIFO) Stack (LIFO) / recursion
Shortest paths (unweighted) Yes No
Typical outputs Distances + predecessor tree Discovery/finish times, DFS tree/forest
Best for Minimum hops, layers, nearest target Structure, cycles, topological order, SCC
Complexity O(|V|+|E|) O(|V|+|E|)

8.4 What BFS Produces

BFS produces two “usable” artifacts:

  • d[v] — shortest distance from s to v (in edges)
  • π[v] — predecessor pointers, forming a BFS tree
Key guarantee: In unweighted graphs, d[v] = δ(s,v).

8.5 What DFS Produces

DFS is best seen as a “structure extractor”. Common DFS metadata:

  • discovery time (tin[v]) and finish time (tout[v])
  • DFS tree / forest
  • edge classification (tree, back, forward, cross) — in directed graphs
Why this matters: Many global graph properties are proven/implemented using DFS time structure.

8.6 When to Choose BFS

Choose BFS when edges represent unit transitions and you care about:

  • minimum number of steps (shortest path in hops)
  • nearest target (early exit when you first reach it)
  • layers / wavefront (broadcast, diffusion, degrees of separation)
  • grid shortest moves (maze, robotics on 2D maps)
One-liner: “If the cost is 1 per move, try BFS first.”

8.7 When to Choose DFS

Choose DFS when you need deep structure or global graph properties:

  • topological sorting (DAG scheduling)
  • cycle detection (especially in directed graphs)
  • connected components (also possible with BFS, but DFS is common)
  • strongly connected components (Kosaraju, Tarjan)
  • backtracking / state-space search (when you must explore choices)
One-liner: “If you need ordering, structure, or recursion-like exploration, use DFS.”

8.8 Complexity

With adjacency lists, both traversals run in:

O(|V| + |E|)

Differences are usually in memory profile:

  • BFS may store an entire frontier (worst-case: large queue).
  • DFS stores a path plus recursion/stack (depth-dependent).
Practical note: For very deep graphs, recursive DFS can overflow the call stack; use an explicit stack if needed.

8.9 Common Pitfalls & Guidelines

  • Mixing goals: using DFS when you need shortest paths (wrong tool).
  • Late marking: in BFS, mark visited when enqueuing, not when dequeuing (avoids duplicates).
  • Disconnected graphs: to traverse everything, run BFS/DFS from each unvisited vertex.
  • Weighted edges: BFS is not for weighted shortest paths → use Dijkstra / 0-1 BFS.
Guideline: Start with the question: “Do I need the nearest solution, or do I need structure?”

Series Complete

You now have the full BFS toolkit: intuition (layers), formal algorithm, correctness proof, complexity analysis, tree/path reconstruction, applications — and how BFS compares to DFS.

Next steps:
  • Revisit the BFS Series Overview
  • Practice on grids and shortest-hop problems (BFS + path reconstruction)
  • Move on to DFS fundamentals and topological sorting