MCS 312 NP Completeness and Approximation algorithms Instructor

  • Slides: 18
Download presentation
MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta ngupta@cs. du. ac. in

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

Table of Contents • Generalizations of Subset Sum • 0 -1 Knapsack • Bin

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

The Knapsack Problem The classic Knapsack problem is: A thief breaks into a store

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?

The Knapsack Problem • Input – Capacity K – n items with weights wi

The Knapsack Problem • Input – Capacity K – n items with weights wi and values vi • 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

Some Simplest Versions… Fractional Knapsack Problem: Can items be picked up partially? The thief’s

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.

Greedy Algorithm for fractional Knapsack 1. Sort the items in the increasing order of

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.

Fractional Knapsack has greedy choice property That is, if v 1/w 1 is maximum,

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

Other Simple versions Are all of the weights or total values identical? The thief

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?

0 -1 Knapsack An item can either be picked or left. It cannot be

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

Greedy doesn’t work for 0 -1 Knapsack Capacity 200 Items : X 1 :

Greedy doesn’t work for 0 -1 Knapsack Capacity 200 Items : X 1 : v 1/w 1 = 12, weight : 50 X 2 : v 2/w 2 = 10, weight : 55 X 3 : v 3/w 3 = 8, weight : 10 X 4 : v 4/w 4 = 6, weight : 100 Value of Greedy : 50 * 12 + 55 *10 + 8* 10= 1230 Optimal : 8 * 100 + 55*10 + 8*10= 1430

Dynamic Programming Solution • Let V(i, w) is the value of the set of

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)

Recurrence Relation • V(i, w) = max (V(i-1, w-wi) + vi, V(i-1, w)) –

Recurrence Relation • V(i, w) = max (V(i-1, w-wi) + vi, 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)

Time Analysis • O(n. K) It is exponential in the input size.

Time Analysis • O(n. K) It is exponential in the input size.

Generalizations of Subset Sum • The Subset Sum problem we have studied and shown

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: Gen 1_SS: Given a finite set S ={x 1 … xn} of n numbers, does there exist a subset S’ of S that whose sum is = K • By Generalization it is NPC

Further generalization of Subset Sum • Gen 1_SS: Given a finite set S ={x

Further generalization of Subset Sum • Gen 1_SS: Given a finite set S ={x 1 … xn} of n numbers, does there exist a subset S’ of S that whose sum is = K. • Gen 2_SS: Given a finite set S ={x 1 … 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 ={x 1 … xn} of n numbers, does there exist a subset S’ of S whose sum >= W with the constraint that the sum is <= K.

0 -1 Knapsack is NP-Complete • Gen 2_SS: Given a finite set S ={x

0 -1 Knapsack is NP-Complete • Gen 2_SS: Given a finite set S ={x 1 … 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.

Bin Packing Problem • Given a set of items S = {x 1…xn} each

Bin Packing Problem • Given a set of items S = {x 1…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

Bin Packing is NPC • By Generalization of Knapsack.

Bin Packing is NPC • By Generalization of Knapsack.