Common Mistakes

Common errors to avoid

  • Forgetting visited tracking in cyclic graphs: Unlike trees, graphs can have cycles. Mark nodes visited before recursing to avoid infinite loops.
  • Modifying grid without restoration for backtracking: If trying multiple paths, restore state after exploring each path.

Permanent marking prevents valid paths.

  • Stack overflow on deep recursion: For deep graphs, convert to iterative DFS with explicit stack to avoid stack overflow.
  • Wrong order for topological sort: Use post-order DFS (add to result after processing children) for correct topological order.