Dynamic Programming
Dynamic Programming
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
Dynamic Programming
Another strategy for designing algorithms is dynamic programming
A metatechnique, not an algorithm (like divide & conquer) The word programming is historical and predates computer programming
Dynamic programming
It is used when the solution can be recursively described in terms of solutions to subproblems (optimal substructure). Algorithm finds solutions to subproblems and stores them in memory for later use. More efficient than brute-force methods, which solve the same subproblems over and over again.
Variations:
Table could be 3-dimensional, triangular, a tree, etc.
Weight 1 3 5
Value 8 6 5
6
Knapsack problem
There are two versions of the problem:
1. 2. 0-1 knapsack problem and Fractional knapsack problem
1. Items are indivisible; you either take an item or not. Solved with dynamic programming 2. Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm.
We have already seen this version
bi
3 4 5 8
max
iT
bi subject to
iT
wi 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.
9 10
10
Defining a Subproblem
If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k} This is a reasonable subproblem definition. The question is: can we describe the final solution (Sn ) in terms of subproblems (Sk)? Unfortunately, we cant do that.
Defining a Subproblem
w1 =2 w2 =4 b1 =3 b2 =5 w3 =5 b3 =8 w4 =3 b4 =4
Weight Benefit
Item
# S4 S5
wi
2 3 4 5 9
bi
3 4 5 8 10
1 2 3 4 5
w5 =9 b5 =10
13
14
15
16
Recursive Formula
B[ k , w ] =
The best subset of Sk that has the total weight w, either contains item k or not. First case: wk>w. Item k cant 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.
Running time
for w = 0 to W O(W) B[0,w] = 0 for i = 1 to n B[i,0] = 0 Repeat n for i = 1 to n for w = 0 to W O(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)
n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6)
19 20
Example (2)
0 1 2 3 4 i\W 0 0 1 0 2 0 3 0 4 0 5 0
Example (3)
0 1 2 3 4 i\W 0 0 0 0 0 0 1 0 2 0 3 0 4 0 5 0
for w = 0 to W B[0,w] = 0
for i = 1 to n B[i,0] = 0
21
22
Example (4)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0
2 0
3 0
4 0
5 0
Example (5)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
23
1 0 0
2 0 3
3 0
4 0
5 0
24
Example (6)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0
2 0 3
3 0 3
4 0
5 0
Example (7)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0
2 0 3
3 0 3
4 0 3
5 0
26
Example (8)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0
2 0 3
3 0 3
4 0 3
5 0 3
Example (9)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0 0
2 0 3
3 0 3
4 0 3
5 0 3
28
Example (10)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0 0
2 0 3 3
3 0 3
4 0 3
5 0 3
Example (11)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0 0
2 0 3 3
3 0 3 4
4 0 3
5 0 3
30
Example (12)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0 0
2 0 3 3
3 0 3 4
4 0 3 4
5 0 3
Example (13)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
31
1 0 0 0
2 0 3 3
3 0 3 4
4 0 3 4
5 0 3 7
32
Example (14)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0 0 0
2 0 3 3 3
3 0 3 4 4
4 0 3 4
5 0 3 7
Example (15)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
33
1 0 0 0 0
2 0 3 3 3
3 0 3 4 4
4 0 3 4 5
5 0 3 7
34
Example (16)
0 1 2 3 4 i\W 0 0 0 0 0 0
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
1 0 0 0 0
2 0 3 3 3
3 0 3 4 4
4 0 3 4 5
5 0 3 7 7
Example (17)
0 1 2 3 4 i\W 0 0 0 0 0 0 1 0 0 0 0 0 2 0 3 3 3 3 3 0 3 4 4 4 4 0 3 4 5 5 5 0 3 7 7
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
35 36
Example (18)
0 1 2 3 4 i\W 0 0 0 0 0 0 1 0 0 0 0 0 2 0 3 3 3 3 3 0 3 4 4 4 4 0 3 4 5 5 5 0 3 7 7 7
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.
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 38
i = i1
44
3 4
i = i1
46
The optimal solution to the 0-1 problem cannot be found with the same greedy strategy
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
47
48
Memoization
Memoization is another way to deal with overlapping subproblems in dynamic programming
After computing the solution to a subproblem, store it in a table Subsequent calls just do a table lookup
Conclusion
Dynamic programming is a useful technique of solving certain kind of problems When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and re-use them as necessary (memoization) Running time of dynamic programming algorithm vs. nave algorithm:
0-1 Knapsack problem: O(W*n) vs. O(2n)
51