Knapsack Problem A dynamic approach.

Slides:



Advertisements
Similar presentations
Dynamic Programming ACM Workshop 24 August Dynamic Programming Dynamic Programming is a programming technique that dramatically reduces the runtime.
Advertisements

MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta
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
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
Dynamic Programming Reading Material: Chapter 7..
18 = = = = = = = =
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)
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.
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)
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
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.
Week 2: Greedy Algorithms
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Lecture 7: Greedy Algorithms II
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.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Dynamic Programming Sequence of decisions. Problem state. Principle of optimality. Dynamic Programming Recurrence Equations. Solution of recurrence equations.
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
DP (not Daniel Park's dance party). Dynamic programming Can speed up many problems. Basically, it's like magic. :D Overlapping subproblems o Number of.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
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.
Dynamic Programming … Continued 0-1 Knapsack Problem.
2/19/ ITCS 6114 Dynamic programming 0-1 Knapsack problem.
Dynamic Programming … Continued
Dynamic Programming Sequence of decisions. Problem state.
Algorithm Design Methods
Weighted Interval Scheduling
Seminar on Dynamic Programming.
Robbing a House with Greedy Algorithms
Topic 25 Dynamic Programming
Algorithm Design Methods
CS 3343: Analysis of Algorithms
Approximate Algorithms
Exam 2 LZW not on syllabus. 73% / 75%.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Dynamic Programming.
Algorithm Design Methods
CSC 413/513- Intro to Algorithms
Lecture 4 Dynamic Programming
Lecture 6 Greedy Algorithms
Algorithm Design Methods
Dynamic Programming.
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Lecture 5 Dynamic Programming
Lecture 5 Dynamic Programming
0-1 Knapsack problem.
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Advance Algorithm Dynamic Programming
Dynamic Programming Sequence of decisions. Problem state.
Algorithm Design Methods
Seminar on Dynamic Programming.
Presentation transcript:

Knapsack Problem A dynamic approach

Knapsack Problem Given a sack, able to hold W kg Given a list of objects Each has a weight and a value Try to pack the object in the sack so that the total value is maximized

Variation Rational Knapsack 0-1 Knapsack Object is like a gold bar, we can cut it in to piece with the same value/weight 0-1 Knapsack Object cannot be broken, we have to choose to take (1) or leave (0) the object E.g. K = 50 Objects = (60,10) (100,20) (120,30) Best solution = second and third

The Problem Input Output A number W, the capacity of the sack n pairs of weight and price ((w1,p1),(w2,p2),…,(wn,pn)) wi = weight of the ith items pi = price of the ith item Output A subset S of {1,2,3,…,n} such that is maximum

from http://en.wikipedia.org/wiki/File:Knapsack.svg

Naïve approach Try every possible combination of {1,2,3,…n} Test whether a combination satisfies the weight constraint If so, remember the best one This gives O(2n)

Dynamic Approach Let us assume that W (the maximum weight) and wi are integers Let us assume that we just want to know “the best total price”, i.e., (well, soon we will see that this also leads to the actual solution The problem can be solved by a dynamic programming How? What should be the subproblem? Is it overlapping?

The Sub Problem What shall we divide? The number of items? Let’s try half of the items? what about the weight?

The Optimal Solution Assume that we know the actual optimal solution to the problem The solution consist of item {2,5,6,7} What if we takes the item number 7 out? What can we say about the set of {2,5,6} Is it an optimal solution of any particular problem?

The Optimal Solution Let K(b) be the “best total value” when W equals to b If the ith item is in the best solution K(W) = K(W – wi) + pi But, we don’t really know that the ith item is in the optimal solution So, we try everything K(W) = max1≤i ≤ n(K(W – wi) + pi) Is this our algorithm? Yes, if and only if we allow each item to be selected multiple times (that is not true for this problem)

Solution We need to keep track whether the item is used What if we know the optimal solution when ith items is not being used? Also for every size of knapsack from 0 to W Then, with additional ith item, we have only two choices, use it or not use it

The Recurrence K(a,b) = the best total price when the knapsack is of size a and only item number 1 to number b is considered K(a,b) = max( K(a – wb,b – 1) + pb , K(a,b – 1) ) K(a,b) = 0 when a = 0 or b = 0 K(a,b) = K(a,b-1) when wb > a The solution is at K(W,n)

The Recurrent wb K(a,b) Normal case (wb <= a) K(a,b) Too much weight (wb > a)

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = 0 when a = 0 or b = 0

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 1 (p1=4 w1=12) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 1 (p1=4 w1=12) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = K(a,b-1) when wb > a

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 1 (p1=4 w1=12) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = max( K(a – wb,b – 1) + pb , K(a,b – 1) )

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 2 (p2=2 w2=2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = K(a,b-1) when wb > a

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 2 (p2=2 w2=2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = max( K(a – wb,b – 1) + pb , K(a,b – 1) )

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 2 (p2=2 w2=2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = max( K(a – wb,b – 1) + pb , K(a,b – 1) )

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 2 (p2=2 w2=2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 K(a,b) = max( K(a – wb,b – 1) + pb , K(a,b – 1) )

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 3 (p3=2 w3=1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 3 (p3=2 w3=1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 4 (p4=1 w4=1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 4 (p4=1 w4=1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 5 (p5=10 w5=4) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Fill row 5 (p5=10 w5=4) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example p = {4, 2, 2, 1, 10} w ={12, 2, 1, 1, 4} W = 15 Trace the solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

The Code set all K[0][j] = 0 and all K[w][0] = 0 for j = 1 to n for w = 1 to W if (wj > W) K[w][j] = K[w][j – 1]; else K[w][j] = max( K[w – wi][j – 1] + pi , K[W][j – 1] ) return K[W][n];