尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
1
Knapsack problem
Data Structures & Algorithms
2
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 # Weight Value
1 1 8
2 3 6
3 5 5
Knapsack problem
3
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
1. “Fractional knapsack problem”
 Items are divisible: you can take any fraction of an item
4
 Given a knapsack with maximum capacity W, and
a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
0-1 Knapsack problem
5
 Problem, in other words, is to find
∑∑ ∈∈
≤
Ti
i
Ti
i Wwb subject tomax
0-1 Knapsack problem
The problem is called a “0-1” problem,
because each item must be entirely
accepted or rejected.
6
Let’s first solve this problem with a
straightforward algorithm
 Since there are n items, there are 2n
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(2n
)
0-1 Knapsack problem:
brute-force approach
7
 We can do better with an algorithm based on
dynamic programming
 We need to carefully identify the subproblems
0-1 Knapsack problem:
dynamic programming approach
8
 Given a knapsack with maximum capacity W, and
a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
Defining a Subproblem
9
 We can do better with an algorithm based on
dynamic programming
 We need to carefully identify the subproblems
Let’s try this:
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
10
If items are labeled 1..n, then a subproblem would be
to find an optimal solution for Sk = {items labeled
1, 2, .. k}
 This is a reasonable subproblem definition.
 The question is: can we describe the final solution
(Sn ) in terms of subproblems (Sk)?
 Unfortunately, we can’t do that.
Defining a Subproblem
11
Max weight: W = 20
For S4:
Total weight: 14
Maximum benefit: 20
w1 =2
b1 =3
w2 =4
b2 =5
w3 =5
b3 =8
w4 =3
b4 =4 wi bi
10
85
54
43
32
Weight Benefit
9
Item
#
4
3
2
1
5
S4
S5
w1 =2
b1 =3
w2 =4
b2 =5
w3 =5
b3 =8
w5 =9
b5 =10
For S5:
Total weight: 20
Maximum benefit: 26
Solution for S4 is
not part of the
solution for S !!!
?
Defining a Subproblem
12
 As we have seen, the solution for S4 is not part of the
solution for S5
 So our definition of a subproblem is flawed and we
need another one!
Defining a Subproblem
13
 Given a knapsack with maximum capacity W, and
a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
Defining a Subproblem
14
 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,w], i.e.,
to find an optimal solution for Sk = {items labeled 1,
2, .. k} in a knapsack of size w
Defining a Subproblem
15
 The subproblem will then be to compute V[k,w], i.e.,
to find an optimal solution for Sk = {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 for
subproblems
16
It means, that the best subset of Sk that has total
weight w is:
1) the best subset of Sk-1 that has total weight ≤ w, or
2) the best subset of Sk-1 that has total weight ≤ w-wk plus the
item k



+−−−
>−
=
else}],1[],,1[max{
if],1[
],[
kk
k
bwwkVwkV
wwwkV
wkV
Recursive formula for subproblems:
Recursive Formula for
subproblems (continued)
17
Recursive Formula
The best subset of Sk that has the total weight ≤ w,
either contains item k or not.
First case: wk>w. Item k can’t be part of the solution,
since if it was, the total weight would be > w, which
is unacceptable.
Second case: wk ≤ w. Then the item k can be in the
solution, and we choose the case with greater value.



+−−−
>−
=
else}],1[],,1[max{
if],1[
],[
kk
k
bwwkVwkV
wwwkV
wkV
18
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0-1 Knapsack Algorithm
19
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
< the rest of the code >
What is the running time of this
algorithm?
O(W)
O(W)
Repeat n times
O(n*W)
Remember that the brute-force algorithm
takes O(2n
)
Running time
20
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
21
for w = 0 to W
V[0,w] = 0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
Example (2)
22
for i = 1 to n
V[i,0] = 0
0
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
Example (3)
23
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
0
i=1
bi=3
wi=2
w=1
w-wi =-1
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
0
0
0
Example (4)
24
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=2
w-wi =0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (5)
25
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=3
w-wi =1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3
Example (6)
26
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=4
w-wi =2
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3 3
Example (7)
27
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=5
w-wi =3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3 3 3
Example (8)
28
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=1
w-wi =-2
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (9)
29
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=2
w-wi =-1
3 3 3 3
3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0
Example (10)
30
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=3
w-wi =0
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
43
Example (11)
31
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=4
w-wi =1
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
43 4
Example (12)
32
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=5
w-wi =2
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
73 4 4
Example (13)
33
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 1..3
3 3 3 3
0 3 4 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
7
3 40
Example (14)
34
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 4
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (15)
35
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 5
w- wi=1
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
5 7
Example (16)
36
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=4
bi=6
wi=5
w= 1..4
3 3 3 3
0 3 4 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
7
3 40
70 3 4 5
5
Example (17)
37
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=4
bi=6
wi=5
w= 5
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
5
7
7
0 3 4 5
Example (18)
38
Exercise
P303 8.2.1 (a).
How to find out which items are in the optimal subset?
39
Comments
 This algorithm only finds the max possible value
that can be carried in the knapsack
» i.e., the value in V[n,W]
 To know the items that make this maximum value,
an addition to this algorithm is necessary
40
 All of the information we need is in the table.
 V[n,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 ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 // Assume the ith
item is not in the knapsack
// Could it be in the optimally packed
knapsack?
How to find actual Knapsack
Items
41
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i−1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
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
5 7
0 3 4 5 7
Finding the Items
42
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i−1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
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
5 7
0 3 4 5 7
Finding the Items (2)
43
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=3
k= 5
bi=5
wi=4
V[i,k] = 7
V[i−1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
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
5 7
0 3 4 5 7
Finding the Items (3)
44
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
k= 5
bi=4
wi=3
V[i,k] = 7
V[i−1,k] =3
k − wi=2
3 3 3 3
0 3 4 4 7
0 3 4
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
5 7
0 3 4 5 7
7
Finding the Items (4)
45
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
k= 2
bi=3
wi=2
V[i,k] = 3
V[i−1,k] =0
k − wi=0
3 3 3 3
0 3 4 4 7
0 3 4
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
5 7
0 3 4 5 7
3
Finding the Items (5)
46
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the nth
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
i=0
k= 0
The optimal
knapsack
should contain
{1, 2}
Finding the Items (6)
47
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k] ≠ V[i−1,k] then
mark the nth
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
5 7
0 3 4 5 7
The optimal
knapsack
should contain
{1, 2}
7
3
Finding the Items (7)
48
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.
49
for i = 1 to n
for w = 1 to W
V[i,w] = -1
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
MFKnapsack(i, w)
if V[i,w] < 0
if w < wi
value = MFKnapsack(i-1, w)
else
value = max(MFKnapsack(i-1, w),
bi + MFKnapsack(i-1, w-wi))
V[i,w] = value
return V[i,w]
0-1 Knapsack Memory Function Algorithm
50
 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. naïve algorithm:
» 0-1 Knapsack problem: O(W*n) vs. O(2n
)
Conclusion
51
In-Class Exercise
52
Brute-Force Approach
Design and Analysis of Algorithms - Chapter 8 52
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
53

More Related Content

What's hot

Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
i i
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
rowntu
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
Rezwan Siam
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
Adnan Malak
 
Np complete
Np completeNp complete
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
Kumar
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
Waqar Akram
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Vertex cover Problem
Vertex cover ProblemVertex cover Problem
Vertex cover Problem
Gajanand Sharma
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
Rajendran
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
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
Abhishek Singh
 

What's hot (20)

Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
Np complete
Np completeNp complete
Np complete
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Vertex cover Problem
Vertex cover ProblemVertex cover Problem
Vertex cover Problem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
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
 

Viewers also liked

Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
Hemant Gautam
 
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
International Islamic University
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
Snehasis Panigrahi
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
Mohanlal Sukhadia University (MLSU)
 

Viewers also liked (6)

Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
 
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
 
Kruskal Algorithm
Kruskal AlgorithmKruskal Algorithm
Kruskal Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming0 1 knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 

Similar to Knapsack problem

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
lecture 25
lecture 25lecture 25
lecture 25
sajinsc
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
AyushJaiswal513854
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
Gaurav Dubey
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
Paras Patel
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1
Amit Kumar Rathi
 
Longest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 KnapsackLongest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 Knapsack
Asif Shahriar
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptxINNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
AvilosErgelaKram
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
Greedy algo revision 2
Greedy algo revision 2Greedy algo revision 2
Greedy algo revision 2
maamir farooq
 
AOA ppt.ppt
AOA ppt.pptAOA ppt.ppt
AOA ppt.ppt
SaimaShaheen14
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
Unit 3-Greedy Method
Unit 3-Greedy MethodUnit 3-Greedy Method
Unit 3-Greedy Method
DevaKumari Vijay
 
A0 integers ppt v16trial
A0 integers ppt v16trialA0 integers ppt v16trial
A0 integers ppt v16trial
scans
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
RacksaviR
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse students
DeepakGowda357858
 
Inner product space
Inner product spaceInner product space
Inner product space
SheharBano31
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
garishma bhatia
 

Similar to Knapsack problem (20)

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
lecture 25
lecture 25lecture 25
lecture 25
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1
 
Longest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 KnapsackLongest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 Knapsack
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
 
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptxINNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
 
Greedy algo revision 2
Greedy algo revision 2Greedy algo revision 2
Greedy algo revision 2
 
AOA ppt.ppt
AOA ppt.pptAOA ppt.ppt
AOA ppt.ppt
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
 
Unit 3-Greedy Method
Unit 3-Greedy MethodUnit 3-Greedy Method
Unit 3-Greedy Method
 
A0 integers ppt v16trial
A0 integers ppt v16trialA0 integers ppt v16trial
A0 integers ppt v16trial
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse students
 
Inner product space
Inner product spaceInner product space
Inner product space
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 

More from Vikas Sharma

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
Vikas Sharma
 
Coloring graphs
Coloring graphsColoring graphs
Coloring graphs
Vikas Sharma
 
Backtracking
Backtracking  Backtracking
Backtracking
Vikas Sharma
 
Backtracking
BacktrackingBacktracking
Backtracking
Vikas Sharma
 
Rules and steps for developing a software product (rsfsp) by vikas sharma
Rules and steps for developing a software product (rsfsp) by vikas sharmaRules and steps for developing a software product (rsfsp) by vikas sharma
Rules and steps for developing a software product (rsfsp) by vikas sharma
Vikas Sharma
 
Office automation system for scholl (oasfs) by vikas sharma
Office automation system for scholl (oasfs) by vikas sharmaOffice automation system for scholl (oasfs) by vikas sharma
Office automation system for scholl (oasfs) by vikas sharma
Vikas Sharma
 
Library and member management system (lamms) by vikas sharma
Library and member management system (lamms) by vikas sharmaLibrary and member management system (lamms) by vikas sharma
Library and member management system (lamms) by vikas sharma
Vikas Sharma
 
Website optimization by vikas sharma
Website optimization by vikas sharmaWebsite optimization by vikas sharma
Website optimization by vikas sharma
Vikas Sharma
 

More from Vikas Sharma (9)

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Coloring graphs
Coloring graphsColoring graphs
Coloring graphs
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Rules and steps for developing a software product (rsfsp) by vikas sharma
Rules and steps for developing a software product (rsfsp) by vikas sharmaRules and steps for developing a software product (rsfsp) by vikas sharma
Rules and steps for developing a software product (rsfsp) by vikas sharma
 
Office automation system for scholl (oasfs) by vikas sharma
Office automation system for scholl (oasfs) by vikas sharmaOffice automation system for scholl (oasfs) by vikas sharma
Office automation system for scholl (oasfs) by vikas sharma
 
Library and member management system (lamms) by vikas sharma
Library and member management system (lamms) by vikas sharmaLibrary and member management system (lamms) by vikas sharma
Library and member management system (lamms) by vikas sharma
 
Website optimization by vikas sharma
Website optimization by vikas sharmaWebsite optimization by vikas sharma
Website optimization by vikas sharma
 

Recently uploaded

220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
Kalna College
 
bryophytes.pptx bsc botany honours second semester
bryophytes.pptx bsc botany honours  second semesterbryophytes.pptx bsc botany honours  second semester
bryophytes.pptx bsc botany honours second semester
Sarojini38
 
Opportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive themOpportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive them
EducationNC
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
Kalna College
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
MattVassar1
 
pol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdfpol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdf
BiplabHalder13
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
shabeluno
 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
Derek Wenmoth
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Kalna College
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
PJ Caposey
 
Talking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual AidsTalking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual Aids
MattVassar1
 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
whatchangedhowreflec
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Celine George
 
A Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by QuizzitoA Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by Quizzito
Quizzito The Quiz Society of Gargi College
 
8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity
RuchiRathor2
 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
ShwetaGawande8
 

Recently uploaded (20)

220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
 
bryophytes.pptx bsc botany honours second semester
bryophytes.pptx bsc botany honours  second semesterbryophytes.pptx bsc botany honours  second semester
bryophytes.pptx bsc botany honours second semester
 
Opportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive themOpportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive them
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
 
pol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdfpol sci Election and Representation Class 11 Notes.pdf
pol sci Election and Representation Class 11 Notes.pdf
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
 
Talking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual AidsTalking Tech through Compelling Visual Aids
Talking Tech through Compelling Visual Aids
 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
 
A Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by QuizzitoA Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by Quizzito
 
8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity
 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
 

Knapsack problem

  • 2. 2 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 # Weight Value 1 1 8 2 3 6 3 5 5 Knapsack problem
  • 3. 3 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 1. “Fractional knapsack problem”  Items are divisible: you can take any fraction of an item
  • 4. 4  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? 0-1 Knapsack problem
  • 5. 5  Problem, in other words, is to find ∑∑ ∈∈ ≤ Ti i Ti i Wwb subject tomax 0-1 Knapsack problem The problem is called a “0-1” problem, because each item must be entirely accepted or rejected.
  • 6. 6 Let’s first solve this problem with a straightforward algorithm  Since there are n items, there are 2n 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(2n ) 0-1 Knapsack problem: brute-force approach
  • 7. 7  We can do better with an algorithm based on dynamic programming  We need to carefully identify the subproblems 0-1 Knapsack problem: dynamic programming approach
  • 8. 8  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? Defining a Subproblem
  • 9. 9  We can do better with an algorithm based on dynamic programming  We need to carefully identify the subproblems Let’s try this: 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
  • 10. 10 If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}  This is a reasonable subproblem definition.  The question is: can we describe the final solution (Sn ) in terms of subproblems (Sk)?  Unfortunately, we can’t do that. Defining a Subproblem
  • 11. 11 Max weight: W = 20 For S4: Total weight: 14 Maximum benefit: 20 w1 =2 b1 =3 w2 =4 b2 =5 w3 =5 b3 =8 w4 =3 b4 =4 wi bi 10 85 54 43 32 Weight Benefit 9 Item # 4 3 2 1 5 S4 S5 w1 =2 b1 =3 w2 =4 b2 =5 w3 =5 b3 =8 w5 =9 b5 =10 For S5: Total weight: 20 Maximum benefit: 26 Solution for S4 is not part of the solution for S !!! ? Defining a Subproblem
  • 12. 12  As we have seen, the solution for S4 is not part of the solution for S5  So our definition of a subproblem is flawed and we need another one! Defining a Subproblem
  • 13. 13  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items? Defining a Subproblem
  • 14. 14  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,w], i.e., to find an optimal solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w Defining a Subproblem
  • 15. 15  The subproblem will then be to compute V[k,w], i.e., to find an optimal solution for Sk = {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 for subproblems
  • 16. 16 It means, that the best subset of Sk that has total weight w is: 1) the best subset of Sk-1 that has total weight ≤ w, or 2) the best subset of Sk-1 that has total weight ≤ w-wk plus the item k    +−−− >− = else}],1[],,1[max{ if],1[ ],[ kk k bwwkVwkV wwwkV wkV Recursive formula for subproblems: Recursive Formula for subproblems (continued)
  • 17. 17 Recursive Formula The best subset of Sk that has the total weight ≤ w, either contains item k or not. First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable. Second case: wk ≤ w. Then the item k can be in the solution, and we choose the case with greater value.    +−−− >− = else}],1[],,1[max{ if],1[ ],[ kk k bwwkVwkV wwwkV wkV
  • 18. 18 for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 0-1 Knapsack Algorithm
  • 19. 19 for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W < the rest of the code > What is the running time of this algorithm? O(W) O(W) Repeat n times O(n*W) Remember that the brute-force algorithm takes O(2n ) Running time
  • 20. 20 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
  • 21. 21 for w = 0 to W V[0,w] = 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW Example (2)
  • 22. 22 for i = 1 to n V[i,0] = 0 0 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW Example (3)
  • 23. 23 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 0 i=1 bi=3 wi=2 w=1 w-wi =-1 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 0 0 0 Example (4)
  • 24. 24 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=2 w-wi =0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w Example (5)
  • 25. 25 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=3 w-wi =1 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3 Example (6)
  • 26. 26 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=4 w-wi =2 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3 3 Example (7)
  • 27. 27 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=5 w-wi =3 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3 3 3 Example (8)
  • 28. 28 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=1 w-wi =-2 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w Example (9)
  • 29. 29 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=2 w-wi =-1 3 3 3 3 3 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 0 Example (10)
  • 30. 30 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=3 w-wi =0 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 43 Example (11)
  • 31. 31 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=4 w-wi =1 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 43 4 Example (12)
  • 32. 32 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=5 w-wi =2 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 73 4 4 Example (13)
  • 33. 33 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 1..3 3 3 3 3 0 3 4 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 7 3 40 Example (14)
  • 34. 34 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 4 w- wi=0 3 3 3 3 0 3 4 4 7 0 3 4 5 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w Example (15)
  • 35. 35 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 5 w- wi=1 3 3 3 3 0 3 4 4 7 0 3 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 5 7 Example (16)
  • 36. 36 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 bi=6 wi=5 w= 1..4 3 3 3 3 0 3 4 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 7 3 40 70 3 4 5 5 Example (17)
  • 37. 37 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 bi=6 wi=5 w= 5 w- wi=0 3 3 3 3 0 3 4 4 7 0 3 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 5 7 7 0 3 4 5 Example (18)
  • 38. 38 Exercise P303 8.2.1 (a). How to find out which items are in the optimal subset?
  • 39. 39 Comments  This algorithm only finds the max possible value that can be carried in the knapsack » i.e., the value in V[n,W]  To know the items that make this maximum value, an addition to this algorithm is necessary
  • 40. 40  All of the information we need is in the table.  V[n,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 ith item as in the knapsack i = i−1, k = k-wi else i = i−1 // Assume the ith item is not in the knapsack // Could it be in the optimally packed knapsack? How to find actual Knapsack Items
  • 41. 41 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 k= 5 bi=6 wi=5 V[i,k] = 7 V[i−1,k] =7 3 3 3 3 0 3 4 4 7 0 3 4 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 5 7 0 3 4 5 7 Finding the Items
  • 42. 42 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 k= 5 bi=6 wi=5 V[i,k] = 7 V[i−1,k] =7 3 3 3 3 0 3 4 4 7 0 3 4 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 5 7 0 3 4 5 7 Finding the Items (2)
  • 43. 43 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 k= 5 bi=5 wi=4 V[i,k] = 7 V[i−1,k] =7 3 3 3 3 0 3 4 4 7 0 3 4 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 5 7 0 3 4 5 7 Finding the Items (3)
  • 44. 44 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 k= 5 bi=4 wi=3 V[i,k] = 7 V[i−1,k] =3 k − wi=2 3 3 3 3 0 3 4 4 7 0 3 4 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 5 7 0 3 4 5 7 7 Finding the Items (4)
  • 45. 45 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 k= 2 bi=3 wi=2 V[i,k] = 3 V[i−1,k] =0 k − wi=0 3 3 3 3 0 3 4 4 7 0 3 4 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 5 7 0 3 4 5 7 3 Finding the Items (5)
  • 46. 46 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k] ≠ V[i−1,k] then mark the nth item as in the knapsack i = i−1, k = k-wi else i = i−1 5 7 0 3 4 5 7 i=0 k= 0 The optimal knapsack should contain {1, 2} Finding the Items (6)
  • 47. 47 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k] ≠ V[i−1,k] then mark the nth item as in the knapsack i = i−1, k = k-wi else i = i−1 5 7 0 3 4 5 7 The optimal knapsack should contain {1, 2} 7 3 Finding the Items (7)
  • 48. 48 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.
  • 49. 49 for i = 1 to n for w = 1 to W V[i,w] = -1 for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 MFKnapsack(i, w) if V[i,w] < 0 if w < wi value = MFKnapsack(i-1, w) else value = max(MFKnapsack(i-1, w), bi + MFKnapsack(i-1, w-wi)) V[i,w] = value return V[i,w] 0-1 Knapsack Memory Function Algorithm
  • 50. 50  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. naïve algorithm: » 0-1 Knapsack problem: O(W*n) vs. O(2n ) Conclusion
  • 52. 52 Brute-Force Approach Design and Analysis of Algorithms - Chapter 8 52
  • 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 53
  翻译: