SlideShare a Scribd company logo
Dynamic Programming …
Continued
0-1 Knapsack Problem
Dr. AMIT KUMAR @JUET
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
Dr. AMIT KUMAR @JUET
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.
Dr. AMIT KUMAR @JUET
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.
Dr. AMIT KUMAR @JUET
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. Dr. AMIT KUMAR @JUET
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
optimal subset of {I0, I1, I2} with weight 12 or less.)
Dr. AMIT KUMAR @JUET
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 kDr. AMIT KUMAR @JUET
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
choose the case with greater value.Dr. AMIT KUMAR @JUET
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
}
}
Dr. AMIT KUMAR @JUET
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)
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
// Initialize the base cases
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
i / w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
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
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
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
Dr. AMIT KUMAR @JUET
Knapsack 0-1 Example
We’re DONE!!
The max possible value that can be carried in this knapsack is $7
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
Dr. AMIT KUMAR @JUET
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.
Dr. AMIT KUMAR @JUET
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?
Dr. AMIT KUMAR @JUET
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
Knapsack:
Dr. AMIT KUMAR @JUET
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
Knapsack:
Dr. AMIT KUMAR @JUET
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
Knapsack:
Item 2
Dr. AMIT KUMAR @JUET
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
Knapsack:
Item 1
Item 2
Dr. AMIT KUMAR @JUET
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
Knapsack:
Item 1
Item 2
Dr. AMIT KUMAR @JUET
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)
Dr. AMIT KUMAR @JUET
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 Dr. AMIT KUMAR @JUET
References
• Slides adapted from Arup Guha’s Computer
Science II Lecture notes:
http://www.cs.ucf.edu/~dmarino/ucf/cop3503/
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
Dr. AMIT KUMAR @JUET

More Related Content

Similar to Dynamic Programming knapsack 0 1

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
lecture 25
lecture 25lecture 25
lecture 25sajinsc
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
Vikas Sharma
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
AyushJaiswal513854
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
Barani Tharan
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
 

Similar to Dynamic Programming knapsack 0 1 (6)

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
lecture 25
lecture 25lecture 25
lecture 25
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 

More from Amit Kumar Rathi

Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
Amit Kumar Rathi
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Amit Kumar Rathi
 
Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)
Amit Kumar Rathi
 
Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)
Amit Kumar Rathi
 
Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
Amit Kumar Rathi
 
Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)
Amit Kumar Rathi
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
Amit Kumar Rathi
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
Amit Kumar Rathi
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Sccd and topological sorting
Sccd and topological sortingSccd and topological sorting
Sccd and topological sorting
Amit Kumar Rathi
 
Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
Amit Kumar Rathi
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
Amit Kumar Rathi
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Amit Kumar Rathi
 
Merge sort analysis
Merge sort analysisMerge sort analysis
Merge sort analysis
Amit Kumar Rathi
 
Loop invarient
Loop invarientLoop invarient
Loop invarient
Amit Kumar Rathi
 
Linear sort
Linear sortLinear sort
Linear sort
Amit Kumar Rathi
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 

More from Amit Kumar Rathi (20)

Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
 
Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)Fundamentals of Genetic Algorithms (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
 
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
 
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
 
Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)Associative Memory using NN (Soft Computing)
Associative Memory using NN (Soft Computing)
 
Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)Back Propagation Network (Soft Computing)
Back Propagation Network (Soft Computing)
 
Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)Fundamentals of Neural Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
 
Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)Introduction to Soft Computing (intro to the building blocks of SC)
Introduction to Soft Computing (intro to the building blocks of SC)
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Sccd and topological sorting
Sccd and topological sortingSccd and topological sorting
Sccd and topological sorting
 
Red black trees
Red black treesRed black trees
Red black trees
 
Recurrence and master theorem
Recurrence and master theoremRecurrence and master theorem
Recurrence and master theorem
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Merge sort analysis
Merge sort analysisMerge sort analysis
Merge sort analysis
 
Loop invarient
Loop invarientLoop invarient
Loop invarient
 
Linear sort
Linear sortLinear sort
Linear sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 

Recently uploaded

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 

Recently uploaded (20)

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 

Dynamic Programming knapsack 0 1

  • 1. Dynamic Programming … Continued 0-1 Knapsack Problem Dr. AMIT KUMAR @JUET
  • 2. 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 Dr. AMIT KUMAR @JUET
  • 3. 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. Dr. AMIT KUMAR @JUET
  • 4. 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. Dr. AMIT KUMAR @JUET
  • 5. 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. Dr. AMIT KUMAR @JUET
  • 6. 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 optimal subset of {I0, I1, I2} with weight 12 or less.) Dr. AMIT KUMAR @JUET
  • 7. 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 kDr. AMIT KUMAR @JUET
  • 8. 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 choose the case with greater value.Dr. AMIT KUMAR @JUET
  • 9. 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 } } Dr. AMIT KUMAR @JUET
  • 10. 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) Dr. AMIT KUMAR @JUET
  • 11. Knapsack 0-1 Example // Initialize the base cases for w = 0 to W B[0,w] = 0 for i = 1 to n B[i,0] = 0 i / w 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 2 0 3 0 4 0 Dr. AMIT KUMAR @JUET
  • 12. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 13. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 14. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 15. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 16. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 17. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 18. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 19. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 20. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 21. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 22. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 23. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 24. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 25. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 26. Knapsack 0-1 Example 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 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 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 Dr. AMIT KUMAR @JUET
  • 27. Knapsack 0-1 Example We’re DONE!! The max possible value that can be carried in this knapsack is $7 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 Dr. AMIT KUMAR @JUET
  • 28. 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. Dr. AMIT KUMAR @JUET
  • 29. 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? Dr. AMIT KUMAR @JUET
  • 30. 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 Knapsack: Dr. AMIT KUMAR @JUET
  • 31. 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 Knapsack: Dr. AMIT KUMAR @JUET
  • 32. 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 Knapsack: Item 2 Dr. AMIT KUMAR @JUET
  • 33. 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 Knapsack: Item 1 Item 2 Dr. AMIT KUMAR @JUET
  • 34. 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 Knapsack: Item 1 Item 2 Dr. AMIT KUMAR @JUET
  • 35. 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) Dr. AMIT KUMAR @JUET
  • 36. 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 Dr. AMIT KUMAR @JUET
  • 37. References • Slides adapted from Arup Guha’s Computer Science II Lecture notes: http://www.cs.ucf.edu/~dmarino/ucf/cop3503/ 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 Dr. AMIT KUMAR @JUET