SlideShare a Scribd company logo
Dynamic Programming …
Continued
0-1 Knapsack Problem
Last Class
 Longest Common Subsequence (LCS)
◦ Assigned problem – Find the LCS between
“HUMAN” and “CHIMPANZEE”
 Fill out the dynamic programming table
 Trace back what the LCS is
 Edit Distance
◦ Assigned problem – Find the edit distance
required to transform “KILT” into “KITTEN”
 Fill out the dynamic programming table
 Trace back the edit path
◦ Assigned problem – Outline the recursive calls
when using the recursive method for finding the
edit distance between “ON” and “OFF”.
Last Class
 Can I have volunteers to do the following on the
board?
1) Fill out the dynamic programming table for the LCS
between “HUMAN” and “CHIMPANZEE”
2) Trace back what the LCS string is between “HUMAN” and
“CHIMPANZEE”
3) Fill out the dynamic programming table to find the edit
distance required to transform “KILT” into “KITTEN”
4) Trace back the edit path that transformed “KILT” into
“KITTEN”
5) Outline the recursive calls when using the recursive
method for finding the edit distance between “ON” and
“OFF”.
Announcements
 Assignment #4 DNA Distance Problem
Knapsack 0-1 Problem
 The goal is to
maximize the value of
a knapsack that can
hold at most W units
(i.e. lbs or kg) worth of
goods from a list of
items I0, I1, … In-1.
◦ Each item has 2
attributes:
1) Value – let this be vi for
item Ii
2) Weight – let this be wi for
item Ii
Knapsack 0-1 Problem
 The difference
between this
problem and the
fractional knapsack
one is that you
CANNOT take a
fraction of an item.
◦ You can either take
it or not.
◦ Hence the name
Knapsack 0-1
problem.
Knapsack 0-1 Problem
 Brute Force
◦ The naïve way to solve this problem is to
cycle through all 2n subsets of the n items
and pick the subset with a legal weight
that maximizes the value of the knapsack.
◦ We can come up with a dynamic
programming algorithm that will USUALLY
do better than this brute force technique.
Knapsack 0-1 Problem
 As we did before we are going to solve the
problem in terms of sub-problems.
◦ So let’s try to do that…
 Our first attempt might be to characterize a
sub-problem as follows:
◦ Let Sk be the optimal subset of elements from {I0, I1, …, Ik}.
 What we find is that 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.
Knapsack 0-1 Problem
 Let’s illustrate that point with an example:
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
Knapsack 0-1 problem
 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.
 So our recursive formula for subproblems:
B[k, w] = B[k - 1,w], if wk > w
= max { B[k - 1,w], B[k - 1,w - wk] + vk}, otherwise
 In English, this 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
Knapsack 0-1 Problem –
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! 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
Knapsack 0-1 Algorithm
for w = 0 to W { // Initialize 1st row to 0’s
B[0,w] = 0
}
for i = 1 to n { // Initialize 1st column to 0’s
B[i,0] = 0
}
for i = 1 to n {
for w = 0 to W {
if wi <= w { //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
}
else B[i,w] = B[i-1,w] // wi > w
}
}
Knapsack 0-1 Problem
 Let’s run our algorithm on the following
data:
◦ n = 4 (# of elements)
◦ W = 5 (max weight)
◦ Elements (weight, value):
(2,3), (3,4), (4,5), (5,6)
Knapsack 0-1 Example
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
// Initialize the base cases
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 1
vi = 3
wi = 2
w = 1
w-wi = -1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 1
vi = 3
wi = 2
w = 2
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 1
vi = 3
wi = 2
w = 3
w-wi = 1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 1
vi = 3
wi = 2
w = 4
w-wi = 2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 1
vi = 3
wi = 2
w = 5
w-wi = 3
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 2
vi = 4
wi = 3
w = 1
w-wi = -2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 2
vi = 4
wi = 3
w = 2
w-wi = -1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 2
vi = 4
wi = 3
w = 3
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 2
vi = 4
wi = 3
w = 4
w-wi = 1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 2
vi = 4
wi = 3
w = 5
w-wi = 2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 3
vi = 5
wi = 4
w = 1..3
w-wi = -3..-1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 3
vi = 5
wi = 4
w = 4
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 3
vi = 5
wi = 4
w = 5
w-wi = 1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 4
vi = 6
wi = 5
w = 1..4
w-wi = -4..-1
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
i = 4
vi = 6
wi = 5
w = 5
w-wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
if wi <= w //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
Knapsack 0-1 Example
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
We’re DONE!!
The max possible value that can be carried in this knapsack is $7
Knapsack 0-1 Algorithm
 This algorithm only finds the max
possible value that can be carried in
the knapsack
◦ The value in B[n,W]
 To know the items that make this
maximum value, we need to trace
back through the table.
Knapsack 0-1 Algorithm
Finding the Items
 Let i = n and k = W
if B[i, k] ≠ B[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?
Knapsack 0-1
Algorithm
Finding the Items
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i = 4
k = 5
vi = 6
wi = 5
B[i,k] = 7
B[i-1,k] = 7
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsac
k:
Knapsack 0-1
Algorithm
Finding the Items
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i = 3
k = 5
vi = 5
wi = 4
B[i,k] = 7
B[i-1,k] = 7
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsac
k:
Knapsack 0-1
Algorithm
Finding the Items
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i = 2
k = 5
vi = 4
wi = 3
B[i,k] = 7
B[i-1,k] = 3
k – wi = 2
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsac
k:
Item 2
Knapsack 0-1
Algorithm
Finding the Items
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i = 1
k = 2
vi = 3
wi = 2
B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
i = n , k = W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Knapsac
k:
Item 1
Item 2
Knapsack 0-1
Algorithm
Finding the Items
Items:
1:
(2,3
)
2:
(3,4
)
3:
(4,5
)
4:
(5,6
)
i = 1
k = 2
vi = 3
wi = 2
B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
k = 0, so we’re DONE!
The optimal knapsack should contain:
Item 1 and Item 2
Knapsac
k:
Item 1
Item 2
Knapsack 0-1 Problem – Run
Time
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[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(n*W)
Remember that the brute-force algorithm takes: O(2n)
O(W)
O(W)
Repeat n times
O(n)
Knapsack Problem
1) Fill out the
dynamic
programming
table for the
knapsack
problem to the
right.
2) Trace back
through the
table to find the
items in the
References
 Slides adapted from Arup Guha’s Computer
Science II Lecture notes:
http://www.cs.ucf.edu/~dmarino/ucf/cop350
3/lectures/
 Additional material from the textbook:
Data Structures and Algorithm Analysis in Java
(Second Edition) by Mark Allen Weiss
 Additional images:
www.wikipedia.com
xkcd.com

More Related Content

Similar to DynProg_Knapsack.ppt (20)

PPT
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
PPTX
Knapsack problem dynamicprogramming
rowntu
 
PPTX
Dynamic programming (dp) in Algorithm
RaihanIslamSonet
 
PPT
AOA ppt.ppt
SaimaShaheen14
 
PPT
4 greedy methodnew
abhinav108
 
PPTX
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
PPTX
Fractional Knapsack Problem
harsh kothari
 
PPTX
Daa:Dynamic Programing
rupali_2bonde
 
PPT
Knapsack Algorithm www.geekssay.com
Hemant Gautam
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
PPTX
Knapsack
Karthik Chetla
 
PPT
Knapsack Problem Analysis of Algorithm.ppt
SharjeelFaisal4
 
DOCX
Exp.docx
vennapusaIndrasenare
 
PDF
Lop1
devendragiitk
 
PPT
Learn about dynamic programming and how to design algorith
MazenulIslamKhan
 
PPT
Dynamic Programming for 4th sem cse students
DeepakGowda357858
 
PPTX
Presentation of knapsack
Gaurav Dubey
 
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
Knapsack problem dynamicprogramming
rowntu
 
Dynamic programming (dp) in Algorithm
RaihanIslamSonet
 
AOA ppt.ppt
SaimaShaheen14
 
4 greedy methodnew
abhinav108
 
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
Fractional Knapsack Problem
harsh kothari
 
Daa:Dynamic Programing
rupali_2bonde
 
Knapsack Algorithm www.geekssay.com
Hemant Gautam
 
Knapsack problem using dynamic programming
khush_boo31
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Knapsack
Karthik Chetla
 
Knapsack Problem Analysis of Algorithm.ppt
SharjeelFaisal4
 
Learn about dynamic programming and how to design algorith
MazenulIslamKhan
 
Dynamic Programming for 4th sem cse students
DeepakGowda357858
 
Presentation of knapsack
Gaurav Dubey
 

More from Ruchika Sinha (20)

PPT
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
PPTX
Difference Between Normal & Smart/Automated Home
Ruchika Sinha
 
PPT
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
PPT
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
PPT
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
PPT
Dijkstra.ppt
Ruchika Sinha
 
PPT
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
PPT
Clipping
Ruchika Sinha
 
PPT
Lec22 intel
Ruchika Sinha
 
PPTX
Pc assembly
Ruchika Sinha
 
PPT
Casing
Ruchika Sinha
 
PPTX
Installation of motherboard
Ruchika Sinha
 
PPT
Shortest path
Ruchika Sinha
 
PPT
Bellman ford algorithm
Ruchika Sinha
 
PPTX
Python material
Ruchika Sinha
 
PPT
Python3
Ruchika Sinha
 
PPT
Optimization problems
Ruchika Sinha
 
PPT
Regular Grammar
Ruchika Sinha
 
PPTX
Software Teting
Ruchika Sinha
 
PPTX
Backtrack search-algorithm
Ruchika Sinha
 
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
Difference Between Normal & Smart/Automated Home
Ruchika Sinha
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
Dijkstra.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Clipping
Ruchika Sinha
 
Lec22 intel
Ruchika Sinha
 
Pc assembly
Ruchika Sinha
 
Installation of motherboard
Ruchika Sinha
 
Shortest path
Ruchika Sinha
 
Bellman ford algorithm
Ruchika Sinha
 
Python material
Ruchika Sinha
 
Python3
Ruchika Sinha
 
Optimization problems
Ruchika Sinha
 
Regular Grammar
Ruchika Sinha
 
Software Teting
Ruchika Sinha
 
Backtrack search-algorithm
Ruchika Sinha
 

Recently uploaded (20)

PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
MRRS Strength and Durability of Concrete
CivilMythili
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Big Data and Data Science hype .pptx
SUNEEL37
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 

DynProg_Knapsack.ppt

  • 2. Last Class  Longest Common Subsequence (LCS) ◦ Assigned problem – Find the LCS between “HUMAN” and “CHIMPANZEE”  Fill out the dynamic programming table  Trace back what the LCS is  Edit Distance ◦ Assigned problem – Find the edit distance required to transform “KILT” into “KITTEN”  Fill out the dynamic programming table  Trace back the edit path ◦ Assigned problem – Outline the recursive calls when using the recursive method for finding the edit distance between “ON” and “OFF”.
  • 3. Last Class  Can I have volunteers to do the following on the board? 1) Fill out the dynamic programming table for the LCS between “HUMAN” and “CHIMPANZEE” 2) Trace back what the LCS string is between “HUMAN” and “CHIMPANZEE” 3) Fill out the dynamic programming table to find the edit distance required to transform “KILT” into “KITTEN” 4) Trace back the edit path that transformed “KILT” into “KITTEN” 5) Outline the recursive calls when using the recursive method for finding the edit distance between “ON” and “OFF”.
  • 4. Announcements  Assignment #4 DNA Distance Problem
  • 5. Knapsack 0-1 Problem  The goal is to maximize the value of a knapsack that can hold at most W units (i.e. lbs or kg) worth of goods from a list of items I0, I1, … In-1. ◦ Each item has 2 attributes: 1) Value – let this be vi for item Ii 2) Weight – let this be wi for item Ii
  • 6. Knapsack 0-1 Problem  The difference between this problem and the fractional knapsack one is that you CANNOT take a fraction of an item. ◦ You can either take it or not. ◦ Hence the name Knapsack 0-1 problem.
  • 7. Knapsack 0-1 Problem  Brute Force ◦ The naïve way to solve this problem is to cycle through all 2n subsets of the n items and pick the subset with a legal weight that maximizes the value of the knapsack. ◦ We can come up with a dynamic programming algorithm that will USUALLY do better than this brute force technique.
  • 8. Knapsack 0-1 Problem  As we did before we are going to solve the problem in terms of sub-problems. ◦ So let’s try to do that…  Our first attempt might be to characterize a sub-problem as follows: ◦ Let Sk be the optimal subset of elements from {I0, I1, …, Ik}.  What we find is that 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.
  • 9. Knapsack 0-1 Problem  Let’s illustrate that point with an example: 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
  • 10. Knapsack 0-1 problem  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.  So our recursive formula for subproblems: B[k, w] = B[k - 1,w], if wk > w = max { B[k - 1,w], B[k - 1,w - wk] + vk}, otherwise  In English, this 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
  • 11. Knapsack 0-1 Problem – 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! 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
  • 12. Knapsack 0-1 Algorithm for w = 0 to W { // Initialize 1st row to 0’s B[0,w] = 0 } for i = 1 to n { // Initialize 1st column to 0’s B[i,0] = 0 } for i = 1 to n { for w = 0 to W { if wi <= w { //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] } else B[i,w] = B[i-1,w] // wi > w } }
  • 13. Knapsack 0-1 Problem  Let’s run our algorithm on the following data: ◦ n = 4 (# of elements) ◦ W = 5 (max weight) ◦ Elements (weight, value): (2,3), (3,4), (4,5), (5,6)
  • 14. Knapsack 0-1 Example i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 2 0 3 0 4 0 // Initialize the base cases for w = 0 to W B[0,w] = 0 for i = 1 to n B[i,0] = 0
  • 15. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 1 vi = 3 wi = 2 w = 1 w-wi = -1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 2 0 3 0 4 0
  • 16. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 1 vi = 3 wi = 2 w = 2 w-wi = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 17. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 1 vi = 3 wi = 2 w = 3 w-wi = 1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 2 0 3 0 4 0
  • 18. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 1 vi = 3 wi = 2 w = 4 w-wi = 2 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 2 0 3 0 4 0
  • 19. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 1 vi = 3 wi = 2 w = 5 w-wi = 3 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 3 0 4 0
  • 20. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 2 vi = 4 wi = 3 w = 1 w-wi = -2 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 21. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 2 vi = 4 wi = 3 w = 2 w-wi = -1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 3 0 4 0
  • 22. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 2 vi = 4 wi = 3 w = 3 w-wi = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 23. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 2 vi = 4 wi = 3 w = 4 w-wi = 1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 3 0 4 0
  • 24. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 2 vi = 4 wi = 3 w = 5 w-wi = 2 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 4 0
  • 25. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 3 vi = 5 wi = 4 w = 1..3 w-wi = -3..-1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 26. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 3 vi = 5 wi = 4 w = 4 w-wi = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 27. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 3 vi = 5 wi = 4 w = 5 w-wi = 1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 28. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 4 vi = 6 wi = 5 w = 1..4 w-wi = -4..-1 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 29. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w i = 4 vi = 6 wi = 5 w = 5 w-wi = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 if wi <= w //item i can be in the solution if vi + B[i-1,w-wi] > B[i-1,w] B[i,w] = vi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
  • 30. Knapsack 0-1 Example Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 We’re DONE!! The max possible value that can be carried in this knapsack is $7
  • 31. Knapsack 0-1 Algorithm  This algorithm only finds the max possible value that can be carried in the knapsack ◦ The value in B[n,W]  To know the items that make this maximum value, we need to trace back through the table.
  • 32. Knapsack 0-1 Algorithm Finding the Items  Let i = n and k = W if B[i, k] ≠ B[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?
  • 33. Knapsack 0-1 Algorithm Finding the Items Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i = 4 k = 5 vi = 6 wi = 5 B[i,k] = 7 B[i-1,k] = 7 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 i = n , k = W while i, k > 0 if B[i, k] ≠ B[i-1, k] then mark the ith item as in the knapsack i = i-1, k = k-wi else i = i-1 Knapsac k:
  • 34. Knapsack 0-1 Algorithm Finding the Items Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i = 3 k = 5 vi = 5 wi = 4 B[i,k] = 7 B[i-1,k] = 7 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 i = n , k = W while i, k > 0 if B[i, k] ≠ B[i-1, k] then mark the ith item as in the knapsack i = i-1, k = k-wi else i = i-1 Knapsac k:
  • 35. Knapsack 0-1 Algorithm Finding the Items Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i = 2 k = 5 vi = 4 wi = 3 B[i,k] = 7 B[i-1,k] = 3 k – wi = 2 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 i = n , k = W while i, k > 0 if B[i, k] ≠ B[i-1, k] then mark the ith item as in the knapsack i = i-1, k = k-wi else i = i-1 Knapsac k: Item 2
  • 36. Knapsack 0-1 Algorithm Finding the Items Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i = 1 k = 2 vi = 3 wi = 2 B[i,k] = 3 B[i-1,k] = 0 k – wi = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 i = n , k = W while i, k > 0 if B[i, k] ≠ B[i-1, k] then mark the ith item as in the knapsack i = i-1, k = k-wi else i = i-1 Knapsac k: Item 1 Item 2
  • 37. Knapsack 0-1 Algorithm Finding the Items Items: 1: (2,3 ) 2: (3,4 ) 3: (4,5 ) 4: (5,6 ) i = 1 k = 2 vi = 3 wi = 2 B[i,k] = 3 B[i-1,k] = 0 k – wi = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 k = 0, so we’re DONE! The optimal knapsack should contain: Item 1 and Item 2 Knapsac k: Item 1 Item 2
  • 38. Knapsack 0-1 Problem – Run Time for w = 0 to W B[0,w] = 0 for i = 1 to n B[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(n*W) Remember that the brute-force algorithm takes: O(2n) O(W) O(W) Repeat n times O(n)
  • 39. Knapsack Problem 1) Fill out the dynamic programming table for the knapsack problem to the right. 2) Trace back through the table to find the items in the
  • 40. References  Slides adapted from Arup Guha’s Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop350 3/lectures/  Additional material from the textbook: Data Structures and Algorithm Analysis in Java (Second Edition) by Mark Allen Weiss  Additional images: www.wikipedia.com xkcd.com