SlideShare a Scribd company logo
WELCOME
TO
OUR
PRESENTATION
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
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,
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”
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 )/()/(
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
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.
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
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
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
EXAMPLE
Given:
𝑛 = 4 (# of elements)
𝑆 = 5 pounds (maximum size)
Elements (size, value) =
{ (1, 200), (3, 240), (2, 140), (5, 150) }
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
GREEDY ALGORITHM
Sample Problem
𝑉𝑖 =
𝑐𝑜𝑠𝑡𝑖
𝑤𝑒𝑖𝑔ℎ𝑡 𝑖
A B C D
cost 200 240 140 150
weight 1 3 2 5
value 200 80 70 30
𝑉𝑖 =
𝑣𝑎𝑙𝑢𝑒 𝑖
𝑠𝑖𝑧𝑒 𝑖
=
𝑐𝑜𝑠𝑡 𝑖
𝑤𝑒𝑖𝑔ℎ𝑡 𝑖
GREEDY ALGORITHM
 The optimal solution to the fractional
knapsack
 Not an optimal solution to the 0-1
knapsack
DYNAMIC PROGRAMMING
Recursive formula for sub problems:
𝑉 𝑘, 𝑠
=
𝑉 𝑘 − 1, 𝑠 𝑖𝑓𝑠 𝑘 > 𝑠
max 𝑉 𝑘 − 1, 𝑠 , 𝑉 𝑘 − 1, 𝑠 − 𝑠 𝑘 + 𝑣 𝑘 𝑒𝑙𝑠𝑒
DYNAMIC PROGRAMMING
for 𝑠 = 0 to 𝑆
V[ 0, s ] = 0
for 𝑖 = 1 to 𝑛
V[𝑖, 0 ] = 0
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]
EXAMPLE
Given:
𝑛 = 4 (# of elements)
𝑆 = 5 (maximum size)
Elements (size, value) =
{ (2, 3), (3, 4), (4, 5), (5, 6) }
EXAMPLE
is 0 1 2 3 4 5
0
1
2
3
4
EXAMPLE
for 𝑠 = 0 to 𝑆
V[ 0, 𝑠 ] = 0
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4
EXAMPLE
for 𝑖 = 0 to 𝑛
V[𝑖, 0 ] = 0
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
V i, s = V[i − 1, s]
else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
V i, s = V[i − 1, s]
else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
V i, s = V[i − 1, s]
else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
V i, s = V[i − 1, s]
else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊
else
V i, s = V[i − 1, s]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
𝑉 𝑖, 𝑠 = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
𝑽 𝒊, 𝒔 = 𝑽[𝒊 − 𝟏, 𝒔]
else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
V i, s = V[i − 1, s]
else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
if 𝑠𝑖 ≤ 𝑠
if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠]
V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖
else
V i, s = V[i − 1, s]
else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
𝑖 = 𝑛, 𝑘 = 𝑆
while 𝑖, 𝑘 > 0
if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘
𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖
else
𝑖 = 𝑖 − 1
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
𝑖 = 𝑛, 𝑘 = 𝑆
while 𝑖, 𝑘 > 0
if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘
𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖
else
𝒊 = 𝒊 − 𝟏
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
𝑖 = 𝑛, 𝑘 = 𝑆
while 𝑖, 𝑘 > 0
if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘
𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖
else
𝒊 = 𝒊 − 𝟏
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
𝑖 = 𝑛, 𝑘 = 𝑆
while 𝑖, 𝑘 > 0
if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘
𝒊 = 𝒊 − 𝟏, 𝒌 = 𝒌 − 𝒔𝒊
else
𝑖 = 𝑖 − 1
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
𝑖 = 𝑛, 𝑘 = 𝑆
while 𝑖, 𝑘 > 0
if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘
𝒊 = 𝒊 − 𝟏, 𝒌 = 𝒌 − 𝒔𝒊
else
𝑖 = 𝑖 − 1
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
𝑖 = 𝑛, 𝑘 = 𝑆
while 𝑖, 𝑘 > 0
if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘
𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖
else
𝑖 = 𝑖 − 1
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
EXAMPLE
The optimal knapsack should
contain {1,2} = 7
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Items (𝑠𝑖, 𝑣𝑖)
1: (2, 3)
2: (3, 4)
3: (4, 5)
4: (5, 6)
Knapsack problem dynamicprogramming

More Related Content

What's hot

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
khush_boo31
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
i i
 
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
Rajendran
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
greedy algorithm Fractional Knapsack
greedy algorithmFractional Knapsack greedy algorithmFractional Knapsack
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
Knapsack
KnapsackKnapsack
Knapsack
Karthik Chetla
 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
Hemant Gautam
 
Floyd warshall-algorithm
Floyd warshall-algorithmFloyd warshall-algorithm
Floyd warshall-algorithm
Malinga Perera
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13Kumar
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
Barani Tharan
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Kiran K
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 

What's hot (20)

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
greedy algorithm Fractional Knapsack
greedy algorithmFractional Knapsack greedy algorithmFractional Knapsack
greedy algorithm Fractional Knapsack
 
Knapsack
KnapsackKnapsack
Knapsack
 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
 
Floyd warshall-algorithm
Floyd warshall-algorithmFloyd warshall-algorithm
Floyd warshall-algorithm
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Network flow problems
Network flow problemsNetwork flow problems
Network flow problems
 

Similar to Knapsack problem dynamicprogramming

Gram-Schmidt process linear algbera.pptx
Gram-Schmidt process linear algbera.pptxGram-Schmidt process linear algbera.pptx
Gram-Schmidt process linear algbera.pptx
Md. Al-Amin
 
Ta 2018-1-2404-24109 algebra lineal
Ta 2018-1-2404-24109 algebra linealTa 2018-1-2404-24109 algebra lineal
Ta 2018-1-2404-24109 algebra lineal
jhonatanVsquezArriag
 
TRANSFORMACIONES LINEALES
TRANSFORMACIONES LINEALESTRANSFORMACIONES LINEALES
TRANSFORMACIONES LINEALES
Wilson Quinatoa
 
Functions of severable variables
Functions of severable variablesFunctions of severable variables
Functions of severable variables
Santhanam Krishnan
 
Gauss elimination & Gauss Jordan method
Gauss elimination & Gauss Jordan methodGauss elimination & Gauss Jordan method
Gauss elimination & Gauss Jordan method
Naimesh Bhavsar
 
Yr7-AlgebraicExpressions (1).pptx
Yr7-AlgebraicExpressions (1).pptxYr7-AlgebraicExpressions (1).pptx
Yr7-AlgebraicExpressions (1).pptx
PremkumarLetchumanan
 
Factoring common monomial
Factoring common monomialFactoring common monomial
Factoring common monomial
AjayQuines
 
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear EquationsTenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
QUESTJOURNAL
 
Estructura Discreta I
Estructura Discreta IEstructura Discreta I
Estructura Discreta I
U.F.T Fermin Toro
 
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
IJMER
 
Gen Math topic 1.pptx
Gen Math topic 1.pptxGen Math topic 1.pptx
Gen Math topic 1.pptx
AngeloReyes58
 
Power Series,Taylor's and Maclaurin's Series
Power Series,Taylor's and Maclaurin's SeriesPower Series,Taylor's and Maclaurin's Series
Power Series,Taylor's and Maclaurin's Series
Shubham Sharma
 
PO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptxPO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptx
PATELPUNIT2
 
Reducible equation to quadratic form
Reducible equation to quadratic formReducible equation to quadratic form
Reducible equation to quadratic form
MahrukhShehzadi1
 
Probability
ProbabilityProbability
Probability
KareemSalem23
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
G2018ChoudhariNikita
 
Seven Dimensional Cross Product using the Octonionic Fanoplane
Seven Dimensional Cross Product using the Octonionic FanoplaneSeven Dimensional Cross Product using the Octonionic Fanoplane
Seven Dimensional Cross Product using the Octonionic FanoplaneAdrian Sotelo
 
Комплекс тоо цуврал хичээл-2
Комплекс тоо цуврал хичээл-2Комплекс тоо цуврал хичээл-2
Комплекс тоо цуврал хичээл-2
Март
 

Similar to Knapsack problem dynamicprogramming (20)

Gram-Schmidt process linear algbera.pptx
Gram-Schmidt process linear algbera.pptxGram-Schmidt process linear algbera.pptx
Gram-Schmidt process linear algbera.pptx
 
Ta 2018-1-2404-24109 algebra lineal
Ta 2018-1-2404-24109 algebra linealTa 2018-1-2404-24109 algebra lineal
Ta 2018-1-2404-24109 algebra lineal
 
TRANSFORMACIONES LINEALES
TRANSFORMACIONES LINEALESTRANSFORMACIONES LINEALES
TRANSFORMACIONES LINEALES
 
Homework 4
 Homework 4 Homework 4
Homework 4
 
Homework 4 (Edited)
 Homework 4 (Edited)  Homework 4 (Edited)
Homework 4 (Edited)
 
Functions of severable variables
Functions of severable variablesFunctions of severable variables
Functions of severable variables
 
Gauss elimination & Gauss Jordan method
Gauss elimination & Gauss Jordan methodGauss elimination & Gauss Jordan method
Gauss elimination & Gauss Jordan method
 
Yr7-AlgebraicExpressions (1).pptx
Yr7-AlgebraicExpressions (1).pptxYr7-AlgebraicExpressions (1).pptx
Yr7-AlgebraicExpressions (1).pptx
 
Factoring common monomial
Factoring common monomialFactoring common monomial
Factoring common monomial
 
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear EquationsTenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
Tenth-Order Iterative Methods withoutDerivatives forSolving Nonlinear Equations
 
Estructura Discreta I
Estructura Discreta IEstructura Discreta I
Estructura Discreta I
 
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
 
Gen Math topic 1.pptx
Gen Math topic 1.pptxGen Math topic 1.pptx
Gen Math topic 1.pptx
 
Power Series,Taylor's and Maclaurin's Series
Power Series,Taylor's and Maclaurin's SeriesPower Series,Taylor's and Maclaurin's Series
Power Series,Taylor's and Maclaurin's Series
 
PO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptxPO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptx
 
Reducible equation to quadratic form
Reducible equation to quadratic formReducible equation to quadratic form
Reducible equation to quadratic form
 
Probability
ProbabilityProbability
Probability
 
Unit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptxUnit 1 Set Theory-Engineering Mathematics.pptx
Unit 1 Set Theory-Engineering Mathematics.pptx
 
Seven Dimensional Cross Product using the Octonionic Fanoplane
Seven Dimensional Cross Product using the Octonionic FanoplaneSeven Dimensional Cross Product using the Octonionic Fanoplane
Seven Dimensional Cross Product using the Octonionic Fanoplane
 
Комплекс тоо цуврал хичээл-2
Комплекс тоо цуврал хичээл-2Комплекс тоо цуврал хичээл-2
Комплекс тоо цуврал хичээл-2
 

Recently uploaded

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 

Recently uploaded (20)

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 

Knapsack problem dynamicprogramming

  • 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
  • 15. DYNAMIC PROGRAMMING Recursive formula for sub problems: 𝑉 𝑘, 𝑠 = 𝑉 𝑘 − 1, 𝑠 𝑖𝑓𝑠 𝑘 > 𝑠 max 𝑉 𝑘 − 1, 𝑠 , 𝑉 𝑘 − 1, 𝑠 − 𝑠 𝑘 + 𝑣 𝑘 𝑒𝑙𝑠𝑒
  • 16. DYNAMIC PROGRAMMING for 𝑠 = 0 to 𝑆 V[ 0, s ] = 0 for 𝑖 = 1 to 𝑛 V[𝑖, 0 ] = 0
  • 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) }
  • 19. EXAMPLE is 0 1 2 3 4 5 0 1 2 3 4
  • 20. EXAMPLE for 𝑠 = 0 to 𝑆 V[ 0, 𝑠 ] = 0 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 2 3 4
  • 21. EXAMPLE for 𝑖 = 0 to 𝑛 V[𝑖, 0 ] = 0 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 2 0 3 0 4 0
  • 22. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else V i, s = V[i − 1, s] else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 2 0 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 23. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 2 0 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 24. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 2 0 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 25. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 2 0 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 26. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 27. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else V i, s = V[i − 1, s] else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 28. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else V i, s = V[i − 1, s] else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 29. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 30. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 31. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 32. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else V i, s = V[i − 1, s] else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 33. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝐕 𝐢, 𝒔 = 𝒗𝒊 + 𝑽 𝒊 − 𝟏, 𝒔 − 𝒔𝒊 else V i, s = V[i − 1, s] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 34. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] 𝑉 𝑖, 𝑠 = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else 𝑽 𝒊, 𝒔 = 𝑽[𝒊 − 𝟏, 𝒔] else 𝑉 𝑖, 𝑠 = 𝑉[𝑖 − 1, 𝑠] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 35. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else V i, s = V[i − 1, s] else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 36. EXAMPLE if 𝑠𝑖 ≤ 𝑠 if 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 > 𝑉[𝑖 − 1, 𝑠] V i, s = 𝑣𝑖 + 𝑉 𝑖 − 1, 𝑠 − 𝑠𝑖 else V i, s = V[i − 1, s] else 𝐕 𝐢, 𝒔 = 𝐕[𝐢 − 𝟏, 𝐬] is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 37. EXAMPLE 𝑖 = 𝑛, 𝑘 = 𝑆 while 𝑖, 𝑘 > 0 if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘 𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖 else 𝑖 = 𝑖 − 1 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 38. EXAMPLE 𝑖 = 𝑛, 𝑘 = 𝑆 while 𝑖, 𝑘 > 0 if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘 𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖 else 𝒊 = 𝒊 − 𝟏 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 39. EXAMPLE is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 𝑖 = 𝑛, 𝑘 = 𝑆 while 𝑖, 𝑘 > 0 if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘 𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖 else 𝒊 = 𝒊 − 𝟏 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 40. EXAMPLE 𝑖 = 𝑛, 𝑘 = 𝑆 while 𝑖, 𝑘 > 0 if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘 𝒊 = 𝒊 − 𝟏, 𝒌 = 𝒌 − 𝒔𝒊 else 𝑖 = 𝑖 − 1 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 41. EXAMPLE 𝑖 = 𝑛, 𝑘 = 𝑆 while 𝑖, 𝑘 > 0 if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘 𝒊 = 𝒊 − 𝟏, 𝒌 = 𝒌 − 𝒔𝒊 else 𝑖 = 𝑖 − 1 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 42. EXAMPLE 𝑖 = 𝑛, 𝑘 = 𝑆 while 𝑖, 𝑘 > 0 if V 𝑖, 𝑘 ≠ 𝑉 1 − 𝑖, 𝑘 𝑖 = 𝑖 − 1, 𝑘 = 𝑘 − 𝑠𝑖 else 𝑖 = 𝑖 − 1 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)
  • 43. EXAMPLE The optimal knapsack should contain {1,2} = 7 is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Items (𝑠𝑖, 𝑣𝑖) 1: (2, 3) 2: (3, 4) 3: (4, 5) 4: (5, 6)

Editor's Notes

  1. 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
  2. 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
  3. Legend: Red – wala pud ko kasabot
  4. Solution is 1 pds A 3 pds B 1 pd C
  5. 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.
  6. 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.
  7. 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.
  8. i = 1 vi = 3 si = 2 s = 1 s - si = -1
  9. i = 1 vi = 3 si = 2 s = 2 s - si = 0
  10. i = 1 vi = 3 si = 2 s = 3 s - si = 1
  11. i = 1 vi = 3 si = 2 s = 4 s - si = 2
  12. i = 1 vi = 3 si = 2 s = 5 s - si = 3
  13. i = 2 vi = 4 si = 3 s = 1 s - si = -2
  14. i = 2 vi = 4 si = 3 s = 2 s - si = -1
  15. i = 2 vi = 4 si = 3 s = 3 s - si = 0
  16. i = 2 vi = 4 si = 3 s = 4 s - si = 1
  17. i = 2 vi = 4 si = 3 s = 5 s - si = 2
  18. i = 3 vi = 5 si = 4 s = 1…3
  19. i = 3 vi = 5 si = 4 s = 4 s - si = 0
  20. i = 3 vi = 5 si = 4 s = 5 s - si = 1
  21. i = 4 vi = 6 si = 5 s = 1…4
  22. i = 4 vi = 6 si = 5 s = 5 s - si = 0
  23. 𝑖 = 4 𝑘 = 5 𝑣 𝑖 =6 𝑠 𝑖 = 5 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =7
  24. 𝑖 = 4 𝑘 = 5 𝑣 𝑖 =6 𝑠 𝑖 = 5 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =7
  25. 𝑖 = 3 𝑘 = 5 𝑣 𝑖 =6 𝑠 𝑖 = 4 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =7
  26. 𝑖 = 1 𝑘 = 5 𝑣 𝑖 =4 𝑠 𝑖 = 3 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =3 𝑘− 𝑠 𝑖 =2
  27. 𝑖 = 1 𝑘 = 2 𝑣 𝑖 =3 𝑠 𝑖 = 2 𝑉 𝑖,𝑘 =3 𝑉 𝑖−1,𝑘 =0 𝑘− 𝑠 𝑖 =0
  28. 𝑖 = 0 𝑘 = 0 The optimal knapsack should contain {1, 2}
  29. 𝑖 = 0 𝑘 = 0 The optimal knapsack should contain {1, 2}