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.

Slides:



Advertisements
Similar presentations
Algorithm Design Methods Spring 2007 CSE, POSTECH.
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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
Dynamic Programming.
Review: Dynamic Programming
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Greedy vs Dynamic Programming Approach
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.
Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
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.
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)
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.
Odds and Ends HP ≤ p HC (again) –Turing reductions Strong NP-completeness versus Weak NP-completeness Vertex Cover to Hamiltonian Cycle.
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.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
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.
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.
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.
G ENERATE AND T EST F RAMEWORK Nattee Niparnan. O PTIMIZATION E XAMPLE : F INDING M AX V ALUE IN AN A RRAY There are N possible answers.
NP-Complete Problems. Running Time v.s. Input Size Concern with problems whose complexity may be described by exponential functions. Tractable problems.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
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.
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
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Dynamic Programming Sequence of decisions. Problem state.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Greedy Algorithms.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
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
4-9问题的形式化描述.
CSC 413/513- Intro to Algorithms
Lecture 4 Dynamic Programming
Lecture 6 Greedy Algorithms
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
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
Knapsack Problem A dynamic approach.
Presentation transcript:

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 to pack the object in the sack so that the total value is maximized

Variation  Rational 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  A number W, the capacity of the sack  n pairs of weight and price ((w 1,p 1 ),(w 2,p 2 ),…,(w n,p n )) w i = weight of the i th items p i = price of the i th item  Output  A subset S of {1,2,3,…,n} such that is maximum

from

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(2 n )

Dynamic Approach  Let us assume that W (the maximum weight) and w i 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(w) be the “best total value” when W equals to w  If the i th item is in the best solution  K(W) = K(W – w i ) + p i  But, we don’t really know that the i th item is in the optimal solution  So, we try everything  K(W) = max 1≤i ≤ n (K(W – w i ) + p i )  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 i th items is not being used?  Also for every size of knapsack from 0 to W  Then, with additional i th 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(W,j) = max( K(W – w i,j – 1) + p i, K(W,j – 1) )  The solution is at K(W,n)

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 – w i,j – 1) + p i, K(W,j – 1) ) return K(W,n);