





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A dynamic programming solution for the 0-1 knapsack problem. The problem involves finding the maximum value of items that can be placed in a knapsack with a given weight capacity. The dynamic programming approach is used to identify the optimal substructure of the problem and find the solution recursively.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






1
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
2
» Most of slides for this lecture are based on slides created by Dr. David Luebke, University of Virginia. » Some slides are based on lecture notes created by Dr. Chuck Cusack, UNL. » I have modified them and added new slides.
CSCE 310J Data Structures & Algorithms
3
» A metatechnique, not an algorithm (like divide & conquer) » The word “programming” is historical and predates computer programming
4
5
Summarizing the Concept of Dynamic Programming
» Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems » Overlapping subproblems: few subproblems in total, many recurring instances of each » Solve bottom-up, building a table of solved subproblems that are used to solve larger ones
» “Table” could be 3-dimensional, triangular, a tree, etc.
6
7
We have already seen this version
8
9
wi bi
10
Problem, in other words, is to find
∈ ∈
≤ i T
i iT
max bi subject to w W
The problem is called a “ 0-1 ” problem, because each item must be entirely accepted or rejected. In the “ Fractional Knapsack Problem ,” we can take fractions of items.
11
Let’s first solve this problem with a straightforward algorithm
12
19
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(W)
O(W)
Repeat n times
O(n*W) Remember that the brute-force algorithm takes O(2n)
20
21
i\W
22
i\W
23
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
i\W
24
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
25
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
26
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
27
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
28
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
29
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
30
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
37
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
38
» I.e., the value in B[n,W]
39
if B [ i,k ] ≠ B [ i − 1,k ] then
i = i − 1 , k = k-wi
i = i − 1 // Assume the i th^ item is not in the knapsack
How to find actual Knapsack Items
40
i\W (^) i= k= 5 bi= wi= B [ i,k ] = 7 B [ i − 1,k ] =
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the i th^ item as in the knapsack i = i − 1 , k = k-wi else i = i − 1
41
i\W (^) i= k= 5 bi= wi= B [ i,k ] = 7 B [ i − 1,k ] =
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the i th^ item as in the knapsack i = i − 1 , k = k-wi else i = i −− −− 1
42
i\W (^) i= k= 5 bi= wi= B [ i,k ] = 7 B [ i − 1,k ] =
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the i th^ item as in the knapsack i = i − 1 , k = k-wi else i = i −−−− 1
43
i\W (^) i= k= 5 bi= wi= B [ i,k ] = 7 B [ i − 1,k ] = k − wi=
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the i th^ item as in the knapsack i = i − 1 , k = k-wi else i = i −− −− 1
44
i\W (^) i= k= 2 bi= wi= B [ i,k ] = 3 B [ i − 1,k ] = k − wi=
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the i th^ item as in the knapsack i = i − 1 , k = k-wi else i = i −−−− 1
45
i\W
3 3 3 3 0 3 4 4 7 0 3 4
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the n th^ item as in the knapsack i = i − 1 , k = k-wi else i = i − 1
i= k= 0
The optimal knapsack should contain {1, 2}
46
i\W
3 3 3 3 0 3 4 4 7 0 3 4
i=n, k=W while i,k > 0 if B [ i,k ] ≠ B [ i − 1,k ] then mark the n th^ item as in the knapsack i = i − 1 , k = k-wi else i = i − 1
The optimal knapsack should contain {1, 2}
47
Review: The Knapsack Problem And Optimal Substructure
» If we remove item j from the load, what do we know about the remaining load? » A: remainder must be the most valuable load weighing at most W - wj that thief could take, excluding item j
48
» Do you recall how? » Greedy strategy: take in order of dollars/pound
» Example: 3 items weighing 10, 20, and 30 pounds, knapsack can hold 50 pounds Suppose item 2 is worth $100. Assign values to the other items so that the greedy strategy will fail