0% found this document useful (0 votes)
20 views

ch15 0 1 Knapsack

The document discusses the 0-1 knapsack problem and how it can be solved using dynamic programming. It describes the problem as selecting a subset of items with maximum value that fits in a knapsack of limited weight capacity. It presents a brute force solution with exponential time complexity and then defines the problem as having optimal substructure, allowing a recursive and memoized solution in polynomial time. Finally, it provides pseudocode for a bottom-up dynamic programming algorithm to solve the 0-1 knapsack problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

ch15 0 1 Knapsack

The document discusses the 0-1 knapsack problem and how it can be solved using dynamic programming. It describes the problem as selecting a subset of items with maximum value that fits in a knapsack of limited weight capacity. It presents a brute force solution with exponential time complexity and then defines the problem as having optimal substructure, allowing a recursive and memoized solution in polynomial time. Finally, it provides pseudocode for a bottom-up dynamic programming algorithm to solve the 0-1 knapsack problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

CSE 373 Design and Analysis of Algorithms

Mirza Mohammad Lutfe Elahi

Department of Electrical and Computer Engineering


North South University

Dynamic Programming - 0-1 Knapsack problem


Chapter 15

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 1 / 44
Dynamic Programming

Dynamic Programming

It is used when the solution can be recursively described in terms of


solutions to subproblems (optimal substructure).
Algorithm finds solutions to subproblems and stores them in memory
for later use.
More efficient than ”brute-force methods”, which solve the same
subproblems over and over again.

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 2 / 44
Dynamic Programming

Knapsack problem

A thief robbing a store finds some items.


He wants to take as valuable a load as possible in his knapsack.
Each item has some weight and some value.
Total weight he can carry is no more than some maximum limit.
So he must consider weights of items as well as their values.
Which items should he take?
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 3 / 44
Dynamic Programming

Knapsack problem

There are two versions of the problem:


0-1 knapsack problem - Items are indivisible; you either take an
item or not. Solved with dynamic programming.
Fractional knapsack problem - Items are divisible. Items can be
taken in any fraction. Solved with a greedy algorithm.

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 4 / 44
Dynamic Programming

0-1 Knapsack Problem

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 , bi and
W are integer values)
Problem: How to pack the knapsack to achieve maximum total value
of packed items?
Solution: Find
max i∈T bi subject to i∈T wi ≤ W
P P

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 5 / 44
Dynamic Programming

0-1 Knapsack Problem - Brute Force Approach

Let’s first solve this problem with a straightforward algorithm


weight[] = {10, 20, 30}
value[] = {60, 100, 120}
W = 50
Solutions:
weight = 10, value = 60
weight = 20, value = 100
weight = 30, value = 120
weight = (10 + 20), value = (60 + 100) = 160
weight = (10 + 30), value = (60 + 120) = 180
weight = (20 + 30), value = (100 + 120) = 220
weight = (10 + 20 + 30) > W
Maximum value = 220

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 6 / 44
Dynamic Programming

0-1 Knapsack Problem - Brute Force Approach

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 ).
Can we do better?
Yes, with an algorithm based on dynamic programming.
We need to carefully identify the subproblems

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 7 / 44
Dynamic Programming

Defining a Subproblem

First attempt might be to characterize a sub-problem as follows:


Let Sk be the optimal subset of elements from {I0 , I1 , ..., Ik }.
The optimal subset from the elements {I0 , I1 , ..., Ik+1 } may not
correspond to the optimal subset of elements from {I0 , I1 , ..., Ik } in
any regular pattern.
Basically, the solution to the optimization problem for Sk+1 might
NOT contain the optimal solution from problem Sk .

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 8 / 44
Dynamic Programming

Defining a Subproblem

Item Weight value


I0 3 10
I1 8 4
I2 9 9
I3 8 11

The maximum weight the knapsack can hold is 20.


The best set of items from {I0 , I1 , I2 } is {I0 , I1 , I2 }.
BUT the best set of items from {I0 , I1 , I2 , I3 } is {I0 , I2 , I3 }.
In this example, note that this optimal solution, {I0 , I2 , I3 }, does NOT
build upon the previous optimal solution, {I0 , I1 , I2 }.
(Instead it build’s upon the solution, {I0 , I2 }, which is really the
optimal subset of {I0 , I1 , I2 } with weight 12 or less.)

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 9 / 44
Dynamic Programming

0-1 Knapsack Problem - Optimal Substructure

To consider all subsets of items, there can be two cases for every item:
the item is included in the optimal subset.
the item is not included in the optimal subset.

Therefore, the maximum value that can be obtained from n items is the
maximum of following two values
Maximum value obtained by n − 1 items and W weight (excluding nth
item).
Value of nth item plus maximum value obtained by n − 1 items and
W minus weight of the nth item (including nth item).

If weight of the nth is greater than W , then the nth item cannot be
included and case 1 is the only possibility.

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 10 / 44
Dynamic Programming

0-1 Knapsack Problem - Recursive Formula

So now we must re-work the way we build upon previous sub-problems...


Let B(k, w ) represent the maximum total value of a subset Sk with
weight w .
Our goal is to find B(n, W ), where n is the total number of items
and W is the maximal weight the knapsack can carry.
(
B(k − 1, w ) if wk > w ,
B(k, w ) =
max {B(k − 1, w ), B(k − 1, w − wk ) + bk } otherwise.
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

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 11 / 44
Dynamic Programming

0-1 Knapsack Problem - Recursive Formula

(
B(k − 1, w ) if wk > w ,
B(k, w ) =
max {B(k − 1, w ), B(k − 1, w − wk ) + bk } otherwise.

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.

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 12 / 44
Dynamic Programming

0-1 Knapsack Algorithm - Recursive

KS-0-1(k, w )
1 if k = 0 or w = 0
2 return 0
3
4 if wk > w
5 return KS-0-1(k − 1, w )
6 else
7 return max(KS-0-1(k − 1, w ), KS-0-1(k − 1, w − wk ) + bk )

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 13 / 44
Dynamic Programming

0-1 Knapsack Problem - Recursive


The two parameters indicated in the following recursion tree are k and w.
The recursion tree is for following sample inputs.
weight[] = {1, 1, 1}, W = 2, value[] = {10, 20, 30}

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 14 / 44
Dynamic Programming

0-1 Knapsack Algorithm - Recursive Memoized

KS-0-1(k, w )
1 if B[k][w ]! = −1
2 return B[k][w ]
3
4 if wk > w
5 B[k][w ] = KS-0-1(k − 1, w )
6 else
7 B[k][w ] = max(KS-0-1(k − 1, w ), KS-0-1(k − 1, w − wk ) + bk )
8
9 return B[k][w ]

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 15 / 44
Dynamic Programming

0-1 Knapsack Algorithm - Bottom up

Knapsack-0-1(W , n, b)
1 let B[1..n, 1..W ] be new tables
2 for w = 0 to W
3 B[0, w ] = 0
4 for i = 1 to n
5 B[i, 0] = 0
6 for i = 1 to n
7 for w = 1 to W
8 if wi ≤ w // item i can be part of the solution
9 if bi + B[i − 1, w − wi ] > B[i − 1, w ]
10 B[i, w ] = bi + B[i − 1, w − wi ]
11 else B[i, w ] = B[i − 1, w ]
12 else B[i, w ] = B[i − 1, w ] // wi > w
13 return B

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 16 / 44
Dynamic Programming

Running Time

What is the running time of this algorithm?


O(W ) + O(n) + O(n ∗ W )
O(n ∗ W )
Remember that the brute-force algorithm takes O(2n )

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 17 / 44
Dynamic Programming

Example

Let’s run our algorithm on the following data:


n = 4 (no. of elements)
W = 5 (max weight)
Elements (weight, benefit): (2, 3), (3, 4), (4, 5), (5, 6)

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 18 / 44
Dynamic Programming

Example

for w = 0 to W
B[0, w ] = 0

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 19 / 44
Dynamic Programming

Example

for i = 1 to n
B[i, 0] = 0

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 20 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 21 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 22 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 23 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 24 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 25 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 26 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 27 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 28 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 29 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 30 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 31 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 32 / 44
Dynamic Programming

Example

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 ] ←−
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 33 / 44
Dynamic Programming

Example

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 ]
else B[i, w ] = B[i − 1, w ] // wi > w ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 34 / 44
Dynamic Programming

Example

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 ] ←−
else B[i, w ] = B[i − 1, w ] // wi > w
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 35 / 44
Dynamic Programming

Comments

This algorithm only finds the max possible value that can be carried
in the knapsack
I.e., the value in B[n, W ]
To know the items that make this maximum value, an addition to this
algorithm is necessary.

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 36 / 44
Dynamic Programming

How to Find Actual Knapsack Items

All of the information we need is in the table.


B[n, W ] is the maximal value of items that can be placed in the
Knapsack.
Let i = n and k = W

if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1
// Assume the i th item is not in the knapsack
// Could it be in the optimally packed knapsack?

Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 37 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 38 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1 ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 39 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1 ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 40 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1 ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 41 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1 ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 42 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1 ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 43 / 44
Dynamic Programming

Finding the Items

i = n, k = W
while i, k > 0
if B[i, k] 6= B[i − 1, k]
mark the i th item as the knapsack
i = i − l, k = k − wi
else i = i − 1 ←−
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 44 / 44

You might also like