Download presentation

Presentation is loading. Please wait.

Published byKaila Schmidt Modified over 3 years ago

1
MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta ngupta@cs.du.ac.in

2
Table of Contents Generalizations of Subset Sum 0-1 Knapsack Bin Packing

3
The Knapsack Problem The classic Knapsack problem is: A thief breaks into a store and wants to fill his knapsack of capacity K with goods of as much value as possible. Decision version: Does there exist a collection of items that fits into his knapsack and whose total value is >= W?

4
The Knapsack Problem Input –Capacity K –n items with weights w i and values v i Output: a set of items S such that the sum of weights of items in S is at most K and the sum of values of items in S is maximized

5
Some Simplest Versions… Fractional Knapsack Problem: Can items be picked up partially? The thief’s knapsack can hold 100 gms and has to choose from: 30 gms of gold dust at Rs 1000 /gm 60 gms of silver dust at Rs 500/gm 30 gms of platinum dust at Rs 1500/gm Note: Optimal fills the Knapsack upto full capacity. Proof: Else the remaining capacity can be filled with some item, picking it partially if the need be.

6
Greedy Algorithm for fractional Knapsack 1.Sort the items in the increasing order of value/weight ratio (cost effectiveness). 2.If the next item cannot fit into the knapsack, break it and pick it partially just to fill the knapsack.

7
Fractional Knapsack has greedy choice property That is, if v1/w1 is maximum, then there exists an optimal solution that contains item x1 upto the extent of min{w1, W}. Proof: Suppose not. Let O be an optimal solution that does not contain x1. Let xt be the item with maximum weight wt in O. If wt > w1, replacing w1 amount of xt by w1 amount of x1, value of the solution will improve (since v1/w1 > vt/wt). Let S’ be a subset of items in O whose is > w1. Replacing w1 of this total weight by w1 of x1 will improve the value of the solution. If no such set S’ exists then (sum of all the sets in O =) W < = w1. Replace all the sets in O by W amount of x1 and the value of the solution will improve.

8
Other Simple versions Are all of the weights or total values identical? The thief breaks into a ring shop where all of the rings weight 10 gms. He can hold 25 gms; which should he take?

9
0-1 Knapsack An item can either be picked or left. It cannot be picked partially. For example gold coins, diamond rings, TV etc.

10
Greedy doesn’t work for 0-1 Knapsack Capacity 200 Items : X1 : v1/w1 = 12, weight : 50 X2 : v2/w2 = 10, weight : 55 X3 : v3/w3 = 8, weight : 10 X4 : v4/w4 = 6, weight : 100 Value of Greedy : 50 * 12 + 55 *10 + 8* 10= 1230 Optimal : 8 * 100 + 55*10 + 8*10= 1430

11
Dynamic Programming Solution Let V(i,w) is the value of the set of items from the first i items that maximizes the value subject to the constraint that the sum of the values of the items in the set is <= w Value of the original problem corresponds to V(n, K)

12
Recurrence Relation V(i,w) = max (V(i-1,w-w i ) + v i, V(i-1, w)) –Fisrt term corresponds to the case when xi is included in the solution and –The second term corresponds to the case when xi is not included V(0,w) = 0 (no items to choose from) V(i,0) = 0 (no weight allowed)

13
Time Analysis O(nK) It is exponential in the input size.

14
Generalizations of Subset Sum The Subset Sum problem we have studied and shown that it is NPC is the following: Given a finite set S of natural numbers and a target t є N, does there exist a subset S’ of S whose elements sum up to t. Its generalization is: Gen1_SS: Given a finite set S ={x1 … xn} of n numbers, does there exist a subset S’ of S that whose sum is = K By Generalization it is NPC

15
Further generalization of Subset Sum Gen1_SS: Given a finite set S ={x1 … xn} of n numbers, does there exist a subset S’ of S that whose sum is = K. Gen2_SS: Given a finite set S ={x1 … xn} of n numbers, find a subset S’ of S that maximizes the sum with the constraint that the sum is <= K. Its decision version is: Given a finite set S ={x1 … xn} of n numbers, does there exist a subset S’ of S whose sum >= W with the constraint that the sum is <= K.

16
0-1 Knapsack is NP-Complete Gen2_SS: Given a finite set S ={x1 … xn} of n numbers, find a subset S’ of S that maximizes the sum with the constraint that the sum is <= K. The above subset sum problem is a particular case of 0-1 Knapsack by putting wi = vi = xi. i.e. 0-1 Knapsack is a generalization of the above subset sum problem. Hence, by generalization 0-1 Knapsack is NPC

17
Bin Packing Problem Given a set of items S = {x1…xn} each with some weight wi, pack maximum number of items into a collection of finite number of bins each with some capacity Bi using minimum number of bins. Knapsack problem is a particular case of Bin-packing when the number of bins is 1 and its capacity is K

18
Bin Packing is NPC By Generalization of Knapsack.

Similar presentations

OK

CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.

CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google