1.00 Tutorial 10 4/29/2002 PS#9 Corrections on web site • Pruning rule #2 updated (should be “<=“, not “<“) • Diagram corrected 1 PS#9: What's the point? Given (1) a series of stereo components of varying lengths, 2 3 5 10 15 and (2) a pallet of width M, M=20 How many ways can we pack some subset of the stereos, side-by-side, so they they exactly fit on the pallet? PS#9: What's the point? For this example, there are three solutions 2 3 5 10 20 2 3 15 5 20 15 20 But how do we solve this problem in general? 2 PS#9: Tree Algorithm s=Sum of lengths in this solution r=Sum of lengths to be considered 2 3 5 10 15 M=20 x0=1 (0,35) x0=0 2 3 5 10 15 2 3 5 10 15 (2,33) x1=1 2 3 5 10 15 (5,30) x2=1 2 3 5 10 15 x2=0 2 3 5 10 15 (0,33) x1=0 2 3 5 10 15 (2,30) x2=1 2 3 5 10 15 x2=0 2 3 5 10 15 x1=1 x1=0 2 3 5 10 15 (3,30) x2=1 2 3 5 10 15 x2=0 2 3 5 10 15 2 3 5 10 15 (0,30) x2=1 2 3 5 10 15 x2=0 2 3 5 10 15 (10,25) (5,25) (7,25) (2,25) (8,25) (3,25) (2,25) (0,25) plus two more levels PS#9 Tree Algorithm • • • • Enumerates all combinations Grows exponentially: O(2n) Solution found whenever s=M x[ ] is the current solution, takes on values of 0 if length Lk is not in solution, or 1 if it is. • Pruning rules reduce number of nodes by “pruning” subtrees. (Note that our “pruning rules” are actually “subtree generation rules”, since the action taken is to generate nodes, not prune existing ones.) 3 Pruning Rule #1 • If (s + Lk = M), output solution (and don’t follow any subtrees!) M=20 x2=1 Lk=10 2 3 5 10 15 S=10 (10,25) x3=1 x3=0 2 3 5 10 15 Solution! (20,15) x4=1 2 3 5 10 15 (10,15) x4=0 x4=1 x4=0 Prune subtree Note that Lk is the next length to be considered, not the current one. Pruning Rule #2 • If (s + Lk + Lk+1 <= M), generate left child and set xk = 1 (otherwise do not!) Lk=3 M=20 S=2 Lk+1=5 2 3 5 10 15 (2,33) x1=1 2 3 5 10 15 (5,30) 2+3+5 <= 20, so we generate a left child Note that Lk is the next length to be considered, not the current one. 4 Pruning Rule #3 • If (s + r - Lk >= M) && (s + Lk+1 <= M) generate right child and set xk = 0 • Part 1: if we were to generate a right node, then the maximum lengths we could add to our current solution would be r-Lk. If that amount is < M, we can never fill the pallet, so we might as well stop now. • Part 2: If the following length (Lk+1) is greater than the pallet width, we also cannot use it and can stop now as well. Note that Lk is the next length to be considered, not the current one. Exercise • Apply the 3 pruning rules to decide whether or not to generate left & right nodes when M=25, L= {10, 12, 13, 30} (1) For root node (k=0) (2) When k = 2, x={0, 1} (3) When k = 2, x={1, 1} Solution on next slide… 5 Exercise Solution 1. Left and right nodes generated. (Rules 2 & 3) 2. Solution found via rule 1. No more nodes generated 3. Neither right nor left generated. (Rules 2 & 3.) This is a dead end. PS#9 Implementation • Input read from command line (String[ ] argv). E.g., M L DOS> java SubsetTree 20 2 3 5 10 15 • Write a SubsetTree class as your main class. It should create the root node and call root.visit() • Suggested implementation is to build a tree using a Node class. – Each node should carry a solution array (int x[ ]) of size L.length. How should it be initialized? – What other variables/methods will you need? 6 Node’s visit() method • visit() method does all the work: – Implements pruning rules – Creates left & right child nodes, and calls visit recursively (left.visit(), right.visit()) as appropriate – Sets x[i] to 0 or 1 – Prints solutions when found (a printSolution method is handy to have) Other solutions • Many solutions are possible • You don’t even need to create nodes (can use recursion alone to traverse an implicit tree) 7