You are on page 1of 3

CMPUT 204 Tutorial Week 9

Question 1 Divide-and-Conquer
Suppose you are given an array A with n entries, with each entry holding a distinct number. You are told that the sequence of values A[1], A[2], . . . , A[n] is unimodal: For some index p between 1 and n the values in the array entries increase up to position p in A and then decrease the remainder of the way until position n. (So if you were to draw a plot with the array position j on the x-axis and the value of the entry A[j ] on the y -axis tile plotted points would rise until x-value p, where theyd achieve their maximum and then fall from there on.) Youd like to nd the peak entry p without having to read the entire arrayin fact, by reading as few entries of A as possible Show how to nd the entry p by reading at most O(log n) entries of A. (Hint : Assume n = 2k and nd the recurrence of the running time) Solution: Look at the value A[n/2] and its adjacent values: 1. if A[n/2 1] < A[n/2] < A[n/2 + 1] then p > n/2, we call recursively on entries n/2 + 1 through n. 2. if A[n/2 1] > A[n/2] > A[n/2 + 1] then p < n/2, we call recursively on entries 1 through n/2 1. 3. if A[n/2] > A[n/2 1] and A[n/2] > A[n/2 + 1] then p = n/2.

FindPeakEntry(A,a,b) m
a+b 2

if A[m] > A[m 1] and A[m] < A[m + 1] then FindPeakEntry(A,m + 1,b) else if A[m] < A[m 1] and A[m] > A[m + 1] then FindPeakEntry(A,a, m 1) else return m

Question 2 Shortest Common Supersequence


In lectures we have seen the problem of Longest Common Subsequence (LCS), where given two sequences X and Y we wanted to nd an LCS of X and Y (a subsequence of a sequence S is obtained by deleting some of the letters in S ). Given two sequences X and Y , we say Z is a common supersequence of them if both X and Y are subsequences of Z . For instance, abbacd is a supersequence of aba and bbcd. Given X = x1 x2 . . . xn and Y = y1 y2 . . . ym , the goal is to nd a Shortest Common Supersequence (SCS) of X and Y using dynamic programming. Hint : We can dene the subproblems and then give the recurrence for the solutions. Step 1 : Dene an appropriate array/table S (corresponding to the subproblems) and say how to extract the solution from the array. Step 2 : Give a recurrence (including the base cases) to compute the entries of your array. Briey explain how you derive this recurrence. Step 3 : Give a pseudocode to compute the entries of the array using your recurrence. Step 4 : Extract a Shortest Common Supersequence from the array. Solution: Step 1 : For any 0 i n and 0 j m, let Xi denote the sequence x1 . . . xi and Yj denote the sequence y1 . . . yj , respectively. Note that Xn = X and Ym = Y . We dene S [i, j ] (0 i n, 0 j m) to be the length of SCS of Xi and Yj . By this denition, the length of SCS of X and Y will be S [n, m]. Step 2 : Clearly the SCS of the empty string and any string T is T itself. Therefore for any value of 0 i n and 0 j m: S [i, 0] = i and S [0, j ] = j . If i, j 1 then to compute S [i, j ] we consider the last elements of Xi and Yj (i.e. xi and yj ): If xi = yj then they can be paired with the same element of an SCS of Xi and Yj . In other words, adding xi to the SCS of Xi1 and Yj 1 will form an SCS of Xi and Yj which has length S [i 1, j 1] + 1. If xi = yj then either we have to add xi to the SCS of Xi1 and Yj , or we have to add yj to the SCS of Xi and Yj 1 . Taking the minimum among these two cases gives the answer.

So we get the following recurrence: S [i, 0] = i and S [0, j ] = j , for any 0 i n, 0 j m. If i, j > 0 then: S [i 1, j ] + 1, S [i, j ] = min S [i, j 1] + 1, S [i 1, j 1] + 1, xi = yj