0-1 Knapsack problemKnapsack Problem
You are a mischievous child camping
in the woods. You would like to steal
items from other campers, but you
can only carry so much mass in your
knapsack. You see seven items worth
stealing. Each item has a certain
mass and monetary value. How do
you maximize your profit so you
can buy more video games
later?Knapsack Problem
> Value and mass of each
item is given
> Maximize profit
> Subject to mass
constraint of knapsack:
15 kg
> Being a smart kid, you
apply dynamic
programming.Solution
oo
8Sensitivity Analysis
> Valuables are not very valuable when camping, pick
richer people to steal from.
> Suddenly a valuable item of $15 that weighs 11kg is
available, will the optimal solution change?
Yes, the maximum profit becomes $23Knapsack problem
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
Item #(i) Weight(w;) Value(b;)
1 1 8
2 3 6
3 5 §0-1 Knapsack problem
Weight —_ Benefit value
Items Wi b,
mm} 3
This is a knapsack || 3 4
Max weight: W = 20 Hil 4 5
5 8
ve»|
L] ° °Knapsack problem
There are two versions of the problem:
1. “0-1 knapsack problem”
+ Items are indivisible; you either take an item or not. Some
special instances can be solved with dynamic programming
2. “Fractional knapsack problem”
+ Items are divisible: you can take any fraction of an item0-1 Knapsack problem
* Given a knapsack with maximum capacity W, and a set S consisting of
nitems
* Each item / has some weight w, and benefit value 6, (all w, b,and W
are integer values)
Problem: How to pack the knapsack to achieve
TEPC Me TLR cyte0-1 Knapsack problem
* Problem, in other words, is to find
max )"b, subject to
ieT
The problem is called a “0-7” problem,
because each item must be entirely
accepted or rejected.Brute-force approach
Let’s first solve this problem with a
straightforward algorithm
* Since there are n items, there are 2” possible
combinations of items.
* We go through all combinations and find the one
with maximum value and with total weight less
or equal to W
* Running time will be O/2")Dynamic programming approach
* We can do better with an algorithm based
on dynamic programming
* We need to carefully identify the
subproblemsDefining a Subproblem
If items are labeled 1..n, then a subproblem would be to find
an optimal solution for S, = {items labeled 1, 2, .. k}
* This is a reasonable subproblem definition.
* The question is:
Can we describe the final solution (S,,, ) in terms of subproblems (S,)?
* Unfortunately, we can’t do that.
ically, th i mization problem for S,,, might NOT
contain the optimal solution from problem S,.Knapsack 0-1 Problem
* Let’s illustrate that point with an example:
item Weight Value
ly 3 10
1 8 4
in 9 9 \s
Is 8 a
* The maximum weight the knapsack can hold is 20.
* The best set of items from {Io, !1, |y} iS {lo, I, Io}
* BUT the best set of items from {lo, |, I5, Ig} iS {lo, Io, Is}.
* In this example, note that this optimal solution, {lp, !,, |5},
does NOT build upon the previous optimal solution, {lo, !;, ls}:
* (Instead it builds upon the solution, (|, |,}, which is really the optimal
subset of {lg |,,|,} with weight 12 or less.)Defining a Subproblem
* Let’s add another parameter: w, which will represent
the maximum weight for each subset of items
* The subproblem then will be to compute V/k,wJ, i.e.,
to find an optimal solution for S, = {items labeled 1,
2, .. k} in a knapsack of size w.
* Assuming knowing V{I, j], where i=0,1, 2, ... k-1,
j=0,1,2, ...w, how to derive V[k,w]?Recursive Formula
* The best subset of 5; that has the total weight < w, either
contains item / or not.
* First case: w;>w. Item / can’t be part of the solution, since if it
was, the total weight would be > w, which is unacceptable.
* Second case: w, < w. Then the item j can be in the solution, and
we choose the case with greater value.0-1 Knapsack Algorithm
forw=0toW
V[0,w] = 0
fori=1ton
Vii
fori=1ton
forw=1toW
if w, <= w// item i can be part of the solution
if b, + V[i-1,w-w]] > V[i-1,w]
Vii,w] = b, + V[i-2,w- wi)
else
V[i.w] = Vii-1.w]
else V[i,w] = V[i-1,w] // w,>wRunning Time — What is the running time of this algorithm?
O(n*W)
forw=0toW
V[0,w] = 0 oe
fori=1ton O(n)
V[i,0] =0
ferl = dton Repeat n times
forw=1toW
if w, <= w// item ican be part of the solution o(w)
if b, + V[i-1,w-w] > V[i-1,w])
V{iw] = b, + V[i-1,w- wi]
else Saat Remember that the
i,w] = V[i-1,w) Meer atTee alGGri
else V[i,w] = V[i-1,w] //w,>w or inExample
Let’s run our algorithm on the
following data:
n=4 (# of elements)
W =5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)Example
iW oO - 2
lo
ls
ln
0; 0}| 0 0 0
Ree
for w= 0 to W
V[0,w] = 0Example
Wo 1 2 3) 4 5
0 | 0 0
1 0
2 0
3 | 0
4| 0
fori=1ton
V[i,0] = 0Example
Woo 1 2) 3) 4
of o[o]olfolfo
1 {0 |*o
2/0
3 0
4[Lo
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solution
if bj + VLi-l.w-wjl > Viielw)
Viinw] = b, + Vli-l.w- wal
else
Viinw] = Viel w)
else V[i,
Vii-l,w] fw, >w
Vf1,1] = V10,1)
oeExample
Woo 1 2) 3 4S
0 oto | 0 ofojo
1| 0 | 0 3
2/0
3 0
4[o
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solu
if by + Vii-l,w-wjl > Vii-lw] 34V10,0] > V{0,2]
V[i,w] = b, + Vfi-L.w- wi] 3. >0
else .
Vii.w] = V[i-1.w]
else V[i,w] = Vii-I,w] // w, > wExample
Wo 1 2 23 4 5
0 | 0 oo [0 00
rio] o | 3 %3
2/0
3 0
4[o
fori=1ton
for w= 1toW
if w, <= w // item i can be part of the solut
if b, + V[i-I.w-w,] > Viielw] 34V10,11 >
Viisw] = b, + Vli-Lw- wi) 340 >
else
Viiwl = Viel wh
else V[iww] = Vii-Lw] // w,>wExample
Woo 1 2) 3 4S
0 [0 0 to fo 0
1} o]o]3 | 3 3
2| 0
a)
4 [0
fori=1ton
if w Vi-l.w) 34¥(0,2] > VI0,4]
Viiw] =b, + Vii-Lw- wi] 340 >0
else =
vi
else VIi,w] =
Li-Lw]
Lw] fw, >Example
Woo 1 2) 3 4S
0 [0 0 oo [0
1{o][o/3 [3 | 3 %3
2 [0
3 0
4 [0
fori=1ton
for w=1toW
if w; <= w/// item i can be part of the solution
if, + Vii-l,w-w]> Vii-lw] — [3syjo3} > vios)
Viiwl = b, + Vli-Lw- wi) 340 > 0
else =
Vfiw = Vii-lw]
else V[i,w] = Vii
ww] fiw,>wExample
Wo 1 2 3) 4 5
ol o oj|/ofolo
r/o], | 3 )3 [3/3
2 [0 |
3 0
4[Lo
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solution
if b, + Vli-L,w-w, > View]
Viiw] = b, + Vii-Lw- w,]
else
Viiww] = Vii-L.w]
else V[i,w] = V[i-l,w] //w, > w
V2.1) = ViL4Example
i\W
oO 1 2 3 4 5
of o] 0 0 [0
1fofo] 3 [3 ]3 [3
2{ 0] 0 [%
3 0
4[o
fori=1ton
forw=1toW
if w, <= w //item i can be part of the solution.
if b, + Vli-lw-w,l > Viel wl
Viiww] = b, + Vii-L.w- wi]
else
View] = Vii-lw) Viz2)=vina)
else V[i,w] = V[i-lw] //w,> w oeExample
Woo 1 2) 3 4S
ofolofolofolj|o
1 Ono | 3 | 3 3.13
2}/o0|0/3 74
3 0
4[Lo
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solution
b, + VLi-l.w-w] > VIi-lw] 44V[1, 1)
VIi.w] = b; + V[i-L,w- wi) 4
else
Vii.w] = Vli-Lw]
else V[i,w] = Vli-l,w] // w,> wExample
Woo 1 2) 3 4S
ofolo]fojfofojo
1/o0;/ot3 ] 3/3 /3
foto ls pata
3 0
4 [0
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solution.
if by + Vii-lw-w)> Vii-lw) — [4*vita) > via)
V[i.w] = b; + V[i-l.w- wi] 4. *
else
V[i.w] = Vfi-1.w]
else V[i,w] = Vii-l,w] // w, > wExample
i\W
0 1 2 3 4 5
0 | 0 0 0 0 0 0
1 0 0 a ee 3
2 0 0 3 4 4 7
3 0
4|0
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solution
if b, + Vii-l,w-w,] > Vii-Lw] Reena
Vli,w] = b, + VLi-l.w- Wj]
else
Vii,w] = Vii-Lw]
else Vii,w] = Vii-L.w] /w, > wExample
Woo 1 2) 3 4S
ofoflo]lofofolo
1fo[o/3/3 {3 | 3
2[o0f[ 0/3 [4 [4/7
3[ 0 ]* [%3 |*4
4 [0
fori=1ton
forw=1toW
if w, <= w // item i can be part of the solution
if b; + Vli-L,w-w,l > Vii-Lw]
Viiww] = by + Vii-Lw- wi)
else
Viiw] = View]
else V[i,w] = V{i-l,w] “/ w, > w
Vi3,31= Vi2,3)Example
oO 1 2 3 4 5
o|o|o]o/}o0]o0]|o0
1}/o0/o0/3 )3 |3 )3
2 ono ts fs | 417
3|0]|0 {3 [4s
4| 0
fori=1ton
for w=1toW
if w, <= w // item i can be part of the solution
if b; + V[i-1,w-w,] > V[i-l.w] 5+V(2,0] > V[2,4]
Viiew] =, + VIi-Lw- wi] 510 > 4
else =
Viiw] = Viielw]
else Vfi,w] = V[i-l,w] /w,> wExample
Woo 1 2) 3 4S
ofolofolofolj|o
1}o]o]3 )3 [3/3
2 0 0 3 4 4 rt
3 0 0 3 4 5 7
4[Lo
fori=1ton
for w=1toW
if w, <= w///item i can be part of the solution
if b; + Vli-L.w-w,l > Vli-Lw]
Viinw] = b; + Viel w- wi]
else
Viiw] = Vii-Lw]
else V[i,w] = V{i-
w] fw, > w
54VI2,1] > VI2,5]
540 > 7
Vi3.4] = 7i_ wb,
Example 1 (2,3)
2: 3,4)
Wo 1 2 3 4 5 jeg 2% 4S)
ofofo]fTolololo|y5 #& 69
1fofol3 [3/3 /3 | ys
2fofo][s]f4fa4[7}] vig
af of] o [3 [4 [5 [7
4[o [40 ['3 [*4 |¥s
fori=1ton
forw=1toW
if w, <= w//item i can be part of the solution
if b; + VLielw-wjl > Vii-lw]
Viiaw] = b, + Vii-Lw- wi
else
v
else V[i,w
(i-1.w]
Iw] fw, > wItems:
i__w, b,
Example I: (2,3)
2: (3,4)
Wo 1 2 3 4 5 jeg 2% 45)
ofofo]folololo|ye5 #& 69
i
rfolo [3/3 ]3 [3 | V5
2,o/o]3|4/4 [7] \5
3 0 0 x 4 5 ¢ w- w.=0
4fofo[3 [4 [5 [%
fori=1ton
for w=1toW
if w, <= w// item i can be part of the solution
if, + VEi-l.w-w,] > VEi-lw] 6+V13,1] > VI3,5)
, + Vii-lw- wi) 610 >7
‘|= Vii-Lw]
i-Lw] dw, > wExercise
1. a, Apply the bottom-up dynamic programming algorithm to the following
instance of the knapsack problem:
3
oe oe
item | weight
value
$25
sx
S15
$40
$50
, capacity W = 6.
How to find out which items are in the optimal subset?Comments
* This algorithm only finds the max possible value that can be carried in
the knapsack
+ i.e., the value in V[n,W]
that ata) aT)
Solty tates layHow to find actual Knapsack Items
* All of the information we need is in the table.
* Vin,W] is the maximal value of items that can be
placed in the Knapsack.
* Let i=n and k=W
if V{i,k] # V[i-1,k] then
mark the /* item as in the knapsack
i=i-l, k= kw,
else
i=i-1 // Assume the i item is not in the knapsack
// Could it be in the optimally packed knapsack?Items:
iw, b
Finding the Items I: (2,3)
2: (3,4)
Wo 1 2 3 4 5 ing 3 5)
ofofo]oflofo!o jes # G,6)
1{o0]o|3 | 3 [3 [3 | ba
2fo0[o0/3 |4 [4/7 |»
3, o0];o0 [3 [4 ]s5 [7 |
afolo[3[4[s [7] WtItems:
- iw by
Finding the Items I: (2,3)
2: G4)
Wo 1 2 3 4 5 ing 3 GS)
ofofo]ojlo]o!o jes *# G,6)
1{o]o/3 /3 | 3 | 3 | b=
2/0] 0 | 3 4 [4 | 7 | wes
3/ofo ]3)4]s5 Gy Vii Kl = VI4,5]=7
4[o[o]3 [4 [5 [\7)] Wi-+4-vB5)=7Items:
- iw by
Finding the Items I: (2,3)
2: (3,4)
iW 9 1 2 3 4 5 3: (4,5)
ofolofolofolj|o 4: (5,6)
1}o]o]3 )3 /3 )3
2{/o0[o0 [3 |4]|4 |f/7
3| 0] 0 | 3 | 4 | 5 |\7/| WiM=Vi35]-7
alolo 3 4 5 7 | VWi-LK=VI2,5] =7Items:
iw, b
Finding the Items I: (2,3)
2: (3,4)
wo 1 2 3 4 5 i > 45)
ofofolofo]o]o ls # 66
1; o]o eH) b=
0 0 3 4 4 we.
@ ofo [3 [4 [5 | 7_| VéK-vesb7
V{i-1,k]=V[1,5] =3
4fo lo 13 1415 17 Pes
i=n, k=W
while i,k > 0
if V/i.k) # V/i-Z,k] then:
mark the # item as in the knapsack
isi kekw;
else
isitFinding the Items
i\W
0
Items:
iw, b
1? (2,3)
2: (3,4)
3:
4:
slolclolo
clolelol-
@
isn, keW
while i,k > 0
if V/i.k) # V/i-Z,k] then:
mark the # item as in the knapsack
isi kekw;
else
isitItems:
iw, b
Finding the Items I: (2,3)
2: G4)
Wo 1 Py ae ee ee)
of o]o 19 | 0 | 0 | xa2 # 6,6)
0 | 0 3 [3 | 3 | b=
0 | 0 4 [4 [7 |
3/o0lo13 1415 17 | Vdl=viL2ie3
alolo 3 4 5 7 V[i-1,k]=V(0,2] =0
k-w,=0Items:
iw by
Finding the Items I: (2,3)
2: G4)
Wo 1 Py ae ee ee)
of o]o 19 | 0 | 0 | xa2 # 6,6)
0 | 0 3 [3 | 3 | b=
0 | 0 4 [4 [7 |
3/o0lo13 1415 17 | Védl=VviL2ie3
alolo 3 4 5 7 V[i-1,k]=V(0,2] =0
k-w,=0Items:
iw, b
Finding the Items I: (2,3)
2: G4)
Wo 1 2 3 4 5 io 3% 5)
ofolo]folo]fo]o|kro # 6,6)
ofo {3 [3/3 ]3
ofo/3 [4 [4/7
3 0 0 3 4 5 7 The optimal
knapsack
# o i 3 4: 5 7 should contain
isn, k=W (1,2)
while ik > 0
if Vfi.k] # V[iL.K] then
mark the n™
fsihk
else
tsi
mas in the knapsackItems:
iw by
Finding the Items I: (2,3)
2: (3,4)
Wo 1 3: (4,5)
0 0 0 0 4: (5,6)
0 0 3
0 0 2
3/0] 0 } 3 The optimal
afolols3 knapsack
else
mark the n™
isik
isit
mas in the knapsack
should contain
(1,2)Memorization (Memory Function Method)
* Goal:
+ Solve only subproblems that are necessary and solve it only once
+ Memorization is another way to deal with overlapping subproblems
in dynamic programming
* With memorization, we implement the algorithm recursively:
* If we encounter a new subproblem, we compute and store the solution.
+ If we encounter a subproblem we have seen, we look up the answer
* Most useful when the algorithm is easiest to implement recursively
+ Especially if we do not need solutions to all subproblems.Conclusion
* Dynamic programming is a useful technique of solving
certain kind of problems
* When the solution can be recursively described in terms of
partial solutions, we can store these partial solutions and
re-use them as necessary (memorization)
* Running time of dynamic programming algorithm vs. naive
algorithm:
* 0-1 Knapsack problem: O(W*n) vs. O(2")