KEMBAR78
Knapsack problem dynamicprogramming | PPTX
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}