2. OUR TOPIC IS FRACTIONAL AND 0/1
KNAPSACK.
SUBMITTED BY:
Lithy Ema Rozario-162-15-7989
Imran Hossain-162-15-7672
Tania Maksum-162-15-7698
Shekh Hasibul Islam-162-15-7748
Mohd Nasir Uddin-162-15-7797
Md Fazlur Rahman-162-15-7796
Salowa Binte Sohel-162-15-7820
Submitted to :
Rifat Ara
Shams
3. THE FRACTIONAL KNAPSACK PROBLEM
β’ Given: A set S of n items, with each item i having
β’ bi - a positive benefit
β’ wi - a positive weight
β’ Goal: Choose items with maximum total benefit but with weight at
most W.
β’ If we are allowed to take fractional amounts, then this is the fractional
knapsack problem.
β’ In this case, we let xi denote the amount we take of item i
β’ Objective: maximize
β’ Constraint:
ο₯οSi
iii wxb )/(
ii
Si
i wxWx ο£ο£ο£ο₯ο
0,
4. EXAMPLE
β’ Given: A set S of n items, with each item i having
β’ bi - a positive benefit
β’ wi - a positive weight
β’ Goal: Choose items with maximum total benefit but with total weight at
most W.
Weight:
Benefit:
1 2 3 4 5
4 ml 8 ml 2 ml 6 ml 1 ml
$12 $32 $40 $30 $50
Items:
Value:
3($ per ml) 4 20 5 50
10 ml
Solution: P
β’ 1 ml of 5 50$
β’ 2 ml of 3 40$
β’ 6 ml of 4 30$
β’ 1 ml of 2 4$
β’Total Profit:124$
βknapsackβ
5. THE FRACTIONAL KNAPSACK ALGORITHM
β’ Greedy choice: Keep taking item with highest value (benefit to
weight ratio)
β’ Since
Algorithm fractionalKnapsack(S, W)
Input: set S of items w/ benefit bi and weight wi; max. weight W
Output: amount xi of each item i to maximize benefit w/ weight at most W
for each item i in S
xi ο¬ 0
vi ο¬ bi / wi {value}
w ο¬ 0 {total weight}
while w < W
remove item i with highest vi
xi ο¬ min{wi , W - w}
w ο¬ w + min{wi , W - w}
ο₯ο₯ οο
ο½
Si
iii
Si
iii xwbwxb )/()/(
6. THE FRACTIONAL KNAPSACK ALGORITHM
β’ Running time: Given a collection S of n items, such that each item i has
a benefit bi and weight wi, we can construct a maximum-benefit subset of
S, allowing for fractional amounts, that has a total weight W in O(nlogn)
time.
β’ Use heap-based priority queue to store S
β’ Removing the item with the highest value takes O(logn) time
β’ In the worst case, need to remove all items
7. KNAPSACK PROBLEM
οGiven a set of items, each with a mass and a value,
determine the number of each item to include in a
collection so that the total weight is less than or equal
to a given limit and the total value is as large as
possible.
ο It derives its name from the problem faced by
someone who is constrained by a fixed-
size knapsack and must fill it with the most valuable
items.
8. KNAPSACK PROBLEM
β’ In a knapsack problem or rucksack problem,
we are given a set of π items, where each item π
is specified by a size π π and a value π£π. We are
also given a size bound π, the size of our
knapsack.
Item # Size Value
1 1 8
2 3 6
3 5 5
9. KNAPSACK PROBLEM
There are two versions of the problem:
1. 0-1 Knapsack Problem
2. Fractional Knapsack Problem
i. Bounded Knapsack Problem
ii. Unbounded Knapsack Problem
10. SOLUTIONS TO KNAPSACK
PROBLEMS
οGreedy Algorithm β keep taking most
valuable items until maximum weight is
reached or taking the largest value of each
item by calculating ππ =
π£πππ’π π
π ππ§π π
οDynamic Programming β solve each sub
problem once and store their solutions in
an array
11. EXAMPLE
Given:
π = 4 (# of elements)
π = 5 pounds (maximum size)
Elements (size, value) =
{ (1, 200), (3, 240), (2, 140), (5, 150) }
12. GREEDY ALGORITHM
1. Calculate Vi =
vi
si
for π = 1,2, β¦ , π
2. Sort the items by decreasing Vi
3. Find j, such that
π 1 + π 2 + β― + π π β€ π < π 1 + π 2 + β― + π π+1
13. GREEDY ALGORITHM
Sample Problem
ππ =
πππ π‘π
π€πππβπ‘ π
A B C D
cost 200 240 140 150
weight 1 3 2 5
value 200 80 70 30
ππ =
π£πππ’π π
π ππ§π π
=
πππ π‘ π
π€πππβπ‘ π
14. GREEDY ALGORITHM
οΆ The optimal solution to the fractional
knapsack
οΆ Not an optimal solution to the 0-1
knapsack
17. DYNAMIC PROGRAMMING
for π = 1 to π
if π π β€ π
if π£π + π π β 1, π β π π > π[π β 1, π ]
V i, s = π£π + π π β 1, π β π π
else
V i, s = V[i β 1, s]
else V i, s = V[i β 1, s]
18. EXAMPLE
Given:
π = 4 (# of elements)
π = 5 (maximum size)
Elements (size, value) =
{ (2, 3), (3, 4), (4, 5), (5, 6) }
1. Items are indivisible: you either take an item or not. Solved with dynamic programming
2. Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm
1. Items are indivisible: you either take an item or not. Solved with dynamic programming
2. Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm
Legend:
Red β wala pud ko kasabot
Solution is
1 pds A
3 pds B
1 pd C
This means that the best subset of π π that has the total size π, can either contains item k or not.
First case: π π >π . Item k canβt be part of the solution, since if it was, the total size would be >s, which is unacceptable
Second case: π€ π β€π€. Then the item k can be in the solution, and we choose the case with greater value.
First for-loop: π =0 π‘π π. We go through all the possible sizes of our knapsack until S and if item i is equal to 0, which is βV[0,s]β its corresponding maximum value is of course, 0. Because when i = 0, this means that we are not taking any item.
Second for-loop: i=1 π‘π π. We go through all the items from 1 to n and if the knapsackβs size is equal to 0, which is βV[i, 0]β its corresponding values is again 0. Because when s = 0, this means that we canβt put anything in the knapsack.
Again, we go through all the items and:
Outer if: π π β€π . This means that the size of the item can fit in the current size of the knapsack and we should consider its possible maximum value.
Outer else: π π >π . Item i canβt be part of the solution, since its size is bigger than the knapsackβs current limit. Then, weβll just copy the value above it.
Inner if: π£ π +π πβ1, π β π π >π[πβ1, π ]. This means that if the current itemβs value + π πβ1, π β π π is greater than the value above it, we must use the current itemβs value + π πβ1, π β π π .
Inner else: π£ π +π πβ1, π β π π β€π[πβ1, π ]. This means that if the current itemβs value + π πβ1, π β π π is less than or equal to the value above it, we must use the value on the previous item (or simple the value above it).
The outer if and else conditions check if the knapsack can hold the current item or not.
The inner if and else conditions check if the current value is bigger than the previous value so as to maximize the values the knapsack can hold.