ch15 0 1 Knapsack
ch15 0 1 Knapsack
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 1 / 44
Dynamic Programming
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 2 / 44
Dynamic Programming
Knapsack problem
Knapsack problem
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 4 / 44
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 5 / 44
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 6 / 44
Dynamic Programming
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 7 / 44
Dynamic Programming
Defining a Subproblem
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 8 / 44
Dynamic Programming
Defining a Subproblem
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 9 / 44
Dynamic Programming
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
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 11 / 44
Dynamic Programming
(
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
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
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 14 / 44
Dynamic Programming
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
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
Mirza Mohammad Lutfe Elahi (ECE@NSU) CSE 373 Design and Analysis of Algorithms 17 / 44
Dynamic Programming
Example
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
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example
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
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
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
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
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
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
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
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
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