Backtracking
Download
Report
Transcript Backtracking
ALGORITHM TYPES
• Divide and Conquer, Dynamic Programming,
Backtracking, and Greedy.
• Note the general strategy from the examples.
• The classification is neither exhaustive (there may be
more) nor mutually exclusive (one may combine).
Backtracking Algorithms
Input: an empty array of size n (or 4) and a starting level index
Output: Prints A
Algorithm Unknown(level, array A[])
1. if (level == 4) then print A;
2. else
1. A[level] =0;
2. Unknown(level+1, A);
3. A[level] =1;
4. Unknown(level+1, A);
End algorithm.
Driver: Unknown(1, A[1..3])
What does the algorithm do?
Backtracking Algorithms
Input: an empty array of size n (or 4) and a starting level index (=1)
Output: Prints A
Algorithm Unknown(level, array A)
A[-, -, -]
1. if (level == 4) then print A;
2. else
A[0, -, -]
A[1, -, -]
1. A[level] =0;
2. Unknown(level+1, A);
A[0, 0, -] A[0, 1, -] A[1, 0, -] A[1, 1, -]
3. A[level] =1;
...........
4. Unknown(level+1, A);
End algorithm.
•The algorithm prints bit strings:
• (000),(001),(010),(011),...,
• when called with driver Unknown(1, A).
•Draw recursion tree: this is an exhaustive Backtracking (BT) algorithm.
0-1 Knapsack Problem (not in the book)
• N objects: (weight, profit); M=Knapsack limit by wt
– {(5 lbs, 10 $), (3, 6), (10, 5), (2, 8)}, M = 9 lbs
• Maximize profit, without over-packing
– OptP = 18 $, OptA = {O1, O4}
• Exhaustive search over each knapsack content: hard
exponential
– Subset of objects. How many subsets?
BT: 0-1 Knapsack Problem
Input: Set of n objects (w[i], p[i]), and knapsack limit=M
Output: Total profits for all possible subsets
1.Algorithm BTKS(level, array A)
2. if (level = = n+1) then
3.
total_profit = i(A[i]*P[i]); // computing profit on each leaf
else
4. A[level] =0; // level-th object excluded from the bag
5. BTKS(level+1, A);
6. A[level] =1; // the object is included
7. BTKS(level+1, A);
End algorithm.
Driver: {Global optP=0; Global optA=null; BTKS(1, A)}
IS THIS ALGORITHM OK?
WHAT IS THE OUTPUT HERE?
BT: 0-1 Knapsack Problem
Input: Set of n objects (w[i], p[i]), and knapsack limit=M
Output: Total profits for all possible subsets
Algorithm BTKS(level, array A)
1. if (level = = n+1) then
2.
total_profit = i(A[i]*P[i]);
3.
if total_profit is > optP then
// optimizing
4.
optP = total_profit;
5.
optA = A;
else
6. A[level] =0; // level-th object excluded from the bag
7. BTKS(level+1, A);
8. A[level] =1; // the object is included
9. BTKS(level+1, A);
End Algorithm.
Driver: {BTKS(1, A)}
IS THIS CORRECT ALGORITHM?
BT: 0-1 Knapsack Problem
Input: Set of n objects (w[i], p[i]), and knapsack limit=M
Output: Total profits for all possible subsets
Algorithm BTKS(level, array A)
1. if (level = = n+1) then
2.
total_profit = i(A[i]*P[i]);
3.
total_weight = i(A[i]*W[i]);
4.
if total_weight is ≤ M & total_profit is > optP then // constraint
5.
optP = total_profit;
6.
optA = A;
1.
else
2. A[level] =0; // level-th object excluded from the bag
3. BTKS(level+1, A);
4. A[level] =1; // the object is included
5. BTKS(level+1, A);
End Algorithm.
Driver: {Global optP=0; Global optA=null; BTKS(1, A)}
BT: 0-1 Knapsack Problem
Input: Set of n objects (w[i], p[i]), and knapsack limit=M
Output: Total profits for all possible subsets
Algorithm BTKS(l, array A)
1. if (l = = n+1) then
2.
total_profit = i(A[i]*P[i]);
3.
if total_profit is > optP then
4.
optP = total_profit;
5.
optA = A;
1.
else
2. A[l] =0; // level-th object excluded from the bag
3. BTKS(l+1, A);
4. A[l] =1; // the object is included
5. total_wt = i=1l (A[i]*W[i]);
6. if (total_wt M) then
// pruning branches
7.
BTKS(l+1, A);
End Algorithm.
Driver: {Global optP=0; Global optA=null; BTKS(1, A)}
BT with further pruning: 0-1 Knapsack Problem
Algorithm BTKS_Prun(l, A) // global optP and global optX
1. if (l = = n+1) then
1. <<same as before>>
2. else
3.
B=i=1l-1 (A[i]*P[i]) +RKS(A, l-1, M- i=1l-1 (A[i]*W[i]));
4.
if (B>optP) then //only if we have a chance for better profit
1.
A[l] =1;
2.
if (total_wt M) then
1.
BTKS_Prun(l+1, A);
3.
if (B>optP) then //optP may have increased by now
4.
A[l] =0;
5.
BTKS_Prun(l+1, A);
End algorithm.
RKS is the Rational knapsack greedy algorithm (later),
used for a “bounding function” here.
Example of Pruning
O1=(5Kg, $3) O2=(3Kg, $30) O3=(8Kg, $56) M=8Kg
OptP = 56
[- - - ]
O1: No
O1: Yes
(w=0, p=0)
OptP=33
(w=5, p=3)
OptP=0
B=3 +(30, for m=3 from O2, O3)
B=0 +(30 + (5/8)56, for m=8 from O2, O3)=65 > 33
OptP is still 33
B =65 > OptP
O2: Yes
(w=5+3, p=33)
OptP=0
B=33 +(0, for m=0 from O3)
O3: Yes
O3: No
X
(w=8+8, ---)
pruned
(w=8, p=33)
OptP=33, updated
B =33,
B == OptP
(w=3, p=30)
OptP=33
X
B=30 + ((5/8)56, for m=5 , frm O3)=65 > 33
Pruned because
OptP cannot be improved
X
(w=3+8, ---)
pruned
(w=0, p=0)
OptP=33
B=0 + ((8/8)56, for m=8, frm O3)=56 > 33
B =56, but OptP now is 56
B == OptP
X
(w=3, p=30)
(w=8,
p=56)
OptP=33, not updated
OptP=56, updated
Pruned
Bounding function (BF) for pruning
• BF is a guessing function: to prun branches from
unnecessary expansion
• Ideal value = actual, at every level (impossible to have such
a BF)
• Should be actual value for maximization-problems, or
actual value for minimization-problems (correctness criterion)
• Should be as close to the actual value as possible (goodness,
for pruning branches of the recursion tree)
• Should be fast to calculate (computation overhead on each
step)
Problem 2: Adversarial Games
•
Between 2 adversaries
O X
O
X X
O
Computer X
Evaluated best move value = 1 (win)
• Input: A board position
• Output: Best move (actually, the score for moving there)
• Objective function:
• board position evaluated to a number
• higher the number better the move
• Task: To compute optimum value of objective function
out of all possible legal moves
– i.e. Optimize for the highest board value
Adversarial Games: Min-Max algorithm
Computer move
X
9 options
Adversary move
8 options
O X O
X X O
O X
Leaf: return +1
•
•
•
•
O X O
X X O
X
O
Leaf: return -1
Search tree is going up to leaves (terminal board) to evaluate board
May not be practically feasible for a given computation time
– 9! total worst case
Most algorithms will search for a pre-assigned depth or “ply” & evaluate board
Alternate move by Adversary/human & Algorithm/computer:
•
Minimizes the objective function for adversary move
•
Maximizes the objective function for self-move or computer move
• the same algorithm alternately calls maximizer and minimizer
Maximizing part of the Min-Max algorithm:
Input: A board position;
Function findCompMove( )
Output: Best next move, with max evaluation value
1. if( fullBoard( ) )
2.
value = DRAW;
3. else if( ( quickWinInfo = immediateCompWin( ) ) != null )
4.
return quickWinInfo; // if the next move ends the game; recursion termination
5. else
6.
value = COMP_LOSS; // initialize with lowest value
7.
for( i = 1; i ≤ 9; i++ ) // Try each square in tic-tac-toe
8.
if ( isEmpty( i) )
9.
place( i, COMP ); // temporary placement on board,
10.
// as global variable
11.
responseValue = findHumanMove( ).value;
12.
unplace( i ); // Restore board: alg does not actually move
13.
if( responseValue > value )
// Update best move
14.
value = responseValue;
15.
bestMove = i;
16. return new Movelnfo( bestMove, value );
Minimizing part of the Min-Max algorithm:
Input: A board position;
Output: Best opponent move, with min evaluation value
Function findHumanMove( )
1. if( fullBoard( ) )
2.
value = DRAW;
3. else if( ( quickWinInfo = immediateHumanWin( ) ) != null )
4.
return quickWinInfo; // if the next move ends the game; recursion termination
5. else
6.
value = COMP_WIN; // initialize with lowest value
7.
for( i = 1; i ≤ 9; i++ ) // Try each square in tic-tac-toe
8.
if ( isEmpty( i) )
9.
place( i, HUMAN ); // temporary placement on board,
10.
// as global variable
11.
responseValue = findCompMove( ).value;
12.
unplace( i ); // Restore board: alg does not actually move
13.
if( responseValue < value )
// Update best move
14.
value = responseValue;
15.
bestMove = i;
16. return new Movelnfo( bestMove, value );
Driver call?
Alpha-beta pruning
Max
[- ] a=44
Max: get me >44
Min
Must get >44
44
But will not return >40, so
Do not call
60>40, so
call
60
40
pruned
All branches pruned
Min-max algorithm with Alpha-beta pruning
Alpha pruning
Must be true:
Max-node’s value ≥
min-node’s value
Otherwise,
useless to expand tree:
Prun the branch
Beta pruning
From Weiss’ text
Problem 3: Turnpike Reconstruction Problem
• Input: M number of distance values between each pair of N exists
on a turnpike, M=N(N-1)/2
Output: assign N-1 exit-points on a line with the 1st exit being at 0.
• Input: D={1,2,2,2,3,3,3,4,5,5,5,6,7,8,10}, note already sorted gap
values
• M=|D|=15, so, N=6, obviously. [DRAW TREE, UPDATE D, page 407, Weiss.]
• x1=0 given, so, x6=10, the largest in D.
D={1,2,2,2,3,3,3,4,5,5,5,6,7,8,} now.
• Largest now is 8. So, either x2=2, or x5=8. WHY?
• In case x2=2, you can inverse direction and make x2 as x5, and then
x5 would be =8 (prune this branch for symmetry).
• So, x5=8 is a unique choice. x6-x5=2, and x5-x1=8 are taken off D.
• 7 is now largest. So, either x4=7, or x2=3. We have two branches
now, for these two options.
Turnpike Reconstruction Problem
• For x4=7, we should have in D: x5-x4=1, and x6-x4=3. Take them
off.
• Next largest is 6. So, either x3=6 or x2=4 (so that x6-x2=6).
• For x3=6, x4-x3=1. There is no more 1 in D, so, this is impossible.
• Backtrack to x2=4 (instead of x3=6), x2-x0=4 and x5-x2=4. We don't
have those many 4's left. So, this is impossible also.
• So, we backtrack past the choice x4=7, because it has no solution.
Rather, we reassign x2=3 (Note. x4 is no longer assigned :
backtracked).
Turnpike Reconstruction Problem
•
•
•
•
•
For x2=3, we take off 3,5,7 from D. Thus, D={1,2,2,3,3,4,5,5,6}
Now, either x4=6 or x3=4.
For x3=4, we need two 4’s for x3-x0, and x5-x3.
Backtrack to x4=6, D={1,2,3,5,5}.
There is only one choice left now, i.e., x3=5, which fits all the values
in input D, and D = {Null} now.
• The algorithm terminates here.
• Note: if backtracking leads up to the first choice and fails even
there, then there is no solution (inconsistent) for the problem!
•
[ALGORITHM on p443-4, Weiss].