Recursive Backtracking Algorithm


Summary

Recursive backtracking is an algorithmic technique for solving problems incrementally by exploring all possible paths and reverting when a path leads to a dead end. It is commonly used in puzzles, pathfinding, and constraint satisfaction problems like Sudoku, N-Queens, and subset generation.

Recursive Backtracking Algorithm

Recursive backtracking is a problem-solving method where you build a solution step-by-step, and abandon a path ("backtrack") as soon as it’s determined to be invalid. It’s especially powerful for problems with multiple choices at each step and where only one or some combinations are valid.

How Backtracking Works

Backtracking involves recursion with state tracking. At each step, the algorithm explores a decision, recurses into the next level, and undoes the decision if it doesn’t lead to a valid solution. This approach ensures that all potential configurations are explored without redundancy.

Use Cases

  • N-Queens: Placing queens on a board so no two attack each other
  • Sudoku Solver: Filling a grid according to constraints
  • Combinations/Subsets: Generating all valid sets or permutations
  • Pathfinding: Exploring all possible routes in a maze

Performance Consideration

Backtracking has exponential time complexity in the worst case, but pruning techniques such as early stopping and memoization can significantly improve its efficiency.

Conclusion

Mastering recursive backtracking enhances your ability to solve constraint-driven problems. It teaches careful state management and control flow, making it a must-know for algorithmic interviews and real-world problem solving.


Recursive Backtracking: A Fundamental Algorithmic Technique

Recursive backtracking is a classic algorithm used to solve problems that require an exhaustive search of all possible solutions. It combines the principles of recursion and backtracking to explore solution spaces systematically by making decisions, exploring their consequences recursively, and undoing those decisions to explore other possibilities.

What Is Recursive Backtracking?

At its core, recursive backtracking is about exploring all potential combinations or configurations of a solution by:

  • Making a decision
  • Recursively exploring the consequences of that decision
  • Undoing (backtracking) the decision to try alternative options

This approach is particularly useful for exhaustive search problems where you need to find all possible solutions, such as subsets, permutations, combinations, and constraint satisfaction problems.

When Is Recursive Backtracking Used?

Recursive backtracking is ideal for problems where:

  • All combinations or configurations need to be evaluated
  • Each decision point leads to a branching structure (like a tree)
  • Solutions must be built incrementally and undone cleanly

Common applications include generating subsets, solving mazes, placing queens on a chessboard (N-Queens problem), and Sudoku solvers.

How Recursive Backtracking Works

Visualizing the Process

The recursive backtracking process can be visualized as a binary decision tree. For example, in the subsets problem, each number in the array presents two choices:

  • Don't pick the number – proceed without including it
  • Pick the number – add it to the current solution and explore further

Starting with an empty list, the algorithm traverses this tree in a Depth-First Search (DFS) manner, exploring one full path before backtracking to explore the next branch.

Key Components of Backtracking

The recursive function typically maintains two data structures:

  • res (results): A global list holding all final valid solutions. This is returned at the end.
  • sol (solution): A dynamic list used to build the current partial solution. Elements are added or removed as decisions are made or undone.

Structure of the Recursive Function

The function is often called backtrack and takes an index i representing the current decision point.

Base Case

When i == n (where n is the length of the input array), all elements have been considered. At this point:

  • A copy of the current sol is appended to res
  • The function then returns to explore other paths

Copying is essential—since sol is reused, appending the reference would lead to incorrect results.

Recursive Steps: Making and Undoing Decisions

For each index i, two recursive calls are typically made:

  1. Don't Pick: Move to the next index without modifying sol. backtrack(i + 1)
  2. Pick:
    • Add nums[i] to sol
    • Recursively call backtrack(i + 1)
    • Backtrack by removing the last element from sol using pop()

Start and Termination

The process is initiated by calling backtrack(0). After all recursive calls complete, the list res will contain every valid solution built throughout the process.

Complexity Analysis

Time Complexity

For problems like generating subsets, the recursive tree has 2^N leaf nodes, where N is the length of the input. This results in:

Time Complexity: O(2^N)

Space Complexity

  • Call Stack: The maximum depth of recursion is N
  • Solution Storage: Depends on the number and size of the results stored in res

Overall Space Complexity: O(N) (not including the output)

Summary

Recursive backtracking is a versatile and elegant way to explore all solutions to a problem by building partial solutions and revisiting decision points through recursion and backtracking. It is an essential technique in the toolbox of any algorithmic problem-solver.

Get Personalized Lessons at AlgoMap Bootcamp 💡