Backtracking
Rationale of the Backtracking Algorithms
-
A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the examination of all or some of the candidates, declare the identified answer
-
If the list is finite and it is possible to identify the answer following the examinations. And, if there are not too many candidates
-
Backtracking enables us to eliminate the explicit examination of a large subset of the candidates while still guaranteeing that the answer will be found if the algorithm is run to termination
-
pruning(剪枝)
-
The basic idea is that suppose we have a partial solution where each for . First we add and check if satisfies the constrains. If the answer is “yes” we continue to add the next x, else we delete and backtrack to the previous partial solution
Eight Queens
- Find a placement of 8 queens on an chessboard such that no two queens attack
- Two queens are said to attack if they are in the same row, column, diagonal, or antidiagonal of the chessboard
-
Constraints
-
for
Note: This implies candidates in the solution space
-
if
Note: This implies that the solution must be a permutation of . Thus the number of candidates in the solution space is reduced to
-
-
-
For the problem with queens, there are candidates in the solution space
-
Method: Take the problem of 4 queens as an example
-
Step1: Construct a game tree
Each path from the root to a leaf defines an element of the solution space
-
Step2: Each path from the root to a leaf defines an element of the solution space
Note: No tree is actually constructed. The game tree is just an abstract concept
-
The Turnpike Reconstruction Problem
- Given points on the x-axis with coordinates . Assume that . There are distances between every pair of points
- Given distances. Reconstruct a point set from the distances
1 | bool Reconstruct (DistType X[ ], DistSet D, int N, int left, int right) |
A Template
1 | bool Backtracking (int i) |
-
回溯的效率跟S的规模、约束函数的复杂性、满足约束条件的结点数相关
-
约束函数决定了剪枝的效率,但是如果函数本身太复杂也未必合算
-
满足约束条件的结点数最难估计,使得复杂度分析很难完成
-
When different ’s have different sizes
Smaller first so we can cut a larger subtree if one doesn’t work
Tic-tac-toe
- The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
Minimax Strategy
-
Use an evaluation function to quantify the “goodness” of a position
where is the number of potential wins at position
-
The human is trying to minimize the value of the position , while the computer is trying to maximize it
pruning
pruning
- pruning
- When both pruning and pruning techniques are combined
- In practice, it limits the searching to only nodes, where is the size of the full game tree