200 likes | 397 Views
ALGORITHM TYPES. Greedy, Divide and Conquer, Dynamic Programming, and Backtracking . Note the general strategy from the examples. The classification is neither exhaustive (there may be more) nor mutually exclusive (one may combine). Backtracking Algorithms.
E N D
ALGORITHM TYPES Greedy, Divide and Conquer, Dynamic Programming, and Backtracking. 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[]) if (level == 4) then print A; else A[level] =0; Unknown(level+1, A); A[level] =1; 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) if (level == 4) then print A; else A[level] =0; Unknown(level+1, A); A[level] =1; 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. A[-, -, -] A[0, -, -] A[1, -, -] A[0, 0, -] A[0, 1, -] A[1, 0, -] A[1, 1, -] . . . . . . . . . . .
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? 0-1 Knapsack Problem (not in the book)
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) if (level = = n+1) then total_profit= i(A[i]*P[i]); // computing profit on each leaf else A[level] =0; //level-th object excluded from the bag BTKS(level+1, A); A[level] =1; // the object is included 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) if (level = = n+1) then total_profit= i(A[i]*P[i]); if total_profit is > optP then // optimizing optP = total_profit; optA = A; else A[level] =0; //level-th object excluded from the bag BTKS(level+1, A); A[level] =1; // the object is included 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) if (level = = n+1) then total_profit= i(A[i]*P[i]); total_weight= i(A[i]*W[i]); if total_weight is <= M &total_profit is > optP then // constraint optP = total_profit; optA = A; else A[level] =0; //level-th object excluded from the bag BTKS(level+1, A); A[level] =1; // the object is included 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) if (l = = n+1) then total_profit= i(A[i]*P[i]); if total_profit is > optP then optP = total_profit; optA = A; else A[l] =0; //level-th object excluded from the bag BTKS(l+1, A); A[l] =1; // the object is included total_wt= i=1l (A[i]*W[i]); if (total_wtM) then // pruning branches 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 • if (l = = n+1) then • <<same as before>> • else • B=i=1l-1(A[i]*P[i]) +RKS(A, l-1, M- i=1l-1(A[i]*W[i])); • if (B>optP) then//only if we have a chance for better profit • A[l] =1; • if (total_wt M) then • BTKS_Prun(l+1, A); • if (B>optP) then //optP may have increased by now • A[l] =0; • 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 B=0 +(30 + (5/8)56, for m=8 from O2, O3)=65 > 33 (w=5, p=3) OptP=0 B=3 +(30,for m=3 from O2, O3) OptP is still 33 B =65 > OptP O2: Yes B =33, B == OptP X (w=5+3, p=33) OptP=0 B=33 +(0,for m=0 from O3) (w=3, p=30) OptP=33 B=30 + ((5/8)56, for m=5 , frm O3)=65 > 33 (w=0, p=0) OptP=33 B=0 + ((8/8)56, for m=8, frm O3)=56 > 33 Pruned because OptP cannot be improved O3: Yes X O3: No B =56, but OptP now is 56 B > OptP X (w=3, p=30) OptP=33, not updated (w=3+8, ---) pruned (w=8, p=33) OptP=33, updated (w=8, p=56) OptP=56, updated (w=8+8, ---) pruned (w=0, p=0) OptP=56, not updated
Bounding function for pruning • BF is a guessing function: so that we can prun branches from unnecessarily expanding • Ideal value is = the real, at every level (impossible to have such a BF) • Should evaluate to real value for maximization-problems, or real value for minimization-problems (correctness criterion) • Should be as close to the real 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 X O O X X O • Between 2 adversaries • 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 Computer X Evaluated best move value = 1 (win)
Adversarial Games: Min-Max algorithm Computer move X X X O O O O 9 options X X X X O O Adversary move 8 options X O X O Leaf: return +1 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: Function findCompMove( ) { if( fullBoard( ) ) value = DRAW; else if( ( quickWinInfo = immediateCompWin( ) ) != null ) return quickWinInfo; // if the next move ends the game; rec termination else { value = COMP_LOSS; // initialize with lowest value for( i = 1; i <= 9; i++ ) { // Try each square in tic-tac-toe if ( isEmpty( i) ) { place( i, COMP ); // temporary placement, // on board as global variable responseValue = findHumanMove( ).value; unplace( i ); } // Restore board: alg does not actually move if( responseValue > value ) { // Update best move value = responseValue; bestMove = i; } } } } } 16. return new Movelnfo( bestMove, value );
Minimizing part of the Min-Max algorithm: Function findHumanMove( ) { if ( fullBoard( ) ) value = DRAW; else if( ( quickWinInfo = immediateHumanWin( ) ) != null ) return quickWinInfo; el se { value = COMP-WIN; for( i = 1; i <= 9; i++ ) { // Try each square if( isEmpty( i) ) { place( i, HUMAN) ; responseValue = findCompMove( ).value; unplace( i ); // Restore board if( responseValue < value ) { // Update best move value = responseValue; bestMove = i; } } } } // end for } // end else 15. return new MoveInfo( 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 pruned 60 40 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
Given N exits (on a turnpike), and M=N(N-1)/2 number of distances between each pair of them, put the exits on a line with the first one being at zero. Input: D={1,2,2,2,3,3,3,4,5,5,5,6,7,8,10}, the sorted gap values Here, M=|D|=15, so, calculated N=6, obviously. [DRAW TREE, UPDATE D, page 407.] 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. Problem 3: 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. BT 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 D. The algorithm terminates here. Note that if backtracking leads up to the first choice and fails even there, then there is no solution (inconsistent) for the problem! [READ ALGORITHM on p443-4]. Turnpike Reconstruction Problem