Overview
Backtracking is an algorithmic technique for solving problems by incrementally building candidates and abandoning a candidate (“backtracking”) as soon as it determines that the candidate cannot lead to a valid solution.Think of backtracking as a smart exhaustive search: try a solution, if it doesn’t work, undo it and try another approach.
Core Concept
Backtracking works by:- Choosing a possible solution or move
- Trying to solve the problem using that choice
- If it doesn’t work, backtrack and try a different choice
- Continue until the problem is solved or all options are exhausted
Problem: Rat in a Maze
Problem Description
Problem Description
A maze is given as an
n × n binary matrix of blocks where:- Source: Upper left
maze[0][0] - Destination: Lower right
maze[n-1][n-1] 0means blocked (dead end)1means open (can be part of path)
Backtracking Strategy
Backtracking Strategy
The backtracking approach:
- Start at
[0, 0] - Mark current position as part of solution path
- Try moving right, then down
- If either move leads to destination, return true
- If both moves fail, backtrack: unmark current position
- Return false
The key insight: if we hit a dead end, we undo our last move and try a different direction. This is the “backtracking” step.
Implementation
Implementation
Step-by-Step Walkthrough
Step-by-Step Walkthrough
Let’s trace the algorithm for the 4×4 maze:
Complexity Analysis
Complexity Analysis
- Time Complexity: O(2^(n²))
- In worst case, we explore all possible paths
- At each cell, we have up to 2 choices (right or down)
- With pruning, actual complexity is often much better
- Space Complexity: O(n²)
- For the solution matrix
- Plus O(n²) for recursion stack in worst case
Extended Version: All Four Directions
Extended Version: All Four Directions
For a more general maze where the rat can move in all four directions:
When to Use Backtracking
Use When
- Need to find all solutions
- Constraint satisfaction problems
- Combinatorial problems
- No greedy/DP solution exists
- Problem involves choices/decisions
- Can detect invalid paths early
Avoid When
- Simple iterative solution exists
- Greedy approach works
- DP is more efficient
- Search space is too large
- Need optimal solution quickly
Classic Backtracking Problems
N-Queens
N-Queens
Place N queens on an N×N chessboard so no two queens attack each other.
Sudoku Solver
Sudoku Solver
Fill a 9×9 Sudoku grid following the rules.
Subset Sum
Subset Sum
Find if there’s a subset that sums to a target value.
Generate Permutations
Generate Permutations
Generate all permutations of an array.
Word Search
Word Search
Find if a word exists in a 2D board.
Backtracking Template
Optimization Techniques
Tips and Best Practices
Common Pitfalls
Forgetting to Backtrack
Forgetting to Backtrack
Modifying Shared State
Modifying Shared State
Not Checking Validity Early
Not Checking Validity Early
Complexity Considerations
| Problem Type | Time Complexity | Space Complexity |
|---|---|---|
| N-Queens | O(N!) | O(N) |
| Sudoku | O(9^(n²)) | O(n²) |
| Permutations | O(N × N!) | O(N) |
| Subsets | O(N × 2^N) | O(N) |
| Combinations | O(N × C(N,K)) | O(K) |
With good pruning, practical performance is often much better than worst-case complexity suggests.
Related Topics
Dynamic Programming
For overlapping subproblems
Divide and Conquer
For independent subproblems
Greedy Algorithms
When local choices suffice