← BACK TO KNOWLEDGE

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

  1. 01Traversal order encodes the question
  2. 02Visited state prevents repeated work
  3. 03Parents reconstruct paths
01

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
02

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
03

Debugging

Write down what makes two states equivalent. Incorrect visited logic is the most common source of missing or repeated paths.