0% found this document useful (0 votes)
59 views15 pages

Standard Problems: PCS104: Advanced Algorithms

The document discusses several standard problems in algorithms including fractional knapsack, 0-1 knapsack, and bin packing. It provides details on the greedy algorithm for fractional knapsack, explains why greedy does not work for 0-1 knapsack, and presents a dynamic programming solution for 0-1 knapsack that runs in O(nK) time. It also defines the bin packing problem and notes that knapsack is a special case of bin packing.

Uploaded by

JITENDER BHATT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views15 pages

Standard Problems: PCS104: Advanced Algorithms

The document discusses several standard problems in algorithms including fractional knapsack, 0-1 knapsack, and bin packing. It provides details on the greedy algorithm for fractional knapsack, explains why greedy does not work for 0-1 knapsack, and presents a dynamic programming solution for 0-1 knapsack that runs in O(nK) time. It also defines the bin packing problem and notes that knapsack is a special case of bin packing.

Uploaded by

JITENDER BHATT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Standard Problems

PCS104: Advanced Algorithms


• Fractional Knapsack

• 0-1 Knapsack

• Bin Packing
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 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 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


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 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.
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


picked partially. For example gold coins,
diamond rings, TV etc.
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
Solution1: 6 * 100 + 55*10 + 8*10= 1230
Solution2: 12*50 + 8*10 + 6*100 = 1280
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))
– 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(nK)

It is exponential in the input size.


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
Bin Packing
• By Generalization of Knapsack.

You might also like