RAIK 283
Data Structures & Algorithms
Knapsack 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 #
1
2
3
Weight
1
3
5
Value
8
6
5
3
Knapsack problem
There are two versions of the problem:
1. 0-1 knapsack problem
max bi subject to wi W
iT
iT
Defining a Subproblem
Defining a Subproblem
Defining a Subproblem
If items are labeled 1..n, then a subproblem would
be to find an optimal solution for Sk = {items
labeled 1, 2, .. k}
Defining a Subproblem
w1 =2 w2 =4
b1 =3 b2 =5
w3 =5
b3 =8
Weight Benefit
w4 =3
b4 =4
?
Max weight: W = 20
For S4:
Total weight: 14
Maximum benefit: 20
w1 =2 w2 =4
b1 =3 b2 =5
w3 =5
b3 =8
w5 =9
b5 =10
For S5:
Total weight: 20
Maximum benefit: 26
wi
bi
10
Item
#
S4
S5
Solution for S4 is
not part of the
solution for S !!!
12
Defining a Subproblem
13
Defining a Subproblem
14
Defining a Subproblem
15
16
or
2) the best subset of Sk-1 that has total weight w-wk plus
the item k
17
Recursive Formula
V [k 1, w]
V [ k , w]
if wk w
19
Running time
for w = 0 to W
O(W)
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
Repeat n
for i = 1 to n
for w = 0 to W
O(W)
< the rest of the code >
times
20
Example
Lets 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)
21
Example (2)
i\W 0
0
0
1
2
3
4
1
0
2
0
3
0
4
0
5
0
for w = 0 to W
V[0,w] = 0
22
Example (3)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
2
0
3
0
4
0
5
0
for i = 1 to n
V[i,0] = 0
23
Example (4)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
2
0
3
0
4
0
5
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=1
bi=3
wi=2
w=1
w-wi =-1
Example (5)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
2
0
3
3
0
4
0
5
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=1
bi=3
wi=2
w=2
w-wi =0
Example (6)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
2
0
3
3
0
3
4
0
5
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=1
bi=3
wi=2
w=3
w-wi =1
Example (7)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
2
0
3
3
0
3
4
0
3
5
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=1
bi=3
wi=2
w=4
w-wi =2
Example (8)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
2
0
3
3
0
3
4
0
3
5
0
3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=1
bi=3
wi=2
w=5
w-wi =3
Example (9)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
2
0
3
3
0
3
4
0
3
5
0
3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=2
bi=4
wi=3
w=1
w-wi =-2
Example (10)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
2
0
3
3
3
0
3
4
0
3
5
0
3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=2
bi=4
wi=3
w=2
w-wi =-1
Example (11)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
2
0
3
3
3
0
3
4
4
0
3
5
0
3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=2
bi=4
wi=3
w=3
w-wi =0
Example (12)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
2
0
3
3
3
0
3
4
4
0
3
4
5
0
3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=2
bi=4
wi=3
w=4
w-wi =1
Example (13)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
2
0
3
3
3
0
3
4
4
0
3
4
5
0
3
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=2
bi=4
wi=3
w=5
w-wi =2
Example (14)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
2
0
3
3
3
3
0
3
4
4
4
0
3
4
5
0
3
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=3
bi=5
wi=4
w= 1..3
Example (15)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
2
0
3
3
3
3
0
3
4
4
4
0
3
4
5
5
0
3
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=3
bi=5
wi=4
w= 4
w- wi=0
Example (16)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
2
0
3
3
3
3
0
3
4
4
4
0
3
4
5
5
0
3
7
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=3
bi=5
wi=4
w= 5
w- wi=1
Example (17)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=4
bi=6
wi=5
w= 1..4
Example (18)
i\W
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=4
bi=6
wi=5
w= 5
w- wi=0
Exercise
Comments
40
41
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i 1,k] =7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
mark the ith item as in the knapsack
i = i 1, k = k-wi
else
i = i 1
42
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i 1,k] =7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
mark the ith item as in the knapsack
i = i 1, k = k-wi
else
i = i 1
43
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=3
k= 5
bi=5
wi=4
V[i,k] = 7
V[i 1,k] =7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
mark the ith item as in the knapsack
i = i 1, k = k-wi
else
i = i 1
44
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=2
k= 5
bi=4
wi=3
V[i,k] = 7
V[i 1,k] =3
k wi=2
45
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
i=1
k= 2
bi=3
wi=2
V[i,k] = 3
V[i 1,k] =0
k wi=0
46
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
i=0
k= 0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
The optimal
knapsack
should
contain {1, 2}
47
0
0
0
0
0
0
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
i=n, k=W
while i,k > 0
if V[i,k] V[i 1,k] then
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
The optimal
knapsack
should
contain {1, 2}
48
Goal:
Solve only subproblems that are necessary and solve it only once
Conclusion
51
In-Class Exercise
52
Brute-Force Approach
53
53
Dynamic-Programming Approach
(1) SMaxV(0) = 0
(2) MaxV(0) = 0
(3) for i=1 to n
(4) SMaxV(i) = max(SmaxV(i-1)+xi, 0)
(5) MaxV(i) = max(MaxV(i-1), SMaxV(i))
(6) return MaxV(n)
Run the algorithm on the following example
instance:
30, 40, -100, 10, 20, 50, -60, 90, -180, 100
54
54