Tutorial Exercise (Dynamic Programming) 1. CS3381 Design and Analysis of Algorithms Helena Wong, 2001 For the equations of the assembly-line scheduling problem: f *= min (f1[n] + x1, f2[n] + x2) e1+a1,1 f 1(j) = min (f 1[j -1] + a1,j, f2[j-1] + t 2,j-1+ a1,j) e2+a2,1 f 2(j) = min (f 2[j-1] + a2,j, f1[j -1] + t 1,j-1+ a 2,j) if j=1 if j>1 if j=1 if j>1 Let ri(j) = number of references made to fi[j] in a recursive algorithm, We have r1(n) = r2(n) = 1 r1(j) = r2(j) = r1(j+1) + r2(j+1) Show that ri(j) = 2n-j. [Hint: You may use substitution method to prove ri(n-k) = 2k first] 2. Using the result of question 1, show that the total number of references to all f i[j] values, is exactly 2n+1-2. What does this imply to the running time of a recursive algorithm for the Assembly-Line Scheduling Problem? Compare it with the running time of the “FASTEST-WAY” algorithm. 3. For the “MATRIX-CHAIN-ORDER” algorithm used in our Matrix-Chain Multiplication problem a. By observation, state the tight upper bound of the running time. b. Show that the total number of times line 9 being executed is (n3-n)/6. [Given 1+22+32+..n2 = n(n+1)(2n+1)/6.] c. Make a conclusion about the asymptotic tight bound of the running time of the algorithm. 4. Draw the recursion tree for the merge-sort procedure on an array of 16 elements. Explain why memoization is ineffective in speeding up a good divide-and-conquer algorithm such as MERGE-SORT. Tutorial Exercise (Dynamic Programming) CS3381 Design and Analysis of Algorithms Helena Wong, 2001 5. Show that the running time of the MEMOIZED-MATRIX-CHAIN algorithm is O(n3) (text book pp. 348-349) 6. A dynamic programming solution to the LCS problem, LCS-LENGTH, is illustrated below. What is the asymptotic upper bound of its running time? Write a Memoized version of LCS-LENGTH. Compare the performances of the 2 solutions. 7. Repeat question 6 for the OPTIMAL-BST algorithm LCS-LENGTH(X,Y) 1 m = length of X 2 n = length of Y 3 for i = 1 to m 4 c[i,0] = 0 5 for j = 1 to n 6 c[0,j] = 0 7 for i = 1 to m 8 for j = 1 to n 9 if xi=yj 10 c[i,j] = c[i-1,j-1] + 1 11 b[i,j] = “use result of c[i,j] plus xi” 12 else if c[i-1,j] >= c[i,j-1] 13 c[i,j] = c[i-1,j] 14 b[i,j] = “use result of c[i-1,j]” 12 else 13 c[i,j] = c[i,j-1] 14 b[i,j] = “use result of c[i,j-1]” 15 return c and b OPTIMAL-BST() 1 for i = 1 to n+1 2 e[i,i-1] = qi-1 3 w[i,i-1] = qi-1 4 for len = 1 to n 5 for i = 1 to n-len + 1 6 j = i + len - 1 7 e[i,j] = 8 w[i,j] = w[i,j-1] + pj + qj 9 for r = i to j 10 t = e[i,r-1] + e[r+1,j] + w[i,j] 11 if t < e[i,j] 12 e[i,j] = t 13 root[i,j] = r 14 return e and root