Download presentation
Presentation is loading. Please wait.
Published byLisa Jenssen Modified over 5 years ago
1
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).
2
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; End algorithm. Driver: Unknown(1, A[1..3]) What does the algorithm do?
3
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; 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, -]
4
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?
5
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 End algorithm. Driver: {Global optP=0; Global optA=null; BTKS(1, A)} IS THIS ALGORITHM OK? WHAT IS THE OUTPUT HERE?
6
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 End Algorithm. Driver: {BTKS(1, A)} IS THIS CORRECT ALGORITHM?
7
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 End Algorithm. Driver: {Global optP=0; Global optA=null; BTKS(1, A)}
8
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_wt M) then // pruning branches End Algorithm. Driver: {Global optP=0; Global optA=null; BTKS(1, A)}
9
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; End algorithm. RKS is the Rational knapsack greedy algorithm (later), used for a “bounding function” here.
10
Example of Pruning OptP = 56 X X X
O1=(5Kg, $3) O2=(3Kg, $30) O3=(8Kg, $56) M=8Kg OptP = 56 [ ] O1: Yes O1: No (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) O2: Yes OptP is still 33 B =65 > OptP 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 O3: No X B =56, but OptP now is 56 B == OptP X X (w=3+8, ---) pruned (w=3, p=30) OptP=33, not updated (w=8, p=33) OptP=33, updated (w=8, p=56) OptP=56, updated (w=8+8, ---) pruned Pruned
11
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)
12
Problem 2: Adversarial Games
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 X X O Computer X Evaluated best move value = 1 (win)
13
Adversarial Games: Min-Max algorithm
Computer move X 9 options Adversary move 8 options X O X O O X 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
14
Maximizing part of the Min-Max algorithm:
Input: A board position; Output: Best next move, with max evaluation value Function findCompMove( ) if( fullBoard( ) ) value = DRAW; else if( ( quickWinInfo = immediateCompWin( ) ) != null ) return quickWinInfo; // if the next move ends the game; recursion 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; return new Movelnfo( bestMove, value );
15
Minimizing part of the Min-Max algorithm:
Input: A board position; Output: Best opponent move, with min evaluation value Function findHumanMove( ) if( fullBoard( ) ) value = DRAW; else if( ( quickWinInfo = immediateHumanWin( ) ) != null ) return quickWinInfo; // if the next move ends the game; recursion termination else value = COMP_WIN; // initialize with lowest value for( i = 1; i ≤ 9; i++ ) // Try each square in tic-tac-toe if ( isEmpty( i) ) place( i, HUMAN ); // temporary placement on board, // as global variable responseValue = findCompMove( ).value; unplace( i ); // Restore board: alg does not actually move if( responseValue < value ) // Update best move value = responseValue; bestMove = i; return new Movelnfo( bestMove, value ); Driver call?
16
But will not return >40, so
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
17
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
18
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.
19
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).
20
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].
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.