The document presents an overview of fractional and 0/1 knapsack problems. It defines the fractional knapsack problem as choosing items with maximum total benefit but fractional amounts allowed, with total weight at most W. An algorithm is provided that takes the item with highest benefit-to-weight ratio in each step. The 0/1 knapsack problem requires choosing whole items. Solutions like greedy and dynamic programming are discussed. An example illustrates applying dynamic programming to a sample 0/1 knapsack problem.
This section welcomes the audience and introduces the topic of the presentation: Fractional and 0/1 Knapsack, along with the names of the presenters.
Describes the fractional knapsack problem, its goal of maximizing total benefit while adhering to weight constraints, and presents an example with numerical values.
Details the greedy algorithm for solving the fractional knapsack problem, emphasizing its efficiency with a running time of O(n log n).
Introduces the general knapsack problem, explaining its objective and distinguishing between the two versions: 0-1 Knapsack and Fractional Knapsack.
Discusses greedy algorithms and dynamic programming as solutions to knapsack problems, elaborating on their methodologies and providing examples.
Presents recursive formulas and dynamic programming techniques for solving knapsack problems, emphasizing stored solutions.
Gives an example with specific elements and dynamic programming matrix setups, illustrating the process of solving the knapsack problem.
Continues the dynamic programming example, showcasing step-by-step calculation and updating of values in the matrix for various item sizes.
Describes the backtracking method to identify which items were added to the optimal knapsack based on computed values.
Finalizes the example by stating the optimal knapsack solution, showing which items yield the maximum value.
OUR TOPIC ISFRACTIONAL 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 KNAPSACKPROBLEM
โข 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: Aset 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 KNAPSACKALGORITHM
โข 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 KNAPSACKALGORITHM
โข 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 aset 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
โข Ina 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 aretwo 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
๏GreedyAlgorithm โ 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. CalculateVi =
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
๏ถ Theoptimal solution to the fractional
knapsack
๏ถ Not an optimal solution to the 0-1
knapsack
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) }
#8ย 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
#10ย 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
#16ย 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.
#17ย 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.
#18ย 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.