dynamic II
dynamic II
CSE 301
Lecture 2
Dynamic Programming
Dynamic Programming
2
DP - Two key ingredients
• Two key ingredients for an optimization problem
to be suitable for a dynamic-programming
solution:
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
Scalar multiplication in line 5 dominates time to compute C
Number of scalar multiplications = pqr
Matrix-chain Multiplication …contd
9
2. A Recursive Solution
• Length = 1: i = j, i = 1, 2, …, n ond
ec
irst
1 2 3 sn f
• Length = 2: j = i + 1, i = 1, 2, …, n-1
n
m[1, n] gives the optimal
solution to the problem
j
Compute rows from bottom to top 3
and from left to right 2
In a similar matrix s we keep the
1
optimal values of k
i 11
Multiply 4 Matrices: A×B×C×D (1)
• Compute the costs in the bottom-up manner
– First we consider AB, BC, CD
– No need to consider AC or BD
ABCD
ABC BCD
AB BC CD
A B C D
12
Multiply 4 Matrices: A×B×C×D (2)
• Compute the costs in the bottom-up manner
– Then we consider A(BC), (AB)C, B(CD), (BC)D
– No need to consider (AB)D, A(CD)
ABCD
ABC BCD
AB BC CD
A B C D
13
Multiply 4 Matrices: A×B×C×D (3)
• Compute the costs in the bottom-up manner
• Select minimum cost matrix calculations of ABC & BCD
ABCD
ABC BCD
min min
A(BC) (AB)C B(CD) (BC)D
AB BC CD
A B C D
14
Multiply 4 Matrices: A×B×C×D (4)
• Compute the costs in the bottom-up manner
– We now consider A(BCD), (AB)(CD), (ABC)D
ABCD
ABC BCD
AB BC CD
A B C D
15
Multiply 4 Matrices: A×B×C×D (5)
• Compute the costs in the bottom-up manner
– Finally choose the cheapest cost plan for matrix calculations
ABCD
min
A(BCD) (AB)(CD) (ABC)D
ABC BCD
AB BC CD
A B C D
16
Example: Step 1
1 2 3 4 5 1 2 3 4 5
1 1
2 2
3 3
4 4
5 5
c(i,j), i ≤ j kay(i,j), i ≤ j 17
Example: Step 2
– s = 0, [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
– c(i,i) = 0
1 2 3 4 5 1 2 3 4 5
1 0 1
2 0 2
3 0 3
4 0 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 18
Example: Step 3
– s = 0, [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
– c(i,i+1) = riri+1ri+2
– kay(i,i+1) = i
1 2 3 4 5 1 2 3 4 5
1 0 50 1 1
2 0 50 2 2
3 0 20 3 3
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 19
Example: Step 4
– s = 1, [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
– c(i,i+2) = min{c(i,i) + c(i+1,i+2) + riri+1ri+3,
c(i,i+1) + c(i+2,i+2) + riri+2ri+3}
1 2 3 4 5 1 2 3 4 5
1 0 50 1 1
2 0 50 2 2
3 0 20 3 3
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 20
Example: Step 5
– s = 1, [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
– c(2,4) = min{c(2,2) + c(3,4) + r2r3r5, c(2,3) + c(4,4) +
r2 r4 r5 }
– c(3,5) = …
1 2 3 4 5 1 2 3 4 5
1 0 50 150 1 1 2
2 0 50 30 2 2 2
3 0 20 40 3 3 3
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 21
Example: Step 6
– s = 2, [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
– c(i,i+3) = min{c(i,i) + c(i+1,i+3) + riri+1ri+4,
c(i,i+1) + c(i+2,i+3) + riri+2ri+4,
c(i,i+2) + c(i+3,i+3) + riri+3ri+4}
1 2 3 4 5 1 2 3 4 5
1 0 50 150 90 1 1 2 2
2 0 50 30 90 2 2 2 2
3 0 20 40 3 3 3
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 22
Example: Step 7
– s = 3, [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
– c(i,i+4) = min{c(i,i) + c(i+1,i+4) + riri+1ri+5,
c(i,i+1) + c(i+2,i+4) + riri+2ri+5, c(i,i+2) + c(i+3,i+4) +
riri+3ri+5, c(i,i+3) + c(i+4,i+4) + riri+4ri+5}
1 2 3 4 5 1 2 3 4 5
1 0 50 150 90 190 1 1 2 2 2
2 0 50 30 90 2 2 2 2
3 0 20 40 3 3 3
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 23
Example: Step 8
1 0 50 150 90 190 1 1 2 2 2
2 0 50 30 90 2 2 2 2
3 0 20 40 3 3 3
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 24
Example: Step 9
– M15 = M12×M35
– kay(1,2) = 1 ▶ M12 = M11×M22
→ M15 = (M11×M22)×M35
1 2 3 4 5 1 2 3 4 5
1 0 50 150 90 190 1 1 2 2 2
2 0 50 30 90 2 2 2 2
3 0 20 40 3 3 4
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 25
Example: Step 10
– M15 = (M11×M22)×M35
– Kay(3,5) = 4 ▶ M35 = M34×M55
→ M15 = (M11×M22)×(M34×M55)
1 2 3 4 5 1 2 3 4 5
1 0 50 150 90 190 1 1 2 2 2
2 0 50 30 90 2 2 2 2
3 0 20 40 3 3 4
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 26
Example: Step 11
– M15 = (M11×M22)×(M34×M55)
– Kay(3,4) = 3 ▶ M34 = M33×M44
→ M15 = (M11×M22)×((M33×M44)×M55)
1 2 3 4 5 1 2 3 4 5
1 0 50 150 90 190 1 1 2 2 2
2 0 50 30 90 2 2 2 2
3 0 20 40 3 3 4
4 0 200 4 4
5 0 5
c(i,j), i ≤ j kay(i,j), i ≤ j 27
Memoization
• Top-down approach with the efficiency of typical dynamic
programming approach
• Maintaining an entry in a table for the solution to each
subproblem
– memoize the inefficient recursive algorithm
Alg.: MEMOIZED-MATRIX-CHAIN(p)
1. n length[p] – 1
2. for i 1 to n
Initialize the m table with
large values that indicate
3. do for j i to n whether the values of m[i, j]
have been computed
4. do m[i, j]
29
Memoized Matrix-Chain
Alg.: LOOKUP-CHAIN(p, i, j) Running time is O(n3)
1. if m[i, j] <
2. then return m[i, j]
3. if i = j
4. then m[i, j] 0
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj}
5. else for k i to j – 1 ik<j
6. do q LOOKUP-CHAIN(p, i, k) +
LOOKUP-CHAIN(p, k+1, j) + pi-1pkpj
7. if q < m[i, j]
8. then m[i, j] q
9. return m[i, j]
30
Dynamic Progamming vs. Memoization