© 5/7/2002 V.S. Subrahmanian1 Knapsack Problem Notes V.S. Subrahmanian University of Maryland.

Slides:



Advertisements
Similar presentations
Lecture 7 Paradigm #5 Greedy Algorithms
Advertisements

Solving IPs – Cutting Plane Algorithm General Idea: Begin by solving the LP relaxation of the IP problem. If the LP relaxation results in an integer solution,
MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta
IP modeling techniques I In this handout, Modeling techniques:  Using binary variables  Restrictions on number of options  Contingent decisions  Variables.
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.
Knapsack Problem: Greedy vs. Brute Force pp (Section 7.6)
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Chapter 5 Fundamental Algorithm Design Techniques.
Analysis of Algorithms
COMP8620 Lecture 8 Dynamic Programming.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
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.
1 ECE 665 Spring 2005 Computer Algorithms Dynamic Programming Knapsack revisited.
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.
Dealing with NP-Complete Problems
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)
© 5/7/2002 V.S. Subrahmanian1 Knapsack Problem Notes V.S. Subrahmanian University of Maryland.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Integer Programming Integer programming is a solution method for many discrete optimization problems Programming = Planning in this context Origins go.
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.
Lecture 7: Greedy Algorithms II
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
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.
Cutting Planes II. The Knapsack Problem Recall the knapsack problem: n items to be packed in a knapsack (can take multiple copies of the same item). The.
Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a.
IT 60101: Lecture #201 Foundation of Computing Systems Lecture 20 Classic Optimization Problems.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
1 0-1 Knapsack problem Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Gary Sham HKOI 2010 Greedy, Divide and Conquer. Greedy Algorithm Solve the problem by the “BEST” choice. To find the global optimal through local optimal.
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
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.
Algorithm Design Methods 황승원 Fall 2011 CSE, POSTECH.
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.
2/19/ ITCS 6114 Dynamic programming 0-1 Knapsack problem.
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
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.
Divide and Conquer. Problem Solution 4 Example.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Greedy Algorithms (Chap. 16)
Robbing a House with Greedy Algorithms
The Knapsack Problem.
CS 3343: Analysis of Algorithms
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
The Greedy Method Spring 2007 The Greedy Method Merge Sort
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Greedy Algorithm Enyue (Annie) Lu.
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Greedy Algorithms: Maximizing Loot
CSC 413/513- Intro to Algorithms
Advanced LP models column generation.
Lecture 4 Dynamic Programming
Lecture 6 Greedy Algorithms
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
0-1 Knapsack problem.
Advance Algorithm Dynamic Programming
Presentation transcript:

© 5/7/2002 V.S. Subrahmanian1 Knapsack Problem Notes V.S. Subrahmanian University of Maryland

© 5/7/2002 V.S. Subrahmanian2 Knapsack Problem You have a knapsack that has capacity (weight) C. You have several items I 1,…,I n. Each item I j has a weight w j and a benefit b j. You want to place a certain number of copies of each item I j in the knapsack so that: –The knapsack weight capacity is not exceeded and –The total benefit is maximal.

© 5/7/2002 V.S. Subrahmanian3 Example ItemWeightBenefit A260 B375 C490 Capacity = 5

© 5/7/2002 V.S. Subrahmanian4 Key question Suppose f(w) represents the maximal possible benefit of a knapsack with weight w. We want to find (in the example) f(5). Is there anything we can say about f(w) for arbitrary w?

© 5/7/2002 V.S. Subrahmanian5 Key observation To fill a knapsack with items of weight w, we must have added items into the knapsack in some order. Suppose the last such item was I j with weight w i and benefit b i. Consider the knapsack with weight (w- w i ). Clearly, we chose to add I j to this knapsack because of all items with weight w i or less, I j had the max benefit b i.

© 5/7/2002 V.S. Subrahmanian6 Key observation Thus, f(w) = MAX { b j + f(w-w j ) | I j is an item}. This gives rise to an immediate recursive algorithm to determine how to fill a knapsack.

© 5/7/2002 V.S. Subrahmanian7 Example ItemWeightBenefit A260 B375 C490

© 5/7/2002 V.S. Subrahmanian8 f(0), f(1) f(0) = 0. Why? The knapsack with capacity 0 can have nothing in it. f(1) = 0. There is no item with weight 1.

© 5/7/2002 V.S. Subrahmanian9 f(2) f(2) = 60. There is only one item with weight 60. Choose A.

© 5/7/2002 V.S. Subrahmanian10 f(3) f(3) = MAX { b j + f(w-w j ) | I j is an item}. = MAX { 60+f(3-2), 75 + f(3-3)} = MAX { , } = 75. Choose B.

© 5/7/2002 V.S. Subrahmanian11 f(4) f(4) = MAX { b j + f(w-w j ) | I j is an item}. = MAX { 60 + f(4-2), 75 + f(4-3), 90+f(4-4)} = MAX { , 75 + f(1), 90 + f(0)} = MAX { 120, 75, 90} =120. Choose A.

© 5/7/2002 V.S. Subrahmanian12 f(5) f(5) = MAX { b j + f(w-w j ) | I j is an item}. = MAX { 60 + f(5-2), 75 + f(5-3), 90+f(5-4)} = MAX { 60 + f(3), 75 + f(2), 90 + f(1)} = MAX { , , 90+0} = 135. Choose A or B.

© 5/7/2002 V.S. Subrahmanian13 Result Optimal knapsack weight is 135. Two possible optimal solutions: –Choose A during computation of f(5). Choose B in computation of f(3). –Choose B during computation of f(5). Choose A in computation of f(2). Both solutions coincide. Take A and B.

© 5/7/2002 V.S. Subrahmanian14 Another example Knapsack of capacity items –Item 1 has weight 10, benefit 60 –Item 2 has weight 20,benefit 100 –Item 3 has weight 30, benefit 120.

© 5/7/2002 V.S. Subrahmanian15 f(0),..,f(9) All have value 0.

© 5/7/2002 V.S. Subrahmanian16 f(10),..,f(19) All have value 10. Choose Item 1.

© 5/7/2002 V.S. Subrahmanian17 f(20),..,f(29) F(20) = MAX { 60 + f(10), f(0) } = MAX { 60+60, 100+0} =120. Choose Item 1.

© 5/7/2002 V.S. Subrahmanian18 f(30),…,f(39) f(30) = MAX { 60 + f(20), f(10), f(0) } = MAX { , , 120+0} = 180 Choose item 1.

© 5/7/2002 V.S. Subrahmanian19 f(40),…,f(49) F(40) = MAX { 60 + f(30), f(20), f(10)} = MAX { , , } = 240. Choose item 1.

© 5/7/2002 V.S. Subrahmanian20 f(50) f(50) = MAX { 60 + f(40), f(30), f(20) } = MAX { , , } = 300. Choose item 1.

© 5/7/2002 V.S. Subrahmanian21 Knapsack Problem Variants 0/1 Knapsack problem: Similar to the knapsack problem except that for each item, only 1 copy is available (not an unlimited number as we have been assuming so far). Fractional knapsack problem: You can take a fractional number of items. Has the same constraint as 0/1 knapsack. Can solve using a greedy algorithm.