11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

512 Chap. 16 P<strong>at</strong>terns of <strong>Algorithm</strong>sUnfortun<strong>at</strong>ely, knowing the answer for 163 is of almost no use <strong>at</strong> allwhen solving for 164. One solution is: 9, 54, 101.If you tried solving these examples, you probably found yourself doing a lot oftrial-<strong>and</strong>-error <strong>and</strong> a lot of backtracking. To come up with an algorithm, we wantan organized way to go through the possible subsets. Is there a way to make theproblem smaller, so th<strong>at</strong> we can apply divide <strong>and</strong> conquer? We essentially have twoparts to the input: The knapsack size K <strong>and</strong> the n items. It probably will not do usmuch good to try <strong>and</strong> break the knapsack into pieces <strong>and</strong> solve the sub-pieces (sincewe already saw th<strong>at</strong> knowing the answer for a knapsack of size 163 did nothing tohelp us solve the problem for a knapsack of size 164).So, wh<strong>at</strong> can we say about solving the problem with or without the nth item?This seems to lead to a way to break down the problem. If the nth item is notneeded for a solution (th<strong>at</strong> is, if we can solve the problem with the first n −1 items)then we can also solve the problem when the nth item is available (we just ignoreit). On the other h<strong>and</strong>, if we do include the nth item as a member of the solutionsubset, then we now would need to solve the problem with the first n − 1 items<strong>and</strong> a knapsack of size K − k n (since the nth item is taking up k n space in theknapsack).To organize this process, we can define the problem in terms of two parameters:the knapsack size K <strong>and</strong> the number of items n. Denote a given instance of theproblem as P (n, K). Now we can say th<strong>at</strong> P (n, K) has a solution if <strong>and</strong> only ifthere exists a solution for either P (n − 1, K) or P (n − 1, K − k n ). Th<strong>at</strong> is, we cansolve P (n, K) only if we can solve one of the sub problems where we use or donot use the nth item. Of course, the ordering of the items is arbitrary. We just needto give them some order to keep things straight.Continuing this idea, to solve any subproblem of size n − 1, we need only tosolve two subproblems of size n − 2. And so on, until we are down to only oneitem th<strong>at</strong> either fills the knapsack or not. This n<strong>at</strong>urally leads to a cost expressedby the recurrence rel<strong>at</strong>ion T (n) = 2T (n − 1) + c = Θ(2 n ). Th<strong>at</strong> can be prettyexpensive!But... we should quickly realize th<strong>at</strong> there are only n(K + 1) subproblemsto solve! Clearly, there is the possibility th<strong>at</strong> many subproblems are being solvedrepe<strong>at</strong>edly. This is a n<strong>at</strong>ural opportunity to apply dynamic programming. We simplybuild an array of size n × K + 1 to contain the solutions for all subproblemsP (i, k), 1 ≤ i ≤ n, 0 ≤ k ≤ K.There are two approaches to actually solving the problem. One is to start withour problem of size P (n, K) <strong>and</strong> make recursive calls to solve the subproblems,each time checking the array to see if a subproblem has been solved, <strong>and</strong> fillingin the corresponding cell in the array whenever we get a new subproblem solution.The other is to start filling the array for row 1 (which indic<strong>at</strong>es a successful solution

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!