Authored by Tony Feng
Created on April 4th, 2022
Last Modified on April 4th, 2022
Understanding Backtracking
Intro
- A backtracking algorithm is a problem-solving algorithm that uses a brute-force approach for finding the desired output.
- The term backtracking suggests that if the current solution is not suitable, then go back and try other solutions.
- Backtracking uses recursion to discover all of the possibilities until we get the best end result for the problem.
- State Space Tree
- A space state tree is a tree representing all the possible states (solution or nonsolution) of the problem from the root as an initial state to the leaf as a terminal state.
- In combinatorial search problems, search space is in the shape of a tree.
Types of Problems
- Decision Problem: Search for a feasible solution
- Optimization Problem: Search for the best solution
- Enumeration Problem: Find all feasible soutions
Template
|
|
Examples
Leetcode 46 Permutations
|
|
Leetcode 77 Combinations
|
|
Leetcode 78 Subsets
|
|
Leetcode 22 Generate Parentheses
|
|
Reference
- 手把手带你刷Leetcode力扣 - 回溯法 Backtracking
- 手把手带你刷Leetcode力扣 - Leetcode 22
- 手把手带你刷Leetcode力扣 - Leetcode 78
- Backtracking in Python
- Backtracking Template