0% found this document useful (0 votes)
11 views12 pages

2.4 knapsack-DP

Uploaded by

2021tuyuzhao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

2.4 knapsack-DP

Uploaded by

2021tuyuzhao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Discrete

Optimization
The Knapsack Problem:
Dynamic Programming
Goals of the Lecture
‣ How to find the BEST knapsack solutions
‣ Using dynamic programming

2
Dynamic Programming
‣ Widely used optimization technique
– for certain classes of problems
– heavily used in computational biology

‣ Basic principle
– divide and conquer
– bottom up computation

3
Dynamic Programming
‣ Basic conventions and notations
– assume that I = {1,2,...,n}
– O(k,j) denotes the optimal solution to the
knapsack problem with capacity k and
items [1..j]
P
maximize i21..j v i x i
subject to P
i21..j w i x i  k
xi 2 {0, 1} (i 2 1..j)
‣ We are interested in finding out the best
value O(K,n)

4
Recurrence Relations (Bellman Equations)
‣ Assume that we know how to solve
• O(k,j-1) for all k in 0..K
‣ We want to solve O(k,j)
• We are just considering one more item, i.e., item j.
‣ If wj ≤ k, there are two cases
• Either we do not select item j, then the best solution we
can obtain is O(k,j-1)
• Or we select item j and the best solution is vj + O(k-wj,j-1)
‣ In summary
• O(k,j) = max(O(k,j-1), vj + O(k-wj,j-1)) if wj ≤ k
• O(k,j) = O(k,j-1) otherwise
‣ Of course
• O(k,0) = 0 for all k

5
Recurrence Relations
‣ We can write a simple program
int O(int k,int j) {
if (j == 0)
return 0;
else if (wj <= k)
return max(O(k,j-1),vj + O(k-wj,j-1));
else
return O(k,j-1)
}

‣ How efficient is this approach?

6
Recurrence Relations - Fibonacci Numbers
‣ We can write a simple program for finding
fibonacci numbers
int fib(int n) {
if (n == 0 || n == 1)
return 1;
else
return fib(n-2) + fib(n-1);
}

‣ How efficient is this approach?


– we are solving many times the same subproblem
• fib(n-1) requires fib(n-2) which we have already solved
• fib(n-3) requires fib(n-4) which we have already solved

7
Dynamic Programming
‣ Compute the recursive equations bottom up
– start with zero items
– continue with one item
– then two items
– ...
– then all items

maximize 5x1 + 6x2 + 3x3


subject to
4x1 + 5x2 + 2x3  9
xi 2 {0, 1} (i 2 1..3)

8
Dynamic Programming - Example
‣ How to find which items to select?
Capacity 0 1 2 3
0 0 0 0 0
1 0 0 0 0
2 0 0 0 3
3 0 0 0 3
4 0 5 5 5
5 0 5 6 6
6 0 5 6 8
7 0 5 6 9
8 0 5 6 9
9 0 5 11 11
v1 = 5 v2 = 6 v3 = 3
Take items 1 and 2 Trace back
w1 = 4 w2 = 5 w3 = 2
9
Dynamic Programming - Example
maximize 16x1 + 19x2 + 23x3 + 28x4
subject to 2x1 + 3x2 + 4x3 + 5x4  7
xi 2 {0, 1} (i 2 1..4)
x1 = 1, x2 = 0, x3 = 0, x4 = 1
Capacity 0 1 2 3 4
0 0 0 0 0 0
1 0 0 0 0 0
2 0 16 16 16 16
3 0 16 19 19 19
4 0 16 19 23 23
5 0 16 35 35 35
6 0 16 35 39 39
7 0 16 35 42 44

10
Dynamic Programming
‣ What is the complexity of this algorithm?
– time to fill the table
– i.e., O(K n)

‣ Is this polynomial?
– How many bits does K need to be
represented on a computer?
• log(K) bits
– Hence the algorithm is in fact
exponential in terms of the input size
• pseudo-polynomial algorithm
• “efficient” when K is small

11
Until Next Time

12

You might also like