KNOWLEDGE / Algorithms / Graphs
Graph Traversal and State
A decision framework for BFS, DFS, visited state, and path reconstruction.
AlgorithmsGraphsBFS
- DOMAIN
- Data Structures & Algorithms
- LEVEL
- Intermediate
- READ
- 7 min
- UPDATED
- Aug 3, 2026
MENTAL MODEL / KEY IDEAS
Keep these in mind
- 01Traversal order encodes the question
- 02Visited state prevents repeated work
- 03Parents reconstruct paths
Choose the traversal
BFS explores by distance and naturally finds shortest unweighted paths. DFS explores depth and fits structural or exhaustive questions.
- Distance layers
- Component discovery
- Cycle detection
Model the state
A node alone may not describe a search state. Constraints such as remaining stops or collected keys can be part of the visited key.
- Node identity
- Path-dependent state
- Parent pointers
Debugging
Write down what makes two states equivalent. Incorrect visited logic is the most common source of missing or repeated paths.