DynamicProgramming
DynamicProgramming
Lecture# 15
Dynamic Programming
1
Optimization Problems
• Some problems can have many possible/ feasible solutions
with each solution having a specific cost. We wish to find the
best solution with the optimal cost.
• Maximization problem finds a solution with maximum cost
• Minimization problem finds a solution with minimum cost
2
Optimization Problems
• Two common techniques:
• Greedy Algorithms (local)
• Make the greedy choice and THEN
• Solve sub-problem arising after the choice is made
• The choice we make may depend on previous choices, but
not on solutions to sub-problems
• Top down solution, problems decrease in size
• Dynamic Programming (global)
• We make a choice at each step
• The choice depends on solutions to sub-problems
3
• Bottom up solution, smaller to larger sub-problems
Dynamic Programming
• Dynamic Programming was introduced by Richard Bellman in 1955.
4
Dynamic Programming
• How to know if an optimization problem can be solved by
applying dynamic programming?
• Two key ingredients
• Optimal substructure
• Overlapping sub-problems
6
Dynamic Programming
• Dynamic programming, like divide and conquer method, solves
problems by combining the solutions to sub-problems.
7
Divide-and-Conquer
Top-down
Prob
Subprob Subprob
…
…
Prob
Subprob Subprob
1 2
Subproblem
Subprob sharing
Subprob 3
4 Subprob
7
Subprob
5 Subprob 9
Bottom-up 6
Knapsack Problem
10
Knapsack Problem
Given n items, each having a specific value vi and weight wi,
and a knapsack of fixed capacity W, pack the knapsack with
given items to maximize total value without exceeding the
total capacity W of the knapsack.
11
Knapsack Problem
There are two versions of the problem:
1. “0-1 knapsack problem”
• Items are indivisible; you either take an item or not. Some
special instances can be solved with dynamic programming
12
0-1 Knapsack Problem
• Given a knapsack with maximum capacity W, and a set S consisting
of n items
• Each item i has some weight wi and benefit value bi (all wi and W
are integer values)
13
0-1 Knapsack Problem: Brute Force
Approach
Subset Total weight Total value
1. ∅ 0 0 # W V
2. {1} 2 20 1 2 20
3. {2} 5 30 2 5 30
4. {3} 10 50 3 10 50
5. {4} 5 10 4 5 10
6. {1,2} 7 50
7. {1,3} 12 70 knapsack capacity W = 16
8. {1,4} 7 30
9. {2,3} 15 80 Go through all combinations and
10. {2,4} 10 40 find the one with maximum value
11. {3,4} 15 60 and with total weight ≤ W
12. {1,2,3} 17 not feasible
13. {1,2,4} 12 60
14. {1,3,4} 17 not feasible 14
15. {2,3,4} 20 not feasible
16. {1,2,3,4} 22 not feasible Running time = O(2n)
0-1 Knapsack Problem: Brute Force
Approach
Knapsack-BF (n, V, W, C)
Compute all subsets, s, of S = {1, 2, 3, 4}
forall s ∈ S
weight = Compute sum of weights of these items
if weight > C, not feasible
new solution = Compute sum of values of these items
solution = solution ∪ {new solution}
Return maximum of solution
Approach: In brute force algorithm, we go through all combinations and find the
one with maximum value and with total weight less or equal to W = 16
Complexity
• Cost of computing subsets O(2n) for n elements
• Cost of computing weight = O(2n) 15
• Cost of computing values = O(2n)
• Total cost in worst case: O(2n)
0-1 Knapsack problem:
Dynamic Programming Approach
We can do better with an algorithm based on dynamic programming.
We need to carefully identify/define the sub-problems
16
Defining a Sub-problem
As we have seen, the solution for S4 is not part of the solution for S5 so
our definition of a sub-problem is flawed and we need another one!
17
Recursive Formula for
sub-problems
Recursive formula for sub-problems:
n = 4 (# of elements)
W = 5 (max weight)
20
Example (2)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4
for w = 0 to W
V[0,w] = 0
21
Example (3)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
for i = 1 to n
V[i,0] = 0
22
Items:
1: (2,3)
Example (4) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 wi=2
2 0 w=1
3 0 w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
23
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (5) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 wi=2
2 0 w=2
3 0 w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
24
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (6) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 wi=2
2 0 w=3
3 0 w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
25
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (7) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3 wi=2
2 0 w=4
3 0 w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
26
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (8) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3 3 wi=2
2 0 w=5
3 0 w-wi =3
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
27
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (9) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 w=1
3 0 w-wi =-2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
28
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (10) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 w=2
3 0 w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
29
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (11) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 4 w=3
3 0 w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
30
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (12) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 4 4 w=4
3 0 w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
31
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (13) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 4 4 7 w=5
3 0 w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
32
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (14) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3 wi=4
2 0 0 3 4 4 7 w= 1..3
3 0 0 3 4
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
33
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (15) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3 wi=4
2 0 0 3 4 4 7 w= 4
3 0 0 3 4 5 w- wi=0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
34
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (16) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3 wi=4
2 0 0 3 4 4 7 w= 5
3 0 0 3 4 5 7 w- wi=1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
35
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (17) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=4
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3 wi=5
2 0 0 3 4 4 7 w= 1..4
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
36
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (18) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=4
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3 wi=5
2 0 0 3 4 4 7 w= 5
3 0 0 3 4 5 7 w- wi=0
4 0 0 3 4 5 7
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
37
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Comments
This algorithm only finds the max possible value that can be
carried in the knapsack
• i.e., the value in V[n,W]
38
How to find actual Knapsack Items
All of the information we need is in the table.
Example:
item weight value knapsack capacity W = 16
1 2 20
2 5 30
3 10 50
4 5 10 47
Exercise
48