Abstract


Use Cases

  • When you only care about whether a path exists
  • When the graph might be cyclic (just need proper visited[] handling)
  • When you want a simple recursive implementation

Complexity Analysis

  • Time Complexity: (visit each node and edge once)
  • Space Complexity: for visited and recursion stack

Pros

  • Easy to write recursively
  • Works fine for reachability
  • Low memory footprint

Cons

  • Might explore long wrong paths first
  • Not guaranteed to find the shortest path

Practice Questions

DFS with Backtracking


Use Cases

  • When you need to explore all possible paths/solutions
  • When you must undo decisions to try other paths (e.g., unmark visited)
  • Classic in combinatorial search, puzzles, and path enumeration

Complexity Analysis

  • Time Complexity: Worst-case (explores all paths)

Pros

  • Finds all paths, all solutions, all combinations
  • Works for grid problems, games, puzzle solving

Cons

  • Overkill for reachability
  • Can be very slow and even TLE if graph has cycles and large branching
  • Must be careful with revisiting nodes