Unit 4: Dynamic Programming - National Taiwan...
Transcript of Unit 4: Dynamic Programming - National Taiwan...
Y.-W. ChangUnit 4 1
Unit 4: Dynamic Programming․Course contents:
Assembly-line scheduling Matrix-chain multiplication Longest common subsequence Optimal binary search trees Applications: Cell flipping, rod cutting, optimal polygon
triangulation, flip-chip routing, technology mapping for logic synthesis
․Reading: Chapter 15
Y.-W. ChangUnit 4 2
Dynamic Programming (DP) vs. Divide-and-Conquer․Both solve problems by combining the solutions to subproblems.․Divide-and-conquer algorithms
Partition a problem into independent subproblems, solve the subproblems recursively, and then combine their solutions to solve the original problem.
Inefficient if they solve the same subproblem more than once.․Dynamic programming (DP)
Applicable when the subproblems are not independent. DP solves each subproblem just once.
Y.-W. ChangUnit 4 3
Assembly-line Scheduling
․An auto chassis enters each assembly line, has parts added at stations, and a finished auto exits at the end of the line. Si,j: the jth station on line i ai,j: the assembly time required at station Si,j ti,j: transfer time from station Si,j to the j+1 station of the other line. ei (xi): time to enter (exit) line i
station S2,1 S2,2
a1,1 a1,2 a1,3 a1,n-1 a1,n
x1
x2
t1,1
t2,1
a2,1 a2,2 a2,3 a2,n-1 a2,n
e2
t1,2
t2,2
t1,n-1
t2,n-1
e1
autoexits
chassisenters
line 1
line 2
station S1,1 S1,2 S1,3 S1,n-1 S1,n
S2,3 S2,n-1 S2,n
Y.-W. ChangUnit 4 4
Optimal Substructure
․Objective: Determine the stations to choose to minimize the total manufacturing time for one auto. Brute force: (2n), why? The problem is linearly ordered and cannot be rearranged =>
Dynamic programming? ․Optimal substructure: If the fastest way through station Si,j is
through S1,j-1, then the chassis must have taken a fastest way from the starting point through S1,j-1.
S1,1 S1,2 S1,3 S1,n-1 S1,n
S2,1 S2,2
a1,1 a1,2 a1,3 a1,n-1 a1,n
x1
x2
t1,1
t2,1
a2,1 a2,2 a2,3 a2,n-1 a2,n
e2
t1,2
t2,2
t1,n-1
t2,n-1
e1
autoexits
chassisenters
line 1
line 2S2,3 S2,n-1 S2,n
Y.-W. ChangUnit 4 5
Overlapping Subproblem: Recurrence
․Overlapping subproblem: The fastest way through station S1,j is either through S1,j-1 and then S1,j , or through S2,j-1 and then transfer to line 1 and through S1,j.
․ fi[j]: fastest time from the starting point through Si,j
․The fastest time all the way through the factoryf* = min(f1[n] + x1, f2[n] + x2)
S1,1 S1,2 S1,3 S1,n-1 S1,n
S2,1 S2,2
a1,1 a1,2 a1,3 a1,n-1 a1,n
x1
x2
t1,1
t2,1
a2,1 a2,2 a2,3 a2,n-1 a2,n
e2
t1,2
t2,2
t1,n-1
t2,n-1
e1
autoexits
chassisenters
line 1
line 2S2,3 S2,n-1 S2,n
1 1, 11
1 1, 2 2, 1 1,
1[ ]
min( [ 1] , [ 1] ) 2j j j
e a if jf j
f j a f j t a if j
Y.-W. ChangUnit 4 6
Computing the Fastest Time
․ li[j]: The line number whose station j-1 is used in a fastest way through Si,j
Fastest-Way(a, t, e, x, n)1. f1[1] = e1 + a1,12. f2[1] = e2 + a2,13. for j = 2 to n4. if f1[j-1] + a1,j f2[j-1] + t2,j -1 + a1,j5. f1[j] = f1[j-1] + a1,j6. l1[j] = 17. else f1[j] = f2[j-1] + t2,j -1 + a1,j8. l1[j] = 29. if f2[j-1] + a2,j f1[j-1] + t1,j -1 + a2,j10. f2[j] = f2[j-1] + a2,j11. l2[j] = 212. else f2[j] = f1[j-1] + t1,j -1 + a2,j13. l2[j] = 114. if f1[n] + x1 f2[n] + x215. f* = f1[n] + x116. l* = 117. else f* = f2[n] + x218. l* = 2
Running time?Linear time!!
Y.-W. ChangUnit 4 7
Constructing the Fastest WayPrint-Station(l, n)1. i = l*2. Print “line” i “, station “ n3. for j = n downto 24. i = li[j] 5. Print “line “ i “, station “ j-1
S1,1 S1,2 S1,3 S1,5 S1,6
S2,1 S2,2
7 9 3 8 4
3
2
2
2
8 5 6 5 7
4
3
1
4
1
2autoexits
chassisenters
line 1
line 2S2,3 S2,5 S2,6
S1,4
4
4
3
2
S2,4
1
2
line 1, station 6line 2, station 5line 2, station 4line 1, station 3line 2, station 2line 1, station 1
Y.-W. ChangUnit 4 8
An ExampleS1,1 S1,2 S1,3 S1,5 S1,6
S2,1 S2,2
7 9 3 8 4
3
2
2
2
8 5 6 5 7
4
3
1
4
1
2autoexits
chassisenters
line 1
line 2S2,3 S2,5 S2,6
S1,4
4
4
3
2
S2,4
1
2
9 18 20 24 32 35
12 16 22 25 30 37
1 2 1 1 2
1 2 1 2 2
1 2 3 4 5 6j
f* = 38
l* = 1
f1[j]
f2[j]
l1[j]
l2[j]
Y.-W. ChangUnit 4 9
․Typically apply to optimization problem.․Generic approach
Calculate the solutions to all subproblems. Proceed computation from the small subproblems to
larger subproblems. Compute a subproblem based on previously computed
results for smaller subproblems. Store the solution to a subproblem in a table and never
recompute.․Development of a DP
1. Characterize the structure of an optimal solution.2. Recursively define the value of an optimal solution.3. Compute the value of an optimal solution bottom-up.4. Construct an optimal solution from computed information
(omitted if only the optimal value is required).
Dynamic Programming (DP)
Y.-W. ChangUnit 4 10
When to Use Dynamic Programming (DP)․DP computes recurrence efficiently by storing partial results
efficient only when the number of partial results is small.․Hopeless configurations: n! permutations of an n-element set,
2n subsets of an n-element set, etc.․Promising configurations: contiguous
substrings of an n-character string, n(n+1)/2 possible subtreesof a binary search tree, etc.
․DP works best on objects that are linearly ordered and cannot be rearranged!! Linear assembly lines, matrices in a chain, characters in a
string, points around the boundary of a polygon, points on a line/circle, the left-to-right order of leaves in a search tree, fixed-order cell flipping, etc.
Objects are ordered left-to-right Smell DP?
1( 1) / 2n
ii n n
Y.-W. ChangUnit 4 11
DP Example: Matrix-Chain Multiplication․If A is a p x q matrix and B a q x r matrix, then C = AB is
a p x r matrix
time complexity: O(pqr).1
[ , ] [ , ] [ , ]q
k
C i j A i k B k j
Matrix-Multiply(A, B)1. if A.columns ≠ B.rows2. error “incompatible dimensions”3. else let C be a new A.rows * B.columns matrix4. for i = 1 to A.rows5. for j = 1 to B.columns6. cij = 07. for k = 1 to A.columns8. cij = cij + aikbkj9. return C
Y.-W. ChangUnit 4 12
DP Example: Matrix-Chain Multiplication․The matrix-chain multiplication problem
Input: Given a chain <A1, A2, …, An> of n matrices, matrix Ai has dimension pi-1 x pi
Objective: Parenthesize the product A1 A2 … An to minimize the number of scalar multiplications
․Exp: dimensions: A1: 4 x 2; A2: 2 x 5; A3: 5 x 1(A1A2)A3: total multiplications = 4 x 2 x 5 + 4 x 5 x 1 = 60A1(A2A3): total multiplications = 2 x 5 x 1 + 4 x 2 x 1 = 18
․So the order of multiplications can make a big difference!
Y.-W. ChangUnit 4 13
Matrix-Chain Multiplication: Brute Force
․A = A1 A2 … An: How to evaluate A using the minimum number of multiplications?
․Brute force: check all possible orders? P(n): number of ways to multiply n matrices.
, exponential in n.․Any efficient solution?
The matrix chain is linearly ordered and cannot be rearranged!!
Smell Dynamic programming?
Y.-W. ChangUnit 4 14
Matrix-Chain Multiplication․m[i, j]: minimum number of multiplications to compute
matrix Ai..j = Ai Ai +1 … Aj, 1 i j n. m[1, n]: the cheapest cost to compute A1..n.
․Applicability of dynamic programming Optimal substructure: an optimal solution contains
within its optimal solutions to subproblems. Overlapping subproblem: a recursive algorithm
revisits the same problem over and over again; only (n2) subproblems.
matrix Ai has dimension pi-1 x pi
Y.-W. ChangUnit 4 15
Bottom-Up DP Matrix-Chain OrderMatrix-Chain-Order(p)1. n = p.length – 12. Let m[1..n, 1..n] and s[1..n-1, 2..n] be new tables3. for i = 1 to n4. m[i, i] = 05. for l = 2 to n // l is the chain length6. for i = 1 to n – l +17. j = i + l -18. m[i, j] = 9. for k = i to j -110. q = m[i, k] + m[k+1, j] + pi-1pkpj11. if q < m[i, j]12. m[i, j] = q13. s[i, j] = k14. return m and s
s
Ai dimension pi-1 x pi
Y.-W. ChangUnit 4 16
Constructing an Optimal Solution․ s[i, j]: value of k such that the optimal parenthesization of
Ai Ai+1 … Aj splits between Ak and Ak+1
․Optimal matrix A1..n multiplication: A1..s[1, n]As[1, n] + 1..n
․Exp: call Print-Optimal-Parens(s, 1, 6): ((A1 (A2 A3))((A4 A5) A6))Print-Optimal-Parens(s, i, j)1. if i == j2. print “A”i3. else print “(“4. Print-Optimal-Parens(s, i, s[i, j])5. Print-Optimal-Parens(s, s[i, j] + 1, j)6. print “)“
s
0
Y.-W. ChangUnit 4 17
Top-Down, Recursive Matrix-Chain Order․Time complexity: (2n) (T(n) > (T(k)+T(n-k)+1)).1
1
n
k
Recursive-Matrix-Chain(p, i, j)1. if i == j2. return 03. m[i, j] =4. for k = i to j -15 q = Recursive-Matrix-Chain(p,i, k)
+ Recursive-Matrix-Chain(p, k+1, j) + pi-1pkpj6. if q < m[i, j]7. m[i, j] = q8. return m[i, j]
Y.-W. ChangUnit 4 18
Top-Down DP Matrix-Chain Order (Memoization)․Complexity: O(n2) space for m[] matrix and O(n3) time to fill in
O(n2) entries (each takes O(n) time)Memoized-Matrix-Chain(p)1. n = p.length - 12. let m[1..n, 1..n] be a new table2. for i = 1 to n3. for j = i to n4. m[i, j] =5. return Lookup-Chain(m, p,1,n)
Lookup-Chain(m, p, i, j)1. if m[i, j] < 2. return m[i, j]3. if i == j4. m[ i, j] = 05. else for k = i to j -16. q = Lookup-Chain(m, p, i, k) + Lookup-Chain(m, p, k+1, j) + pi-1pkpj7. if q < m[i, j]8. m[i, j] = q9. return m[i, j]
Y.-W. ChangUnit 4 19
Two Approaches to DP1. Bottom-up iterative approach
Start with recursive divide-and-conquer algorithm. Find the dependencies between the subproblems (whose
solutions are needed for computing a subproblem). Solve the subproblems in the correct order.
2. Top-down recursive approach (memoization) Start with recursive divide-and-conquer algorithm. Keep top-down approach of original algorithms. Save solutions to subproblems in a table (possibly a lot of
storage). Recurse only on a subproblem if the solution is not already
available in the table.․ If all subproblems must be solved at least once, bottom-up DP is
better due to less overhead for recursion and table maintenance.․ If many subproblems need not be solved, top-down DP is better
since it computes only those required.
Y.-W. ChangUnit 4 20
Longest Common Subsequence․Problem: Given X = <x1, x2, …, xm> and
Y = <y1, y2, …, yn>, find the longest common subsequence (LCS) of X and Y.
․Exp: X = <a, b, c, b, d, a, b> and Y = <b, d, c, a, b, a>LCS = <b, c, b, a> (also, LCS = <b, d, a, b>).
․Exp: DNA sequencing: S1 = ACCGGTCGAGATGCAG;
S2 = GTCGTTCGGAATGCAT; LCS S3 = CGTCGGATGCA
․Brute-force method: Enumerate all subsequences of X and check if they
appear in Y. Each subsequence of X corresponds to a subset of
the indices {1, 2, …, m} of the elements of X. There are 2m subsequences of X. Why?
Y.-W. ChangUnit 4 21
Optimal Substructure for LCS․Let X = <x1, x2, …, xm> and Y = <y1, y2, …, yn> be
sequences, and Z = <z1, z2, …, zk> be LCS of X and Y.1. If xm = yn, then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-1.2. If xm ≠ yn, then zk ≠ xm implies Z is an LCS of Xm-1 and Y. 3. If xm ≠ yn, then zk ≠ yn implies Z is an LCS of X and Yn-1.
․c[i, j]: length of the LCS of Xi and Yj․c[m, n]: length of LCS of X and Y․Basis: c[0, j] = 0 and c[i, 0] = 0
Y.-W. ChangUnit 4 22
Top-Down DP for LCS․ c[i, j]: length of the LCS of Xi and Yj, where Xi = <x1, x2, …, xi>
and Yj=<y1, y2, …, yj>.․ c[m, n]: LCS of X and Y.․Basis: c[0, j] = 0 and c[i, 0] = 0.
․The top-down dynamic programming: initialize c[i, 0] = c[0, j] = 0, c[i, j] = NIL
TD-LCS(i, j)1. if c[i,j] == NIL2. if xi == yj3. c[i, j] = TD-LCS(i-1, j-1) + 14. else c[i, j] = max(TD-LCS(i, j-1), TD-LCS(i-1, j))5. return c[i, j]
Y.-W. ChangUnit 4 23
Bottom-Up DP for LCS
․Find the right order to solve the subproblems
․To compute c[i, j], we need c[i-1, j-1], c[i-1, j], and c[i, j-1]
․b[i, j]: points to the table entry w.r.t. the optimal subproblem solution chosen when computing c[i, j]
LCS-Length(X,Y)1. m = X.length2. n = Y.length3. let b[1..m, 1..n] and c[0..m, 0..n]
be new tables4. for i = 1 to m5. c[i, 0] = 06. for j = 0 to n7. c[0, j] = 08. for i = 1 to m9. for j = 1 to n10. if xi == yj11. c[i, j] = c[i-1, j-1]+112. b[i, j] = “↖”13. elseif c[i-1,j] c[i, j-1]14. c[i,j] = c[i-1, j]15. b[i, j] = `` ''16. else c[i, j] = c[i, j-1]17. b[i, j] = “ “18. return c and b
Y.-W. ChangUnit 4 24
Example of LCS․LCS time and space complexity: O(mn).․X = <A, B, C, B, D, A, B> and Y = <B, D, C, A, B, A>
LCS = <B, C, B, A>.
Y.-W. ChangUnit 4 25
Constructing an LCS
․Trace back from b[m, n] to b[1, 1], following the arrows: O(m+n) time.
Print-LCS(b, X, i, j)1. if i == 0 or j == 02. return3. if b[i, j] == “↖ “4. Print-LCS(b, X, i-1, j-1)5. print xi6. elseif b[i, j] == “ “7. Print-LCS(b, X, i -1, j)8. else Print-LCS(b, X, i, j-1)
Y.-W. ChangUnit 4 26
Optimal Binary Search Tree․Given a sequence K = <k1, k2, …, kn> of n distinct keys in
sorted order (k1 < k2 < … < kn) and a set of probabilities P = <p1, p2, …, pn> for searching the keys in K and Q = <q0, q1, q2, …, qn> for unsuccessful searches (corresponding to D = <d0, d1, d2, …, dn> of n+1 distinct dummy keys with direpresenting all values between ki and ki+1), construct a binary search tree whose expected search cost is smallest.
k2
k1
k3
k4
k5d0 d1
d2 d3 d4 d5
k2
k1
k4
k5
k3
d0 d1
d2 d3
d4
d5
p2
q2
p1 p4
p3 p5
q0 q1
q3 q4 q5
Y.-W. ChangUnit 4 27
An Example
i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20qi 0.05 0.10 0.05 0.05 0.05 0.10 1 0
1n n
i ii i
p q
1 0[search cost in ] = ( ( ) 1) ( ( ) 1)
n n
T i i T i ii i
E T depth k p depth d q
1 0
1 ( ) ( )n n
T i i T i i
i i
depth k p depth d q
Cost = 2.75Optimal!!
k2
k1
k4
k5
k3
d0 d1
d2 d3
d4
d5
Cost = 2.80
k2
k1
k3
k4
k5d0 d1
d2 d3 d4 d5
0.10×1 (depth 0)0.15×2
0.10×2
0.10×4
0.20×3
Y.-W. ChangUnit 4 28
Optimal Substructure․If an optimal binary search tree T has a subtree T’
containing keys ki, …, kj, then this subtree T’ must be optimal as well for the subproblem with keys ki, …, kjand dummy keys di-1, …, dj. Given keys ki, …, kj with kr (i r j) as the root, the left subtree
contains the keys ki, …, kr-1 (and dummy keys di-1, …, dr-1) and the right subtree contains the keys kr+1, …, kj (and dummy keys dr, …, dj).
For the subtree with keys ki, …, kj with root ki, the left subtreecontains keys ki, .., ki-1 (no key) with the dummy key di-1.
k2
k1
k4
k5
k3
d0 d1
d2 d3
d4
d5
Y.-W. ChangUnit 4 29
Overlapping Subproblem: Recurrence․e[i, j] : expected cost of searching an optimal binary
search tree containing the keys ki, …, kj. Want to find e[1, n]. e[i, i -1] = qi-1 (only the dummy key di-1).
․If kr (i r j) is the root of an optimal subtree containing keys ki, …, kj and let thene[i, j] = pr + (e[i, r-1] + w(i, r-1)) + (e[r+1, j] +w(r+1, j))
= e[i, r-1] + e[r+1, j] + w(i, j)․Recurrence:
1( , ) j j
l ll i l i
w i j p q
1 1[ , ] min{ [ , 1] [ 1, ] ( , )}
i
i r j
q if j ie i j e i r e r j w i j if i j
k3
k4
k5
d2 d3 d4 d5
0.10×1
0.10×3
0.20×2
Node depths increase by 1 after merging two subtrees, and so do the costs
Y.-W. ChangUnit 4 30
Computing the Optimal Cost․ Need a table e[1..n+1, 0..n] for e[i, j] (why e[1, 0] and e[n+1, n]?)․ Apply the recurrence to compute w(i, j) (why?)
Optimal-BST(p, q, n)1. let e[1..n+1, 0..n], w[1..n+1, 0..n],
and root[1..n, 1..n] be new tables 2. for i = 1 to n + 13. e[i, i-1] = qi-14. w[i, i-1] = qi-15. for l = 1 to n6. for i = 1 to n – l + 17. j = i + l -18. e[i, j] = 9. w[i, j] = w[i, j-1] + pj + qj10. for r = i to j11. t = e[i, r-1] + e[r+1, j] + w[i, j]12. if t < e[i, j]13. e[i, j] = t14. root[i, j] = r15. return e and root
1 1[ , ]
[ , 1] i
j j
q if j iw i j
w i j p q if i j
․root[i, j] : index r for which kr is the root of an optimal search tree containing keys ki, …, kj.
2.752.00
0.50
1.30
0.05
0.90
0.10
1.751.20
0.05
0.600.30
0.900.40
1.250.70
0.250.05
0.450.100.05
0
34
5 12
34
56
21
e
j i
k3
k4
k5
d2 d3 d4 d5
Y.-W. ChangUnit 4 31
Example
2.752.00
0.50
1.30
0.05
0.90
0.10
1.751.20
0.05
0.600.30
0.900.40
1.250.70
0.250.05
0.450.100.05
0
34
5 12
34
56
21
e
j i
1.000.80
0.35
0.60
0.05
0.50
0.10
0.700.50
0.05
0.300.20
0.450.25
0.550.35
0.150.05
0.300.100.05
0
34
5 12
34
56
21
w
j i
24
5
55
22
44
12
22
31
34
5 12
34
52
1
root
j i
i 0 1 2 3 4 5pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10
k2
k1
k4
k5
k3
d0 d1
d2 d3
d4
d5
e[1, 1] = e[1, 0] + e[2, 1] + w(1,1)= 0.05 + 0.10 + 0.3= 0.45
e[1, 5] = e[1, 1] + e[3, 5] + w(1,5)= 0.45 + 1.30 + 1.00 = 2.75
(r = 2; r=1, 3?)
Y.-W. Chang
Appendix A: Cell Compaction with Flipping․Different cell boundaries need different minimum spacing
Optimize cell orientations to get smaller chip area
․Consider the cells in a single row with a fixed cell order Exhibit optimal substructure Dynamic programming (DP)
Flip c2
Smaller spacing
, 1
min∈ , , , 1
∗ min ,
T: cost function: #cells in row j
: minimum spacing: nodes of cell flipping
graph∗: node of optimal solution, : the two orientations∈ ,
Y.-W. Chang
Example of DP-Based Cell Flipping
4 5
3 21
22
3
1∗
63
4
10 4 10 10
Cell flipping graph
Flip c2 and c4
450
Unflipped
Flipped390
i 1 i 2 i 3 i 4
0 12 16 32
0 11 19 29
0 0
10 0 3 , 0 2 12
10 0 1 , 0 2 11
4 12 2 , 11 1 16
4 12 3 , 11 4 19
10 16 6 , 19 4 32
10 16 3 , 19 5 29∗ 10 32,29 39∗ 39
, 1
min∈ , , , 1
∗ min ,
linear-time dynamic programming
Y.-W. ChangUnit 4 34
Appendix B: Rod Cutting․Cut steel rods into pieces to maximize the revenue
Assumptions: Each cut is free; rod lengths are integral numbers.․Input: A length n and table of prices pi, for i = 1, 2, …, n.․Output: The maximum revenue obtainable for rods whose
lengths sum to n, computed as the sum of the prices for the individual rods.
length i 1 2 3 4 5 6 7 8 9 10price pi 1 5 8 9 10 17 17 20 24 30
length i = 49 85581
1 1 1 1 1 1
1
1 5 1 51 1 5max revenuer4 = 5+5 =10
5 5
Objects are linearly ordered (and cannot be rearranged)??
Y.-W. ChangUnit 4 35
Optimal Rod Cutting
․ If pn is large enough, an optimal solution might require no cuts, i.e., just leave the rod as n unit long.
․Solution for the maximum revenue ri of length i
r1 = 1 from solution 1 = 1 (no cuts)r2 = 5 from solution 2 = 2 (no cuts)r3 = 8 from solution 3 = 3 (no cuts)r4 = 10 from solution 4 = 2 + 2r5 = 13 from solution 5 = 2 + 3r6 = 17 from solution 6 = 6 (no cuts)r7 = 18 from solution 7 = 1 + 6 or 7 = 2 + 2 + 3r8 = 22 from solution 8 = 2 + 6
length i 1 2 3 4 5 6 7 8 9 10price pi 1 5 8 9 10 17 17 20 24 30max revenue ri 1 5 8 10 13 17 18 22 25 30
)(max),...,,,max(1
112211 inini
nnnnn rprrrrrrpr
Y.-W. ChangUnit 4 36
Optimal Substructure
․Optimal substructure: To solve the original problem, solve subproblems on smaller sizes. The optimal solution to the original problem incorporates optimal solutions to the subproblems. We may solve the subproblems independently.
․After making a cut, we have two subproblems. Max revenue r7: r7 = 18 = r4 + r3 = (r2 + r2) + r3 or r1 + r6
․Decomposition with only one subproblem: Some cut gives a first piece of length i on the left and a remaining piece of length n - i on the right.
length i 1 2 3 4 5 6 7 8 9 10
price pi 1 5 8 9 10 17 17 20 24 30
max revenue ri 1 5 8 10 13 17 18 22 25 30
),...,,,max( 112211 rrrrrrpr nnnnn
)(max1
inini
n rpr
Y.-W. ChangUnit 4 37
Recursive Top-Down “Solution”
․ Inefficient solution: Cut-Rod calls itself repeatedly, even on subproblems it has already solved!!
)(max1
inini
n rpr
Cut-Rod(p, n)1. if n == 02. return 03. q = - ∞4. for i = 1 to n5. q = max(q, p[i] + Cut-Rod(p, n - i))6. return q
1
0
.0 if ),(1
0 if ,1)( n
j
njT
nnT
Cut-Rod(p, 4)Cut-Rod(p, 3)
Cut-Rod(p, 1)T(n) = 2n
Cut-Rod(p, 0)
Overlapping subproblems?
Y.-W. ChangUnit 4 38
Top-Down DP Cut-Rod with Memoization․Complexity: O(n2) time
Solve each subproblem just once, and solves subproblems for sizes 0,1, …, n. To solve a subproblem of size n, the for loop iterates n times.
Memoized-Cut-Rod(p, n)1. let r[0..n] be a new array2. for i = 0 to n3. r[i] = - 4. return Memoized-Cut-Rod -Aux(p, n, r)
Memoized-Cut-Rod-Aux(p, n, r)1. if r[n] ≥ 02. return r[n]3. if n == 04. q = 0 5. else q = - ∞6. for i = 1 to n7. q = max(q, p[i] + Memoized-Cut-Rod -Aux(p, n-i, r))8. r[n] = q9. return q
// Each overlapping subproblem // is solved just once!!
Y.-W. ChangUnit 4 39
Bottom-Up DP Cut-Rod․Complexity: O(n2) time
Sort the subproblems by size and solve smaller ones first. When solving a subproblem, have already solved the
smaller subproblems we need.
Bottom-Up-Cut-Rod(p, n)1. let r[0..n] be a new array2. r[0] = 03. for j = 1 to n4. q = - ∞5. for i = 1 to j6. q = max(q, p[i] + r[j – i])7. r[j] = q8. return r[n]
Y.-W. ChangUnit 4 40
Bottom-Up DP with Solution Construction
․Extend the bottom-up approach to record not just optimal values, but optimal choices.
․Saves the first cut made in an optimal solution for a problem of size i in s[i].
Extended-Bottom-Up-Cut-Rod(p, n)1. let r[0..n] and s[0..n] be new arrays2. r[0] = 03. for j = 1 to n4. q = - ∞5. for i = 1 to j6. if q < p[i] + r[j – i]7. q = p[i] + r[j – i]8. s[j] = i9. r[j] = q10. return r and s
Print-Cut-Rod-Solution(p, n)1. (r, s) = Extended-Bottom-Up-Cut-Rod(p, n)2. while n > 03. print s[n]4. n = n – s[n]
i 0 1 2 3 4 5 6 7 8 9 10r[i] 0 1 5 8 10 13 17 18 22 25 30
s[i] 0 1 2 3 2 2 6 1 2 3 10
555 5
1 17
Y.-W. ChangUnit 4 41
Appendix C: Optimal Polygon Triangulation․Terminology: polygon, interior, exterior, boundary, convex polygon,
triangulation?․The Optimal Polygon Triangulation Problem: Given a convex
polygon P = <v0, v1, …, vn-1> and a weight function w defined on triangles, find a triangulation that minimizes w().
․One possible weight function on triangle:w(vivjvk)=|vivj| + |vjvk| + |vkvi|,
where |vivj| is the Euclidean distance from vi to vj.
Y.-W. ChangUnit 4 42
Optimal Polygon Triangulation (cont'd)․Correspondence between full parenthesization, full binary tree
(parse tree), and triangulation full parenthesization full binary tree full binary tree (n-1 leaves) triangulation (n sides)
․ t[i, j]: weight of an optimal triangulation of polygon <vi-1, vi, …, vj>.
Y.-W. ChangUnit 4 43
Pseudocode: Optimal Polygon Triangulation․Matrix-Chain-Order is a special case of the optimal polygonal
triangulation problem.․Only need to modify Line 9 of Matrix-Chain-Order.․Complexity: Runs in (n3) time and uses (n2) space.
Optimal-Polygon-Triangulation(P)1. n = P.length2. for i = 1 to n3. t[i,i] = 04. for l = 2 to n5. for i = 1 to n – l + 1 6. j = i + l - 17. t[i, j] = 8. for k = i to j-19. q = t[i, k] + t[k+1, j ] + w( vi-1 vk vj)10. if q < t[i, j]11. t[i, j] = q12. s[i,j] = k13. return t and s
Y.-W. ChangUnit 4 44
Appendix D: LCS for Flip-Chip/InFO Routing
I/O pad bump pad ring net assignment
(1) (2)
․Lee, Lin, and Chang, ICCAD-09; Lin, Lin, Chang, ICCAD-16․Given: A set of I/O pads on I/O pad rings, a set of bump
pads on bump pad rings, a set of nets/connections․Objective: Connect I/O pads and bump pads according
to a predefined assignment between the I/O and bumppads such that the total wirelength is minimized
Y.-W. ChangUnit 4 45
Sd 1 2 1 3 4 3Sb
3214
Minimize # of Detoured Nets by LCS
d1
d2
d3
d4
b1 b2 b3 b4 b1 b2 b3 b4
b1 b2 b3 b4
d1
d2
d3
d4d1 d1 d3 d3
d1
d2
d3
d4d1 d1 d3 d3
n1
n2
n3
n4
n2n3 n4
n4 n3n3n2 n1n1
n1n2
n3
n4
n1
Sd=[1,2,1,3,4,3]Sb=[3,2,1,4]
(1) (2)
(3) (4)
0 0 0 0000
0 1 1 33220 1 1 22220 0 1 11110 0 0 1110
net sequenceNeed detour for
only n3
․Cut the rings into lines/segments (form linear orders)
Y.-W. Chang 46
#Detours Minimization by Dynamic Programming
Longest common subsequence (LCS) computation
d1 d2 d3
b3 b1 b2
d1 d2 d3
b3 b1 b2
seq.1=<1,2,3>seq.2=<3,1,2> Common subseq = <3>
d1 d2 d3
b3 b1 b2
(c) LCS= <1,2>(a) (b)
d1 d2 d3
b3 b1 b2
d1 d2 d3
b3 b1 b2
d1 d2 d3
b3 b1 b2
(d) (e)
b4 b5 b6 b4 b5 b6 b4 b5 b6
chord set: {3,4,5} subset:{3} (f) MPSC = {4,5}
Maximum planar subset of chords (MPSC) computation
Y.-W. ChangUnit 7 47
Supowit's Algorithm for Finding MPSC․Supowit, “Finding a maximum planar subset of a set of nets
in a channel,” IEEE TCAD, 1987.․Problem: Given a set of chords, find a maximum planar
subset of chords. Label the vertices on the circle 0 to 2n-1. Compute MIS(i, j): size of maximum independent set between
vertices i and j, i < j. Answer = MIS(0, 2n-1).
Vertices on the circle
Y.-W. ChangUnit 7 48
Dynamic Programming in Supowit's Algorithm․Apply dynamic programming to compute MIS(i, j ).
Y.-W. Chang49
Ring-by-Ring Routing․Decompose chip into
rings of pads․Initialize I/O-pad
sequences․Route from inner rings to
router rings ․Exchange net order on
current ring to be consistent with I/O pads’ Keep applying LCS and
MPSC algorithms between two adjacent rings to minimize #detours current ring
preceding ring
Over 100X speedups over ILP
Y.-W. ChangUnit 4 50
Global Routing Results․The global routing result of circuit fc2624․A routing path for each net is guided by a set of segments
Segment
Y.-W. ChangUnit 4 51
Detailed Routing: Phase 1․Segment-by-segment routing in counter-clockwise order․As compacted as possible
Y.-W. ChangUnit 4 52
Detailed Routing: Phase 2․Net-by-net re-routing in clockwise order․Wirelength and number of bends minimizations
Y.-W. ChangUnit 4 53
Appendix E: Cell Based VLSI Design Style
Y.-W. ChangUnit 4 54
Pattern Graphs for an Example Library
Y.-W. ChangUnit 4 55
Technology Mapping
․Technology Mapping: The optimization problem of finding a minimum cost covering of the subject graph by choosing from the collection of pattern graphs for all gates in the library.
․A cover is a collection of pattern graphs such that every node of the subject graph is contained in one (or more) of the pattern graphs.
․The cover is further constrained so that each input required by a pattern graph is actually an output of some other pattern graph.
Y.-W. ChangUnit 4 56
Trivial Covering․Mapped into 2-input NANDs and 1-input inverters.․8 2-input NAND-gates and 7 inverters for an area cost
of 23.․Best covering?
an example subject graph
Y.-W. ChangUnit 4 57
Optimal Tree Covering by Dynamic Programming․If the subject directed acyclic graph (DAG) is a tree,
then a polynomial-time algorithm to find the minimum cover exists. Based on dynamic programming: optimal substructure?
overlapping subproblems?․Given: subject trees (networks to be mapped), library
cells․Consider a node n of the subject tree
Recursive assumption: For all children of n, a best match which implements the node is known.
Cost of a leaf is 0. Consider each pattern tree which matches at n, compute
cost as the cost of implementing each node which the pattern requires as an input plus the cost of the pattern.
Choose the lowest-cost matching pattern to implement n.
Y.-W. ChangUnit 4 58
Best Covering
․A best covering with an area of 15.․Obtained by the dynamic programming approach.