SlideShare a Scribd company logo
1 of 44
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

knapsack problem
knapsack problemknapsack problem
knapsack problemAdnan Malak
Β 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approachpadmeshagrekar
Β 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
Β 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
Β 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEMi i
Β 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmHoneyChintal
Β 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's AlgorithmTanmay Baranwal
Β 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and boundAbhishek Singh
Β 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comHemant Gautam
Β 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithmmansab MIRZA
Β 
Master theorem
Master theoremMaster theorem
Master theoremfika sweety
Β 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
Β 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problemharsh kothari
Β 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithmtaimurkhan803
Β 
CLOSEST PAIR (Final)
CLOSEST PAIR (Final)CLOSEST PAIR (Final)
CLOSEST PAIR (Final)Aruneel Das
Β 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
Β 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
Β 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithmPankaj Thakur
Β 

What's hot (20)

knapsack problem
knapsack problemknapsack problem
knapsack problem
Β 
Knapsack
KnapsackKnapsack
Knapsack
Β 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
Β 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Β 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
Β 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
Β 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Β 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
Β 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
Β 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
Β 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
Β 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
Β 
Master theorem
Master theoremMaster theorem
Master theorem
Β 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
Β 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
Β 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
Β 
CLOSEST PAIR (Final)
CLOSEST PAIR (Final)CLOSEST PAIR (Final)
CLOSEST PAIR (Final)
Β 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
Β 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
Β 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
Β 

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.pptxMd. 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 linealjhonatanVsquezArriag
Β 
TRANSFORMACIONES LINEALES
TRANSFORMACIONES LINEALESTRANSFORMACIONES LINEALES
TRANSFORMACIONES LINEALESWilson Quinatoa
Β 
Functions of severable variables
Functions of severable variablesFunctions of severable variables
Functions of severable variablesSanthanam Krishnan
Β 
Gauss elimination & Gauss Jordan method
Gauss elimination & Gauss Jordan methodGauss elimination & Gauss Jordan method
Gauss elimination & Gauss Jordan methodNaimesh Bhavsar
Β 
Yr7-AlgebraicExpressions (1).pptx
Yr7-AlgebraicExpressions (1).pptxYr7-AlgebraicExpressions (1).pptx
Yr7-AlgebraicExpressions (1).pptxPremkumarLetchumanan
Β 
Factoring common monomial
Factoring common monomialFactoring common monomial
Factoring common monomialAjayQuines
Β 
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 EquationsQUESTJOURNAL
Β 
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.pptxAngeloReyes58
Β 
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 SeriesShubham Sharma
Β 
PO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptxPO_groupVI_Term assignment.pptx
PO_groupVI_Term assignment.pptxPATELPUNIT2
Β 
Reducible equation to quadratic form
Reducible equation to quadratic formReducible equation to quadratic form
Reducible equation to quadratic formMahrukhShehzadi1
Β 
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.pptxG2018ChoudhariNikita
Β 
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

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto GonzΓ‘lez Trastoy
Β 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
Β 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
Β 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
Β 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
Β 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
Β 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
Β 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
Β 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
Β 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
Β 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
Β 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
Β 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
Β 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
Β 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
Β 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
Β 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
Β 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
Β 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
Β 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Β 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Β 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
Β 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
Β 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
Β 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Β 
Call Girls In Mukherjee Nagar πŸ“± 9999965857 🀩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar πŸ“±  9999965857  🀩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar πŸ“±  9999965857  🀩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar πŸ“± 9999965857 🀩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Β 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Β 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
Β 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
Β 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
Β 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
Β 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Β 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
Β 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Β 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
Β 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
Β 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
Β 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
Β 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
Β 

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}