{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw5solution - Homework 5 Odd numbered problem solutions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Homework 5 Odd numbered problem solutions cs161 Summer 2009 Problem 1 (10 points): Algorithm: First we sort the set {x 1 ,x 2 ,…x n } to get a set of sorted real numbers S={y 1 ,y 2 ,…y n } Beginning with y 1 , cover the set with the interval K 1 = [y 1 ,y 1 +1], remove any points in S that are covered by K 1 . Now suppose that y i is the smallest real number in S that is not covered by K 1 , add in the interval K 2 = [y i ,y i +1] and remove all points covered by K 2 . Repeat this process until all the points are covered. We end up with a set T of intervals that cover S. Proof of Correctness: Suppose we are given another set of unit intervals T’ that covers S, we claim that T’ has at least as many intervals as T. For each interval K i in T, we pick out the left endpoint of K i , which we call z i , note that by the way we defined our intervals, Z = {z i | i=1 to i=|T|} is a subset of S. Now again by the way we defined the intervals in T, each z i is distance > 1 from any other element in Z. Because Z is a subset of S, in order to cover S, T’ must cover Z. Now since there are |T| elements in Z We must have at least |T| unit intervals in T’ to cover Z, one for each element in Z. If this is not the case, then there must be a unit interval in T’ that covers more than 1 point in Z, this is impossible because any two points in Z are more than distance 1 from each other. QED Running Time: Use your favorite O (nlog(n)) sorting algorithm to sort the set. The algorithm itself runs in O (n) time. Hence the overall running time is O (nlog(n))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problem 3 (10 points): Given the sequence s, we try to find the longest subsequence LP(s) of s such that LP(s) is a palindrome. We find LP(s) recursively: Given s, if s(1) = s(n) where n is the length of s, then LP(s) =s(1) + LP(s(2,n 1))+s(n), where s(2,n 1) is the subsequence of s with the first and last characters removed and ‘+’ denotes concatenation If s(1) !=s(n), then LP(s) = LP(s(1,n 1)) if |LP(s(1,n 1))| |LP(s(2,n))|, else: LP(s) = LP(s(2,n)) To include the base cases, we define LP(s(i)) = s(i) for any i from 1 to n To implement this using dynamic programming, we need to have an nxn lookup matrix where for the (i, j) entry we store s(i) if i=j (i.e. the diagonals) and fill in the i<j entries using our recursive
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}