Professional Documents
Culture Documents
Dynamic programming
0-1 Knapsack problem
10/28/23 1
0-1 Knapsack problem
2 3
This is a knapsack 3 4
Max weight: W = 20 4 5
5 8
W = 20
9 10
10/28/23 3
0-1 Knapsack problem
Problem, in other words, is to find
max bi subject to wi W
iT iT
The problem is called a “0-1” problem,
because each item must be entirely
accepted or rejected.
Just another version of this problem is the
“Fractional Knapsack Problem”, where we
can take fractions of items.
10/28/23 4
0-1 Knapsack problem: brute-
force approach
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 the most total value and with
total weight less or equal to W
Running time will be O(2n)
10/28/23 5
0-1 Knapsack problem: brute-
force approach
Can we do better?
Yes, 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}
10/28/23 6
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}
This is a valid subproblem definition.
For S5:
Solution for S4 is
Total weight: 20 not part of the
10/28/23 total benefit: 26 solution for S5!!! 8
Defining a Subproblem
(continued)
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!
Let’s add another parameter: w, which will
represent the exact weight for each subset of
items
The subproblem then will be to compute
B[k,w] 9
Recursive Formula for
subproblems
Recursive formula for subproblems:
B[k 1, w] if wk w
B[k , w]
max{B[k 1, w], B[k 1, w wk ] bk } else
It means, that the best subset of Sk that has
total weight w is one of the two:
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
10/28/23 10
Recursive Formula
B[k 1, w] if wk w
B[k , w]
max{B[k 1, w], B[k 1, w wk ] bk } else
The best subset of Sk that has the total weight
w, either contains item k or not.
First case: w >w. Item k can’t be part of the
k
solution, since if it was, the total weight
would be > w, which is unacceptable
Second case: w <=w. Then the item k can
k
be in the solution, and we choose the case
with greater value
10/28/23 11
0-1 Knapsack Algorithm
for w = 0 to W
B[0,w] = 0
for i = 0 to n
B[i,0] = 0
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23 else B[i,w] = B[i-1,w] // wi > w 12
Running time
for w = 0 to W O(W)
B[0,w] = 0
for i = 0 to n Repeat n times
B[i,0] = 0
for w = 0 to W O(W)
< the rest of the code >
What is the running time of this
algorithm?
O(n*W)
Remember that the brute-force algorithm
10/28/23
takes O(2 n
) 13
Example
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)
10/28/23 14
Example (2)
i 0 1 2 3 4
W
0 0
1 0
2 0
3 0
4 0
5 0
for w = 0 to W
B[0,w] = 0
10/28/23 15
Example (3)
i 0 1 2 3 4
W
0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0
for i = 0 to n
B[i,0] = 0
10/28/23 16
Items:
Example (4) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 bi=3
3 0 wi=2
4 0 w=1
5 0 w-wi =-1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 17
Items:
Example (5) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 wi=2
4 0 w=2
5 0 w-wi =0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 18
Items:
Example (6) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 3 wi=2
4 0 w=3
5 0 w-wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 19
Items:
Example (7) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 3 wi=2
4 0 3 w=4
5 0 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 20
Items:
Example (8) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 3 wi=2
4 0 3 w=5
5 0 3 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 21
Items:
Example (9) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 bi=4
3 0 3 wi=3
4 0 3 w=1
5 0 3 w-wi=-2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 22
Items:
Example (10) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 wi=3
4 0 3 w=2
5 0 3 w-wi=-1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 23
Items:
Example (11) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 4 wi=3
4 0 3 w=3
5 0 3 w-wi=0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 24
Items:
Example (12) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 4 wi=3
4 0 3 4 w=4
5 0 3 w-wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 25
Items:
Example (13) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 4 wi=3
4 0 3 4 w=5
5 0 3 7 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 26
Items:
Example (14) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 0 i=3 4: (5,6)
2 0 3 3 3 bi=5
3 0 3 4 4 wi=4
4 0 3 4 w=1..3
5 0 3 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 27
Items:
Example (15) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 0 i=3 4: (5,6)
2 0 3 3 3 bi=5
3 0 3 4 4 wi=4
4 0 3 4 5 w=4
5 0 3 7 w- wi=0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 28
Items:
Example (15) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 0 i=3 4: (5,6)
2 0 3 3 3 bi=5
3 0 3 4 4 wi=4
4 0 3 4 5 w=5
5 0 3 7 7 w- wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 29
Items:
Example (16) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 0 0 i=3 4: (5,6)
2 0 3 3 3 3 bi=5
3 0 3 4 4 4 wi=4
4 0 3 4 5 5 w=1..4
5 0 3 7 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 30
Items:
Example (17) 1: (2,3)
i 0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 0 0 i=3 4: (5,6)
2 0 3 3 3 3 bi=5
3 0 3 4 4 4 wi=4
4 0 3 4 5 5 w=5
5 0 3 7 7 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
10/28/23
else B[i,w] = B[i-1,w] // wi > w 31
Comments
This algorithm only finds the max possible
value that can be carried in the knapsack
To know the items that make this maximum
value, an addition to this algorithm is
necessary
Please see LCS algorithm from the previous
lecture for the example how to extract this
data from the table we built
10/28/23 32
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
Running time (Dynamic Programming
algorithm vs. naïve algorithm):
– LCS: O(m*n) vs. O(n * 2m)
– 0-1 Knapsack problem: O(W*n) vs. O(2n)
10/28/23 33