Depth-First Search
Updated 2026-07-30
Summary
In computer science, depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking…
Definition
A traversal/search strategy: go all the way down one path until no further progress is possible, then backtrack and try another path. The counterpart is breadth-first search (exploring all adjacent options first). Scaffold use: clarify the choice between "depth-first" and "breadth-first". Each strategy has its applicable scenarios: - Depth-first: when the solution is likely deep, or when a complete path is needed - Breadth-first: when the solution is likely shallow, or when the shortest path is needed
Introduction
How to use
Clarify the choice between "depth-first" and "breadth-first". Each strategy has its applicable scenarios: - Depth-first: when the solution is likely deep, or when a complete path is needed - Breadth-first: when the solution is likely shallow, or when the shortest path is needed
Mechanism
DFS: starting from the origin, go as deep as possible along one branch, then backtrack to explore sibling branches upon reaching a leaf; can be implemented with a stack or recursion, space is better than BFS, but does not guarantee finding the shortest path.
Practice
In progress 0/4Practice this model in one real situation:
Cases
- Maze solving, topological sorting, connected components; backtracking for Sudoku.
Common pitfalls
- Confusing with BFS (DFS does not guarantee shortest path); not marking visited leads to infinite loops; excessive recursion depth causes stack overflow.
Sources
Source support: Explicit
References currently contain only Wikipedia links; primary sources still need review.
- en.wikipedia.orghttps://en.wikipedia.org/wiki/Depth-first_searchverified
account_treeGenealogyexpand_more
PRIVATE NOTES · Only visible to you
SAVED Q&A
ENTRY Q&A · Private saving available
Ask with a clear boundary
thinkingmodels answers from published entry context only.
Your question is sent to thinkingmodels. The answer uses public entry context only.
RELATED MODELS