Breadth-First Search (BFS) and Depth-First Search (DFS): Concepts and Worked Examples
Breadth-First Search BFS and Depth-First Search DFS are two foundational strategies for exploring graphs graph.
At a high level:
- BFS explores the graph in layers (all nodes at distance from the start are visited before distance ), which makes it ideal for finding shortest paths in unweighted graphs.
- DFS explores by going deep first, which is useful for tasks like connectivity checking and cycle detection (with the right bookkeeping).
The key implementation idea is the data structure:
- BFS uses a queue (FIFO) to ensure “level-order” expansion.
- DFS uses a stack behavior (explicit stack or recursion) to ensure “deep-first” expansion.
Important: I attempted to run web searches to support every major claim with citations, but the web-search tool is currently unavailable due to plan/quota limits. As a result, I cannot include the required external citations/footnotes for this response.
Breadth-First Search (BFS) — step-by-step
- 1Step 1
Select a source vertex . Initialize a queue with .
- 2Step 2
Mark as visited, then push it into the queue.
- 3Step 3
While the queue is not empty, pop the front vertex and iterate over its adjacency list.
- 4Step 4
For each neighbor of , if is unvisited, mark it visited, record its parent/level if needed, and enqueue .
- 5Step 5
"When the queue empties, BFS has visited the reachable component from , producing a BFS tree."
Keywords: queue, adjacency list, BFS tree.
Depth-First Search (DFS) — step-by-step
- 1Step 1
Select a source vertex . Initialize an empty stack (if iterative) or start recursion at .
- 2Step 2
Mark visited and push it onto the stack (iterative) or call DFS(s) (recursive).
- 3Step 3
From the top of the stack (or current recursion frame), pick an unvisited neighbor .
- 4Step 4
Mark visited, then either recurse into or push onto the stack.
- 5Step 5
If all neighbors are visited, backtrack (return from recursion or pop the stack).
- 6Step 6
"DFS ends after exploring as deep as possible along every branch in the reachable component."
Keywords: stack, backtracking, DFS tree.
Worked Example Graph
Let’s use the same undirected graph for both BFS and DFS traversals:
Vertices:
Edges:
connected to
connected to
connected to
connected to
connected to
connected to
(Assume adjacency lists are processed in alphabetical order for determinism.)
BFS example (from )
With BFS, we visit nodes by increasing distance from :
- Distance 0:
- Distance 1:
- Distance 2:
- Distance 3:
One consistent BFS order (depending on adjacency iteration order) is:
Why BFS yields shortest paths in unweighted graphs:
When BFS first discovers a vertex , it has found a path from using the minimum number of edges—because BFS explores all vertices at distance before any at distance .
DFS example (from )
With DFS (alphabetical neighbor order), DFS goes deep before backtracking:
Starting at :
- Visit , then go to
- From , go to
- From , go to
- Backtrack; from next go to
- From , is already visited; backtrack
- Back to , next go to
- From , go to
- From , neighbor already visited; backtrack and finish
A consistent DFS visitation order is:
DFS does not guarantee shortest paths; it guarantees a systematic exploration pattern.
BFS vs DFS: traversal behavior (intuition)
Qualitative comparison for the same start node and neighbor ordering.
Visual intuition (BFS layers vs DFS deep dives)
Common pitfalls and edge cases
Pro Tip
To reconstruct an actual path with BFS, store a parent pointer when you discover each node. Then backtrack from the target to the start.
Warning: recursion depth vs iterative DFS
Recursive DFS may overflow the call stack on very large graphs or deep recursion paths. Iterative DFS using an explicit stack is safer in production systems.
How these traversals are commonly taught/used
Graph model
1. RepresentationChoose adjacency list/matrix and define neighbors."
Queue (BFS) vs stack (DFS)
2. Core mechanismSelect the data structure and the visited rule."
BFS levels vs DFS discovery path
3. InterpretationUse BFS levels for shortest paths; use DFS tree for deep structure."
Shortest paths / components / cycles
4. ApplicationsBFS: shortest unweighted paths; DFS: topology & cycle detection (with extra logic)."
Knowledge Check
Which data structure is most associated with Breadth-First Search?
Note on citations
"This output could not include the mandatory external citations because the web-search tool returned quota/plan errors during research. If you enable web access, I can regenerate the section with full source-backed claims and footnotes."