

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


COSC242 Lecture 23 Slide 1
Dynamic programming
Dynamic programming often produces a polynomial- time algorithm for finding the optimal solution when brute force enumeration of possibilities would be exponential.
COSC242 Lecture 23 Slide 2
Given: a set S = {s 1 , s 2 , …, s n } of n items where each s i has integer value vi and integer weight wi. Required: to choose a subset O of S such that the total weight of the items chosen does not exceed W and the sum of the values vi of items in O is maximal. How can we break the global problem down into subproblems in a way that gives subproblem optimality? Suppose the optimal solution for S and W is a subset O in which s k is the highest numbered item. Then O-{s k } is an optimal solution for S k -1 = {s 1 , …, s k -1} and total weight W- wk. And the value of the global solution O is vk plus the value of the subproblem solution. Given a target weight w and a set S k = {s 1 , …, s k } imagine examining all the subsets of S k whose total weight is £ w. Some of these subsets might have bigger total values than others. Let V[ k , w ] be the biggest total value of such a subset of S k. Now we give a recursive definition of V[ k , w ]. V [k, w ] = 0 if either k = 0 or w = 0, otherwise V [ k , w ] = if wk > w then V[ k -1, w ] else max{V[ k -1, w ], vk + V[ k -1, w - wk ] }
COSC242 Lecture 23 Slide 3
The recursive definition of V [ k , w ] says that the value of a solution for stage S k and target weight w either
The thief can solve the problem by considering the total set S n and weight W deriving its solution from that of the subproblems S n -1 and weight W- wn or S n - and weight W. These subproblems in turn …
A t each stage S k ,there are two choices to be compared and the better one made:
COSC242 Lecture 23 Slide 4
Inputs = max weight W, item values ( v 1 , v 2 , …, vn ) and weights ( w 1 , w 2 , …, wn ). We can solve the problem by recursively computing V [ k , w ]. But to avoid subproblem overlap dynamic programming uses a bottom-up table — The values V[ k , w ] are stored in a table V[0.. n , 0..W] whose entries are computed in row-major order (i.e. the first row of V is filled in from left to right, then the second row, and so on). This bottom-up approach ensures that subproblems are solved once only. At the end of the computation, V[ n , W] contains the maximum value the thief can take. Tracing the choices — the subset of items to take can be recovered from the table V by starting at V[ n , W] and tracing where the optimal values came from. If V[ k , w ] = V[ k -1, w ] then item s k is not part of the solution and we continue tracing with V[ k -1, w ]. Otherwise item s k is part of the solution and we continue tracing with V[ k -1, w - wk ].
COSC242 Lecture 23 Slide 5
Dynamic_01_knapsack( n , W, v 1 , …, vn , w 1 , …, wn )
The algorithm describes how the table is filled in.
It doesn’t describe how to trace the choices.
COSC242 Lecture 23 Slide 6
Apply the algorithm to solve the 0-1 knapsack problem: s 1 : v 1 = 60 w 1 = 10 s 2 : v 2 = 100 w 2 = 20 s 3 : v 3 = 120 w 3 = 30
3
2
1
0
w 0 10 20 30 40 50 k