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

Knapsack problem dynamicprogramming

  • 1.
  • 2.
    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
  • 15.
    DYNAMIC PROGRAMMING Recursive formulafor 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 12 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 12 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 knapsackshould 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

  • #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
  • #13 Legend: Red – wala pud ko kasabot
  • #14 Solution is 1 pds A 3 pds B 1 pd C
  • #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.
  • #23 i = 1 vi = 3 si = 2 s = 1 s - si = -1
  • #24 i = 1 vi = 3 si = 2 s = 2 s - si = 0
  • #25 i = 1 vi = 3 si = 2 s = 3 s - si = 1
  • #26 i = 1 vi = 3 si = 2 s = 4 s - si = 2
  • #27 i = 1 vi = 3 si = 2 s = 5 s - si = 3
  • #28 i = 2 vi = 4 si = 3 s = 1 s - si = -2
  • #29 i = 2 vi = 4 si = 3 s = 2 s - si = -1
  • #30 i = 2 vi = 4 si = 3 s = 3 s - si = 0
  • #31 i = 2 vi = 4 si = 3 s = 4 s - si = 1
  • #32 i = 2 vi = 4 si = 3 s = 5 s - si = 2
  • #33 i = 3 vi = 5 si = 4 s = 1…3
  • #34 i = 3 vi = 5 si = 4 s = 4 s - si = 0
  • #35 i = 3 vi = 5 si = 4 s = 5 s - si = 1
  • #36 i = 4 vi = 6 si = 5 s = 1…4
  • #37 i = 4 vi = 6 si = 5 s = 5 s - si = 0
  • #38 𝑖 = 4 𝑘 = 5 𝑣 𝑖 =6 𝑠 𝑖 = 5 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =7
  • #39 𝑖 = 4 𝑘 = 5 𝑣 𝑖 =6 𝑠 𝑖 = 5 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =7
  • #40 𝑖 = 3 𝑘 = 5 𝑣 𝑖 =6 𝑠 𝑖 = 4 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =7
  • #41 𝑖 = 1 𝑘 = 5 𝑣 𝑖 =4 𝑠 𝑖 = 3 𝑉 𝑖,𝑘 =7 𝑉 𝑖−1,𝑘 =3 𝑘− 𝑠 𝑖 =2
  • #42 𝑖 = 1 𝑘 = 2 𝑣 𝑖 =3 𝑠 𝑖 = 2 𝑉 𝑖,𝑘 =3 𝑉 𝑖−1,𝑘 =0 𝑘− 𝑠 𝑖 =0
  • #43 𝑖 = 0 𝑘 = 0 The optimal knapsack should contain {1, 2}
  • #44 𝑖 = 0 𝑘 = 0 The optimal knapsack should contain {1, 2}