Lecture 25 Dynamic programing 0 1 Knapsack problem
Lecture 25. Dynamic programing 0 -1 (Knapsack problem) 1
Recap • The focus points of Dynamic programming refer to gaining optimal solution of a problem by using recurrence. • Like greedy algorithm, it is alos used for optimizaton problems. • But in greedy algorithm, recurrence approach s not used. • Several problems can be solved through dynamic programming approach such as solution of binomial series, matrix multiplication and Fibonacci series. 2
Knapsack problem Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W. So we must consider weights of items as well as their values. Item # 1 2 3 3 Weight Value 1 8 3 6 5 5
Knapsack problem There are two versions of the problem: 1. “ 0 -1 knapsack problem” l 2. 4 Items are indivisible; you either take an item or not. Some special instances can be solved with dynamic programming “Fractional knapsack problem” l Items are divisible: you can take any fraction of an item
0 -1 Knapsack problem 5 l Given a knapsack with maximum capacity W, and a set S consisting of n items l Each item i has some weight wi and benefit value bi (all wi and W are integer values) l Problem: How to pack the knapsack to achieve maximum total value of packed items?
0 -1 Knapsack problem l Problem, in other words, is to find The problem is called a “ 0 -1” problem, because each item must be entirely accepted or rejected. 6
0 -1 Knapsack problem: brute-force approach Let’s first solve this problem with a straightforward algorithm l l l 7 Since there are n items, there are 2 n possible combinations of items. We go through all combinations and find the one with maximum value and with total weight less or equal to W Running time will be O(2 n)
0 -1 Knapsack problem: dynamic programming approach 8 l We can do better with an algorithm based on dynamic programming l We need to carefully identify the subproblems
Defining a Subproblem 9 l Given a knapsack with maximum capacity W, and a set S consisting of n items l Each item i has some weight wi and benefit value bi (all wi and W are integer values) l Problem: How to pack the knapsack to achieve maximum total value of packed items?
Defining a Subproblem l We can do better with an algorithm based on dynamic programming l We need to carefully identify the subproblems Let’s try this: If items are labeled 1. . n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, . . k} 10
Defining a Subproblem If items are labeled 1. . n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, . . k} 11 l This is a reasonable subproblem definition. l The question is: can we describe the final solution (Sn ) in terms of subproblems (Sk)? l Unfortunately, we can’t do that.
Defining a Subproblem w 1 =2 w 2 =4 b 1 =3 b 2 =5 w 3 =5 b 3 =8 Max weight: W = 20 For S 4: Total weight: 14 Maximum benefit: 20 w 1 =2 w 2 =4 b 1 =3 b 2 =5 12 w 3 =5 b 3 =8 For S 5: Total weight: 20 Maximum benefit: 26 Weight Benefit w 4 =3 b 4 =4 # ? w 5 =9 b 5 =10 wi bi 1 2 3 2 4 5 3 5 8 4 3 4 5 9 10 Item S 5 S 4 Solution for S 4 is not part of the solution for S 5!!!
Defining a Subproblem l As we have seen, the solution for S 4 is not part of the solution for S 5 l 13 So our definition of a subproblem is flawed and we need another one!
Defining a Subproblem 14 l Given a knapsack with maximum capacity W, and a set S consisting of n items l Each item i has some weight wi and benefit value bi (all wi and W are integer values) l Problem: How to pack the knapsack to achieve maximum total value of packed items?
Defining a Subproblem l Let’s add another parameter: w, which will represent the maximum weight for each subset of items l The subproblem then will be to compute V[k, w], i. e. , to find an optimal solution for Sk = {items labeled 1, 2, . . k} in a knapsack of size w 15
Recursive Formula for subproblems l The subproblem will then be to compute V[k, w], i. e. , to find an optimal solution for Sk = {items labeled 1, 2, . . k} in a knapsack of size w l 16 Assuming knowing V[i, j], where i=0, 1, 2, … k-1, j=0, 1, 2, …w, how to derive V[k, w]?
Recursive Formula for subproblems (continued) Recursive formula for subproblems: It means, that the best subset of Sk that has total weight w is: 1) the best subset of Sk-1 that has total weight w, or 2) the best subset of Sk-1 that has total weight w-wk plus the item k 17
Recursive Formula l l l 18 The best subset of Sk that has the total weight w, either contains item k or not. First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable. Second case: wk w. Then the item k can be in the solution, and we choose the case with greater value.
0 -1 Knapsack Algorithm 19 for w = 0 to W V[0, w] = 0 for i = 1 to n V[i, 0] = 0 for i = 1 to n for w = 0 to W if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w
Running time for w = 0 to W O(W) V[0, w] = 0 for i = 1 to n V[i, 0] = 0 Repeat n times for i = 1 to n O(W) for w = 0 to W < the rest of the code > 20 What is the running time of this algorithm? O(n*W) Remember that the brute-force algorithm takes O(2 n)
Example Let’s run our algorithm on the following data: n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2, 3), (3, 4), (4, 5), (5, 6) 21
Example (Cont !!!) iW 0 0 0 1 2 3 4 1 0 2 0 for w = 0 to W V[0, w] = 0 22 3 0 4 0 5 0
Example (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 2 0 for i = 1 to n V[i, 0] = 0 23 3 0 4 0 5 0
Example (Cont !!!) iW 0 1 2 3 4 24 0 0 0 1 0 0 2 0 3 0 4 0 5 0 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 bi=3 wi=2 w=1 w-wi =-1
Example (Cont !!!) iW 0 1 2 3 4 25 0 0 0 1 0 0 2 0 3 3 0 4 0 5 0 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 bi=3 wi=2 w-wi =0
Example (Cont !!!) iW 0 1 2 3 4 26 0 0 0 1 0 0 2 0 3 3 0 3 4 0 5 0 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 bi=3 wi=2 w=3 w-wi =1
Example (Cont !!!) iW 0 1 2 3 4 27 0 0 0 1 0 0 2 0 3 3 0 3 4 0 3 5 0 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 bi=3 wi=2 w=4 w-wi =2
Example (8) iW 0 1 2 3 4 28 0 0 0 1 0 0 2 0 3 3 0 3 4 0 3 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w 5 0 3 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 bi=3 wi=2 w=5 w-wi =3
Example (9) iW 0 1 2 3 4 29 0 0 0 1 0 0 0 2 0 3 3 0 3 4 0 3 5 0 3 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 bi=4 wi=3 w=1 w-wi =-2
Example (Cont !!!) iW 0 1 2 3 4 30 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 0 3 5 0 3 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 bi=4 wi=3 w=2 w-wi =-1
Example (Cont !!!) iW 0 1 2 3 4 31 0 0 0 2 0 3 3 3 0 3 4 4 0 3 5 0 3 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 bi=4 wi=3 w-wi =0
Example (Cont !!!) iW 0 1 2 3 4 32 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 0 3 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 bi=4 wi=3 w=4 w-wi =1
Example (Cont !!!) iW 0 1 2 3 4 33 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w 4 0 3 4 5 0 3 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 bi=4 wi=3 w=5 w-wi =2
Example (Cont !!!) iW 0 1 2 3 4 34 0 0 0 1 0 0 2 0 3 3 0 3 4 4 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w 4 0 3 4 5 0 3 7 i=3 bi=5 wi=4 w= 1. . 3 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
Example (Cont !!!) iW 0 1 2 3 4 35 0 0 0 1 0 0 2 0 3 3 0 3 4 4 4 0 3 4 5 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w 5 0 3 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=3 bi=5 wi=4 w= 4 w- wi=0
Example (Cont !!!) iW 0 1 2 3 4 36 0 0 0 1 0 0 2 0 3 3 0 3 4 4 4 0 3 4 5 5 0 3 7 7 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=3 bi=5 wi=4 w= 5 w- wi=1
Example (Cont !!!) iW 0 1 2 3 4 37 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 4 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w 4 0 3 4 5 5 5 0 3 7 7 i=4 bi=6 wi=5 w= 1. . 4 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
Example (Cont !!!) iW 0 1 2 3 4 38 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 if wi <= w // item i can be part of the solution if bi + V[i-1, w-wi] > V[i-1, w] V[i, w] = bi + V[i-1, w- wi] else V[i, w] = V[i-1, w] // wi > w 5 0 3 7 7 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=4 bi=6 wi=5 w= 5 w- wi=0
Comments l This algorithm only finds the max possible value that can be carried in the knapsack – l 39 i. e. , the value in V[n, W] To know the items that make this maximum value, an addition to this algorithm is necessary
How to find actual Knapsack Items l l l 40 All of the information we need is in the table. V[n, W] is the maximal value of items that can be placed in the Knapsack. Let i=n and k=W if V[i, k] V[i 1, k] then mark the ith item as in the knapsack i = i 1, k = k-wi else i = i 1 // Assume the ith item is not in the knapsack // Could it be in the optimally packed knapsack?
Finding the Items iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then mark the ith item as in the knapsack i = i 1, k = k-wi 41 else i = i 1 5 0 3 7 7 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=4 k= 5 bi=6 wi=5 V[i, k] = 7 V[i 1, k] =7
Finding the Items (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 5 0 3 7 7 7 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then mark the ith item as in the knapsack i = i 1, k = k-wi 42 else i = i 1 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=4 k= 5 bi=6 wi=5 V[i, k] = 7 V[i 1, k] =7
Finding the Items (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then mark the ith item as in the knapsack i = i 1, k = k-wi 43 else i = i 1 5 0 3 7 7 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=3 k= 5 bi=5 wi=4 V[i, k] = 7 V[i 1, k] =7
Finding the Items (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 4 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then 44 mark the ith item as in the knapsack i = i 1, k = k-wi else i = i 1 4 0 3 4 5 5 5 0 3 7 7 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=2 k= 5 bi=4 wi=3 V[i, k] = 7 V[i 1, k] =3 k wi=2
Finding the Items (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then mark the ith item as in the knapsack i = i 1, k = k-wi 45 else i = i 1 5 0 3 7 7 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) i=1 k= 2 bi=3 wi=2 V[i, k] = 3 V[i 1, k] =0 k wi=0
Finding the Items (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then mark the nth item as in the knapsack i = i 1, k = k-wi 46 else i = i 1 5 0 3 7 7 7 i=0 k= 0 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) The optimal knapsack should contain {1, 2}
Finding the Items (Cont !!!) iW 0 1 2 3 4 0 0 0 1 0 0 0 2 0 3 3 3 0 3 4 4 0 3 4 5 5 i=n, k=W while i, k > 0 if V[i, k] V[i 1, k] then mark the nth item as in the knapsack i = i 1, k = k-wi 47 else i = i 1 5 0 3 7 7 7 Items: 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6) The optimal knapsack should contain {1, 2}
Home Work l 48 How to find out which items are in the optimal subset?
Summary l l 49 Dynamic programming is a useful technique of solving certain kind of problems When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and reuse them as necessary (memorization) Knapsack problem can be solve using dynamic programming approach where items are selected in such a optimal way which help to maximize the values Running time of dynamic programming algorithm vs. naïve algorithm: – 0 -1 Knapsack problem: O(W*n) vs. O(2 n)
In Next Lecturer l 50 In next lecturer , we will discuss about the Floyd algorithm using dynamic programming approach.
- Slides: 50