0% found this document useful (0 votes)
196 views16 pages

Fractional Knapsack Problem

The total time complexity is O(n log n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views16 pages

Fractional Knapsack Problem

The total time complexity is O(n log n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Knapsack Problem

The Knapsack Problem


• The famous knapsack problem:
– A thief breaks into a museum. Fabulous paintings,
sculptures, and jewels are everywhere. The thief
has a good eye for the value of these objects, and
knows that each will fetch hundreds or thousands
of dollars on the clandestine art collector’s market.
But, the thief has only brought a single knapsack to
the scene of the robbery, and can take away only
what he can carry. What items should the thief take
to maximize the haul?
Fractional Knapsack Problems
A thief robbing a store finds n items.
ith item: worth vi dollars
wi pounds
W, wi, vi are integers.
He can carry at most W pounds.
He can take fractions of items.

?
Knapsack Problem – in Short
• A thief considers taking W pounds of loot. The
loot is in the form of n items, each with weight
wi and value vi. Any amount of an item can be
put in the knapsack as long as the weight limit
W is not exceeding.
Knapsack Problem – 2 types

1. 0-1 Knapsack Problem


(Dynamic Programming
Solution)
2. Fractional Knapsack Problem
(Greedy Approach Solution)
0-1 Knapsack problem
• Given a knapsack with maximum capacity W,
and a set S consisting of n items
• Each item i has some weight wi and benefit
value bi (all wi , bi and W are integer values)
• Problem: How to pack the knapsack to achieve
maximum total value of packed items?
• Solution: Dynamic Programming.
Fractional Knapsack Problem
• we are given n objects and a knapsack.
• Object i has a weight wi and the knapsack has a
capacity m.

• If a fraction xi, 0<=xi <=1, of object i is placed


into the knapsack, then a profit pi xi is earned.

• The objective is to obtain a filling of the


knapsack that maximizes the total profit earned.
Fractional Knapsack

max imize  px
1i  n
i i

subject to  w x  m and 0  x  1, 1  i  n
1i  n
i i i

Greedy Algorithm gives the optimal solution


for the Fractional Knapsack Problem.
Greedy 1: Selection criteria: Maximum
beneficial item.
Counter Example:
S = { ( item1 , 5, $70 ), (item2 ,10, $90 ), ( item3, 25, $140 ) }
$140

10 lb

W=
25lb
$90 $140 $70
5 lb
25 lb

$70 25 lb
10 lb $90
5 lb 10 lb

item1 item2 item3 Knapsack Greedy Optimal


Solution
=$140 Solution =$160

Greedy vs Dynamic 9
Greedy 2: Selection criteria: Minimum
weight item
Counter Example:
S = { ( item , 5, $150 ), (item ,10, $60 ), ( item , 20, $140 ) }
1 2 3

5 lb

$140
W=
30lb $140
20 lb
$60 $60
20 lb 10 lb
$150 10 lb
$150
5 lb $150 5 lb
5 lb
item1 item2 item3 Knapsack Greedy Optimal =$290
Solution
=$210 Solution

Greedy vs Dynamic 10
Greedy 3: Selection criteria: Maximum
weight item
Counter Example:
S = { ( item , 5, $150 ), (item ,10, $60 ), ( item , 20, $140 ) }
1 2 3

5 lb
10 lb
$60
$140
W=
30lb $140
20 lb
$60
20 lb
$150 10 lb 20 lb
$140 $150
5 lb 5 lb

item1 item2 item3 Knapsack Greedy Optimal


Solution =$200 Solution =$290

Greedy vs Dynamic 11
Greedy 4: Selection criteria: Maximum
benefit per unit item Counter
Example
S = { ( item1 , 5, $50 ), ( item2, 20, $140 ) (item3 ,10, $60 ), }
5 lb
B/W: $7

$140
W=
30lb 20 lb $140
$140
B/W 2: $6
20 lb
B/W 1: $10 $60
20 lb
$50 10 lb $60
10 lb $50
5 lb 5 lb

item1 item2 item3 Knapsack Greedy Optimal =$200


Solution
=$190 Solution
What is the asymptotic runtime of this algorithm?
Greedy vs Dynamic 12
Fractional Knapsack-Greedy Solution
• Greedy-fractional-knapsack (w, v, W)
1. for i =1 to n
2. do x[i] =0
3. weight = 0
4. while weight < W
5. do i = best remaining item
6. if weight + w[i] ≤ W
7. then x[i] = 1
8. weight = weight + w[i]
9. else
10. x[i] = (w - weight) / w[i]
11. weight = W
12. return x
2 Knapsack Problems
Dynamic Programming Solution

Example: Fractional Knapsack Problem


Suppose there are totally n = 100 pounds of metal dust:
30 pounds Gold dust: each pound $10000 (most expensive)
20 pounds Silver dust: each pound $2000
50 pounds Copper dust: each pound $500

Then, the most valuable way to fill a capacity of W pounds


= The most valuable way among the followings:
(1) take 1 pound of gold + the most valuable way to fill W-1 pounds from 29
pounds of gold, 20 pounds of silver, 50 pounds of copper
(2) take 1 pound of silver + the most valuable way to fill W-1 pounds from 30
pounds of gold, 19 pounds of silver, 50 pounds of copper
(3) take 1 pound copper + the most valuable way to fill W-1 pounds from 30
pounds of gold, 20 pounds of silver, 49 pounds of copper
Knapsack Problems
By Greedy Strategy

Fractional Knapsack Problem can be solved in a


greedy strategy.
Step 1. Compute the value per pound for each item
Eg. gold dust: $10000 per pound (most expensive)
Silver dust: $2000 per pound
Copper dust: $500 per pound
Step 2. Take as much as possible of the most expensive (ie.
Gold dust)
Step 3. If the supply of that item is exhausted (ie. no more
gold) and he can still carry more, he takes as much
as possible of the item that is next most expensive
and so forth until he can’t carry any more.
Analysis
• If the items are already sorted into decreasing
order of vi / wi, then the while-loop takes a time in
O(n);

• Therefore, the total time including the sort is in


O(n log n).
• If we keep the items in heap with largest vi/wi at the
root. Then creating the heap takes O(n) time
• while-loop now takes O(log n) time (since heap property
must be restored after the removal of root)

You might also like