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.