Home
DSA Visualizer
Backtracking
Backtracking Introduction
Backtracking Introduction
Backtracking is a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. It incrementally builds candidates to solutions and abandons a candidate ('backtracks') as soon as it determines that the candidate cannot possibly be completed to a valid solution.
READY
Depth
0
Three moves, over and over: choose, check, un-choose.
Next
Nothing chosen yet. Two options lead out of Start.
Intuition
Imagine you are in a maze. You embrace a path and keep walking. If you hit a dead end, you turn back (backtrack) to the last junction and try a different path. You repeat this until you find the exit.
This 'try and retreat' strategy is the core of backtracking. It is essentially an optimized exhaustice search.
Concept
Core Principles:
- Choice: Make a choice from the available options.
- Constraint: Check if the choice is valid.
- Goal: Check if we reached the solution.
- Backtrack: If the choice leads to a dead end, undo the choice and try the next option.
Recursion Tree:
Backtracking algorithms are often visualized as a tree traversal where the root is the initial state, and branches represent choices. Pruning branches that don't lead to a solution optimizes the search.
How it Works
Backtracking algorithms are often visualized as a State Space Tree.
- Root: Initial state.
- Branches: Possible choices.
- Nodes: Partial solutions.
- Leaf: Final valid solution or dead end.
The algorithm traverses this tree using Depth-First Search (DFS). When a node violates a constraint, the algorithm 'prunes' the subtree rooted at that node (stops exploring it).
Step-by-Step Breakdown
Generic Algorithm:
- Start with an empty solution.
- Extend the partial solution by adding an element from the candidate set.
- Check if the partial solution is valid:
- Yes: If it's a complete solution, return it. Else, recurse.
- No: Discard the extension (backtrack).
- If all candidates are tried and none work, return failure.
When to Use
Advantages:
- Guaranteed to find a solution if one exists.
- Can find all possible solutions (e.g., all N-Queens arrangements).
- More efficient than brute force due to pruning.
Use Cases:
- Constraint Satisfaction Problems (Sudoku, N-Queens).
- Combinatorial Optimization (Knapsack, Traveling Salesman).
- Generating all permutations or subsets.
When NOT to Use
Disadvantages:
- Exponential time complexity in worst case (e.g., O(2^N) or O(N!)).
- Can be slow for large inputs.
- Recursive implementation can lead to stack overflow for deep trees.
Avoid When:
- When an optimal solution can be found using greedy or dynamic programming (which are faster).
How to Identify
Keywords: "Find all solutions", "Generate all...", "Possible combinations", "Constraint satisfaction".
Sample Problems
Preparing for interviews? This topic is also taught as a pattern:
Introduction to Backtracking
Applying for software engineering roles? Free, no sign-up:
