Algorithms (112 01) Ch4
Algorithms (112 01) Ch4
of ECE, TKU
Fall, 2023
Chapter 4
Dynamic Programming
Optimization Problems
• Find an optimal solution (maybe not unique)
from many possible solutions
• Applications
– Electronic design automation (EDA) –
floorplan, placement, and routing…
– Control, operation, our daily lives…
– E.g. n = 4 Optimal
No cutting
•
n-1
pn-1 + r1 p1 + rn-1 n-2
• n different possibilities 1
Recursive Solution
• How to know what is the last cut i during
the optimization process?
– Try all possible values of last cuts and find the
optimal solution
• Recursive version of the equation for rn
Complexity Analysis
• T(n) = number of calls to CUT-ROD of size
n
// initialize r
Down 1
// memoization
Time Complexity
• Both (top-down and bottom-up) running
time Θ(n2)
– Doubly-nested loop structure for bottom-up
– Aggregate analysis for top-down (shall see later)
Inspect (1/2)
• Four steps of dynamic programming
1. Characterize the structure of an optimal
solution (optimal substructure)
How to solve a smaller problem?
What is the last choice in the optimal solution?
2. Recursively define the value of an optimal
solution
Search all possibilities
Inspect (2/2)
3. Compute the value of an optimal solution
(either bottom-up or top-down)
Memoize solutions to many overlapping
subproblems
4. Construct an optimal solution from step 3.
(b)
S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA
S2=GTCGTTCGGAATGCCGTTGCTCTGTAAA
LCS=GTCGT CGGAA GCCG GC C G AA
Z Z’ match
also an LCS
DP--Recursive Solution
• Let c[i, j] = LCS length of Xi and Yj
b[i, j] points to
optimal solution of subproblem
© 2023 by Jiann-Chyi Rau Algorithms 4-35
Dept. of ECE, TKU
Fall, 2023
//A.row is p
//B.column is r
//A.column is q
– P(n) = Ω(2n)
• The brute-force method of exhaustive search makes for
a poor strategy when determining how to optimally
parenthesize a matrix chain
• How can we do better?
An Optimal Parenthesization
• Suppose to optimally parenthesize
Ai Ai+1… Aj, we split the product between Ak
and Ak+1
Then the way we parenthesize the “prefix”
subchain Ai Ai+1… Ak within this optimal
parenthesization of Ai Ai+1… Aj must be an
optimal parenthesization of Ai Ai+1… Ak
Similar observation holds for “postfix”
subchain
© 2023 by Jiann-Chyi Rau Algorithms 4-45
Dept. of ECE, TKU
Fall, 2023
A Recursive Solution
• Let m[i, j] be the minimum number of scalar
multiplications needed to compute the
matrix Ai…j, where 1 ≤ i ≤ j ≤ n
//i + (l – 1) ≤ n
l=6
l=1
Conclusions (1/2)
• Elements of dynamic Programming
– Optimal substructure
• The solutions to the subproblems used within the
optimal solution must themselves be optimal
– Overlapping subproblems
• Recursively solve the same subproblems over and
over again
Divide-and-conquer generates new subproblems
(e.g. merge sort is not dynamic programming)
Conclusions (2/2)
• When to use dynamic programming
– Objects are linearly ordered, cannot rearranged
• E.g. Matrices in a chain, characters in a string
Suitable for
contiguous substrings of a n-character string
Not suitable for
n! permutation of an n-element set
2n subsets of an n-element set
© 2023 by Jiann-Chyi Rau Algorithms 4-54
Dept. of ECE, TKU
Fall, 2023
Notes
Notes