1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Knapsack Problem Section 7.6. Problem Suppose we have n items U={u 1,..u n }, that we would like to insert into a knapsack of size C. Each item u i has.
Analysis of Algorithms
CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Overview What is Dynamic Programming? A Sequence of 4 Steps
COMP8620 Lecture 8 Dynamic Programming.
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 14 Dynamic.
Dynamic Programming.
1 Recursive Algorithm Analysis Dr. Ying Lu RAIK 283: Data Structures & Algorithms September 13, 2012.
Review: Dynamic Programming
Introduction to Algorithms
RAIK 283: Data Structures & Algorithms
Dynamic Programming (II)
Greedy vs Dynamic Programming Approach
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Warshall’s and Floyd’sAlgorithm Dr. Ying Lu RAIK 283: Data Structures.
Dynamic Programming Reading Material: Chapter 7..
0-1 Knapsack Problem A burglar breaks into a museum and finds “n” items Let v_i denote the value of ith item, and let w_i denote the weight of the ith.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
© 5/7/2002 V.S. Subrahmanian1 Knapsack Problem Notes V.S. Subrahmanian University of Maryland.
© 5/7/2002 V.S. Subrahmanian1 Knapsack Problem Notes V.S. Subrahmanian University of Maryland.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
The Knapsack Problem The classic Knapsack problem is typically put forth as: A thief breaks into a store and wants to fill his knapsack with as much value.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Odds and Ends HP ≤ p HC (again) –Turing reductions Strong NP-completeness versus Weak NP-completeness Vertex Cover to Hamiltonian Cycle.
KNAPSACK PROBLEM A dynamic approach. Knapsack Problem  Given a sack, able to hold K kg  Given a list of objects  Each has a weight and a value  Try.
Dynamic Programming1 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
1 Dynamic Programming Jose Rolim University of Geneva.
The Knapsack Problem Input –Capacity K –n items with weights w i and values v i Goal –Output a set of items S such that the sum of weights of items in.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Dynamic Programming – Part 2 Introduction to Algorithms Dynamic Programming – Part 2 CSE 680 Prof. Roger Crawfis.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Dynamic Programming … Continued 0-1 Knapsack Problem.
1 Today’s Material Dynamic Programming – Chapter 15 –Introduction to Dynamic Programming –0-1 Knapsack Problem –Longest Common Subsequence –Chain Matrix.
2/19/ ITCS 6114 Dynamic programming 0-1 Knapsack problem.
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Dynamic Programming … Continued
TU/e Algorithms (2IL15) – Lecture 3 1 DYNAMIC PROGRAMMING
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
CS 312: Algorithm Design & Analysis Lecture #26: 0/1 Knapsack This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Algorithm Design Methods
CS 3343: Analysis of Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming.
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Longest Common Subsequence
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
CSC 413/513- Intro to Algorithms
Lecture 4 Dynamic Programming
This is not an advertisement for the profession
0-1 Knapsack problem.
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Knapsack Problem A dynamic approach.
Presentation transcript:

1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms

2  Giving credit where credit is due: »Most of slides for this lecture are based on slides created by Dr. David Luebke, University of Virginia. »Some slides are based on lecture notes created by Dr. Chuck Cusack, Hope College. »I have modified them and added new slides. RAIK 283 Data Structures & Algorithms

3 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 # Weight Value Knapsack problem

4 There are two versions of the problem: 1.“0-1 knapsack problem”  Items are indivisible; you either take an item or not. Some special instances can be solved with dynamic programming 2.“Fractional knapsack problem”  Items are divisible: you can take any fraction of an item

5  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight w i and benefit value b i (all w i and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? 0-1 Knapsack problem

6  Problem, in other words, is to find 0-1 Knapsack problem u The problem is called a “0-1” problem, because each item must be entirely accepted or rejected.

7 Let’s first solve this problem with a straightforward algorithm  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: brute-force approach

8  We can do better with an algorithm based on dynamic programming  We need to carefully identify the subproblems 0-1 Knapsack problem: dynamic programming approach

9  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight w i and benefit value b i (all w i and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? Defining a Subproblem

10  We can do better with an algorithm based on dynamic programming  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 S k = {items labeled 1, 2,.. k} Defining a Subproblem

11 If items are labeled 1..n, then a subproblem would be to find an optimal solution for S k = {items labeled 1, 2,.. k}  This is a reasonable subproblem definition.  The question is: can we describe the final solution (S n ) in terms of subproblems (S k )?  Unfortunately, we can’t do that. Defining a Subproblem

12 Max weight: W = 20 For S 4 : Total weight: 14 Maximum benefit: 20 w 1 =2 b 1 =3 w 2 =4 b 2 =5 w 3 =5 b 3 =8 w 4 =3 b 4 =4 wiwi bibi WeightBenefit 9 Item # S4S4 S5S5 w 1 =2 b 1 =3 w 2 =4 b 2 =5 w 3 =5 b 3 =8 w 5 =9 b 5 =10 For S 5 : Total weight: 20 Maximum benefit: 26 Solution for S 4 is not part of the solution for S 5 !!! ? Defining a Subproblem

13  As we have seen, the solution for S 4 is not part of the solution for S 5  So our definition of a subproblem is flawed and we need another one! Defining a Subproblem

14  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight w i and benefit value b i (all w i and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? Defining a Subproblem

15  Let’s add another parameter: w, which will represent the maximum weight for each subset of items  The subproblem then will be to compute V[k,w], i.e., to find an optimal solution for S k = {items labeled 1, 2,.. k} in a knapsack of size w Defining a Subproblem

16  The subproblem will then be to compute V[k,w], i.e., to find an optimal solution for S k = {items labeled 1, 2,.. k} in a knapsack of size w  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

17 It means, that the best subset of S k that has total weight w is: 1) the best subset of S k-1 that has total weight  w, or 2) the best subset of S k-1 that has total weight  w-w k plus the item k Recursive formula for subproblems: Recursive Formula for subproblems (continued)

18 Recursive Formula u The best subset of S k that has the total weight  w, either contains item k or not. u First case: w k >w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable. u Second case: w k  w. Then the item k can be in the solution, and we choose the case with greater value.

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 w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 0-1 Knapsack Algorithm

20 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 What is the running time of this algorithm? O(W) Repeat n times O(n*W) Remember that the brute-force algorithm takes O(2 n ) Running time

21 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) Example

22 for w = 0 to W V[0,w] = i\W Example (2)

23 for i = 1 to n V[i,0] = i\W Example (3)

24 if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 0 i=1 b i =3 w i =2 w=1 w-w i = i\W Example (4)

25 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=1 b i =3 w i =2 w=2 w-w i =0 if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w Example (5)

26 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=1 b i =3 w i =2 w=3 w-w i =1 if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 3 Example (6)

27 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=1 b i =3 w i =2 w=4 w-w i =2 if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 33 Example (7)

28 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=1 b i =3 w i =2 w=5 w-w i =3 if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 333 Example (8)

29 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=2 b i =4 w i =3 w=1 w-w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w Example (9)

30 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=2 b i =4 w i =3 w=2 w-w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 0 Example (10)

31 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=2 b i =4 w i =3 w=3 w-w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 43 Example (11)

32 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=2 b i =4 w i =3 w=4 w-w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 434 Example (12)

33 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=2 b i =4 w i =3 w=5 w-w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 7344 Example (13)

34 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=3 b i =5 w i =4 w= if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w Example (14)

35 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=3 b i =5 w i =4 w= 4 w- w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w Example (15)

36 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=3 b i =5 w i =4 w= 5 w- w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w 57 Example (16)

37 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=4 b i =6 w i =5 w= if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w Example (17)

38 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=4 b i =6 w i =5 w= 5 w- w i = if w i <= w // item i can be part of the solution if b i + V[i-1,w-w i ] > V[i-1,w] V[i,w] = b i + V[i-1,w- w i ] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // w i > w Example (18)

39 Exercise u P (a). u How to find out which items are in the optimal subset?

40 Comments  This algorithm only finds the max possible value that can be carried in the knapsack »i.e., the value in V[n,W]  To know the items that make this maximum value, an addition to this algorithm is necessary

41  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 i th item as in the knapsack i = i  1, k = k-w i else i = i  1 // Assume the i th item is not in the knapsack // Could it be in the optimally packed knapsack? How to find actual Knapsack Items

42 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=4 k= 5 b i =6 w i =5 V[i,k] = 7 V[i  1,k] = i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the i th item as in the knapsack i = i  1, k = k-w i else i = i  Finding the Items

43 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=4 k= 5 b i =6 w i =5 V[i,k] = 7 V[i  1,k] = i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the i th item as in the knapsack i = i  1, k = k-w i else i = i  Finding the Items (2)

44 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=3 k= 5 b i =5 w i =4 V[i,k] = 7 V[i  1,k] = i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the i th item as in the knapsack i = i  1, k = k-w i else i = i  Finding the Items (3)

45 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=2 k= 5 b i =4 w i =3 V[i,k] = 7 V[i  1,k] =3 k  w i = i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the i th item as in the knapsack i = i  1, k = k-w i else i = i  Finding the Items (4)

46 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=1 k= 2 b i =3 w i =2 V[i,k] = 3 V[i  1,k] =0 k  w i = i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the i th item as in the knapsack i = i  1, k = k-w i else i = i  Finding the Items (5)

47 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the n th item as in the knapsack i = i  1, k = k-w i else i = i  i=0 k= 0 The optimal knapsack should contain {1, 2} Finding the Items (6)

48 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) i\W i=n, k=W while i,k > 0 if V[i,k]  V[i  1,k] then mark the n th item as in the knapsack i = i  1, k = k-w i else i = i  The optimal knapsack should contain {1, 2} 7 3 Finding the Items (7)

49 Memorization (Memory Function Method)  Goal: »Solve only subproblems that are necessary and solve it only once  Memorization is another way to deal with overlapping subproblems in dynamic programming  With memorization, we implement the algorithm recursively: »If we encounter a new subproblem, we compute and store the solution. »If we encounter a subproblem we have seen, we look up the answer  Most useful when the algorithm is easiest to implement recursively »Especially if we do not need solutions to all subproblems.

50 for i = 1 to n for w = 1 to W V[i,w] = -1 for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 MFKnapsack(i, w) if V[i,w] < 0 if w < w i value = MFKnapsack(i-1, w) else value = max(MFKnapsack(i-1, w), b i + MFKnapsack(i-1, w-w i )) V[i,w] = value return V[i,w] 0-1 Knapsack Memory Function Algorithm

51  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 re-use them as necessary (memorization)  Running time of dynamic programming algorithm vs. naïve algorithm: »0-1 Knapsack problem: O(W*n) vs. O(2 n ) Conclusion

52 In-Class Exercise

53 Brute-Force Approach Design and Analysis of Algorithms - Chapter 853

54 Dynamic-Programming Approach  (1)SMaxV(0) = 0  (2)MaxV(0) = 0  (3)for i=1 to n  (4) SMaxV(i) = max(SmaxV(i-1)+x i, 0)  (5) MaxV(i) = max(MaxV(i-1), SMaxV(i))  (6)return MaxV(n)  Run the algorithm on the following example instance: »30, 40, -100, 10, 20, 50, -60, 90, -180,