Lec 9 DynamicProgramming-1
Lec 9 DynamicProgramming-1
Lecture# 09
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
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
Subprob1 Subprob2
Subproblem
Subprob3 sharing
Subprob4
Subprob7
Subprob5
9
Subprob6
Bottom-up
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)
max bi subject to wi W
iT iT
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 Go through all combinations and
6. {1,2} 7 50 find the one with maximum value
7. {1,3} 12 70 and with total weight
knapsack ≤W W
capacity
= 16
8. {1,4} 7 30
9. {2,3} 15 80 14
10. {2,4} 10 40
11. {3,4} 15 60 Running time = O(2n)
12. {1,2,3} 17 not feasible
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
w1 =2 w2 =4 w3 =5 w4 =3
Weight Benefit
b1 =3 b2 =5 b3 =8 b4 =4
Item wi bi
#
?
Max weight: W = 20
1 2 3
S4 2 4 5
For S4:
S5
Total weight: 14 3 5 8
Maximum benefit: 20 4 3 4
5 9 10
w1 =2 w2 =4 w3 =5 w5 =9
b1 =3 b2 =5 b3 =8 b5 =10
Solution for S4 is
For S5: 17
Total weight: 20 not part of the
Maximum benefit: 26 solution for S5!!!
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!
n = 4 (# of elements)
W = 5 (max weight)
21
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
22
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
23
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 24
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 25
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 26
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 27
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 28
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 29
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 30
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 31
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 32
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 33
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 34
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 35
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 36
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 37
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 38
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]
39
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 48
4 5 10
Exercise
49
Assembly-Line Scheduling
Problem
50
Assembly-Line Scheduling Problem
• After going through the jth station on a line i, the auto goes on to the
(j+1)st station on either line.There is no transfer cost if it stays on the
same line
• An optimal solution to the entire problem depends on optimal
solutions to sub-problems
• We formulate a solution for the assembly line problem stage-by-stage 51
Notations: Assembly-Line Scheduling Problem
e1 x1
2 3 1 3 4
In 2
Out
2 1 2 1
e2 x2
e1 x1
2 3 1 3 4
In 2
Out
2 1 2 1
e2 x2
2 S1 j-1 3
In Out
4 2
8 5 6 Si,j 5 7
ai,j
fi(j)
t1,j-1
l2(j) =1
55
Mathematical Model: Finding Objective Function
2 3
In Out
4 2
f1[1] = e1 + a1,1;
f2[1] = e2 + a2,1.
f1[j] = min (f1[j-1] + a1,j, f2[j-1] + t2,j-1 + a1,j) for j ≥ 2;
f2[j] = min (f2[j-1] + a2,j, f1[j-1] + t1,j-1 + a2,j) for j ≥ 2;
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
• f1[1] = e1 + a1,1 = 2 + 7 = 9
• f2[1] = e2 + a2,1 = 4 + 8 = 12
f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)
f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=2
f1[2] = min (f1[1] + a1, 2, f2[1] + t2, 1 + a1, 2)
58
= min (9 + 9, 12 + 2 + 9) = min (18, 23) = 18, l1[2] = 1
Computation of f2[2]
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
• f1[1] = e1 + a1,1 = 2 + 7 = 9
• f2[1] = e2 + a2,1 = 4 + 8 = 12
f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)
f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=2
f2[2] = min (f2[1] + a2, 2, f1[1] + t1, 1 + a2, 2)
59
= min (12 + 5, 9 + 2 + 5) = min (17, 16) = 16, l2[2] = 1
Computation of f1[3]
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
j 1 2 3 4 5 6 j 2 3 4 5 6
fi(j) li(j)
1 9 18 20 24 32 35 1 1 2 1 1 2
2 12 16 22 25 30 37 2 1 2 1 2 2
69
f* = 38 l* = 1
Fastest Way: Assembly-Line Scheduling
2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
*
Optimal Solution: Constructing The Fastest Way
1. Print-Stations (l, n)
2. i l*
3. print “line” i “, station” n
4. for j n downto 2
5. do i li[j]
6. print “line” i “, station” j - 1