Concept
The backtracking algorithm is actually an enumeration-like search attempt process, which is mainly to find a solution to the problem during the search attempt. When it is found that the solution conditions have not been met, it “backtracks” and try another path.
The backtracking method is a search method for optimal selection. It searches forward according to the optimal conditions to achieve the goal. However, when exploring a certain step, if it is found that the original choice is not good or does not reach the goal, it goes back one step and chooses again, which is called “backtrack point”.
Many complex and large-scale problems can use the backtracking method. So it is known as the “universal problem solving method”.
Basic idea
In the solution space which contains all the solutions to the problem, the solution space is explored from the root node according to the depth-first search
strategy. When a node is explored, first determine whether the node contains a solution to the problem. If it does, continue to explore from that node. If the node does not contain a solution to the problem, go back to its ancestor layers. (in fact, the backtracking method is a depth-first search
algorithm for implicit graphs).
If using the backtracking method to find all the solutions to the problem, you must backtrack to the root, and all feasible subtrees of the root node must have been searched through before ending.
If you use the backtracking method to find any solution, you can end as long as you find a solution to the problem.
General steps to solve a problem using backtracking
Determine the solution space for the given problem. The solution space of the problem should be clearly defined. The solution space of the problem should contain at least one (optimal) solution of the problem.
Determine search rules for nodes
Search the solution space in a
depth-first
manner, and use pruning functions to avoid invalid searches during the search.
Algorithm framework
- Problem framework
1 | Suppose the solution of the problem is an n-dimensional vector(a1, a2, ...... an), and the constraint condition is that ai(i = 1,2,3, ....., n) satisfies a certain condition and is denoted as f(ai). |
- Non-recursive backtracking framework
1 | int a[n],i; |
- Recursive backtracking framework
The backtracking method is adepth-first search
of the solution space. In general, it is relatively simple to use a recursive function to implement the backtracking method, wherei
is the depth of the search, and the framework is as follows:
1 | int a[n]; |