This is the end of the preview.
Sign up
to
access the rest of the document.

Unformatted text preview: CS 473: Fundamental Algorithms, Spring 2011 HW 4 Homework is due by Monday, 23:59:59, February 21 Problem 1 is due by Sunday, 23:59:59, February 20 This homework contains four problems. Read the instructions for submitting homework on the course webpage . Collaboration Policy: For this homework, Problems 2–4 can be worked in groups of up to three students. Problem 1 should be answered in Compass as part of the assessment HW4-Online and should be done individually. 1. (30 pts.) Short questions to be answered on compass individually. 1 2. (20 pts.) Longest calculation sequence. A sequence of numbers x 1 ,x 2 ,...,x u is a calculation sequence if for any i ≥ 3, we have that x i is equal to of the following values: (i) x i- 1 + x i- 2 , (ii) x i- 1- x i- 2 , (iii) x i- 1 * x i- 2 , or (iv) x i- 1 /x i- 2 . You are given a sequence of numbers y 1 ,...,y n . (A) (10 pts.) Describe a recursive algorithm that finds the longest subsequence of the num- bers that is a calculation sequence. What is the running time of the algorithm? Solution : Assume that n ≥ 2; if n = 1 then output y 1 . We give the following recursive algorithm that given two integers i and j such that 1 ≤ i < j ≤ n , gives the length of a longest calculation subsequence (LCS) ending with y i and y j ; we will show how to retrieve the LCS later. Algorithm RecursiveLCS ( i,j ): Input: a sequence of numbers y 1 ,y 2 ,...,y n , and two integers i and j s.t. 1 ≤ i < j ≤ n . MaxLen = 2 For k = 1 to i- 1 If y k ,y i ,y j is a calculation sequence then If RecursiveLCS ( k,i ) + 1 > MaxLen then MaxLen = RecursiveLCS ( k,i ) + 1 /* no need to call RecursiveLCS ( k,i ) twice. */ Return MaxLen We run RecursiveLCS ( i,j ) for all 1 ≤ i < j ≤ n , and let i * and j * are such that RecursiveLCS ( i * ,j * ) = max 1 ≤ i<j ≤ n RecursiveLCS ( i,j ). The running time is O ( n n ). We now discuss how to obtain the LCS . Since we do not care about efficiency for this problem, we will use one of the most obvious ways. We give a sketch. By calling RecursiveLCS multiple times, we find t 1 ,t 2 ,....,t l- 1 = i * ,t l = j * such that RecursiveLCS ( t k- 2 ,t k- 1 )+1 = RecursiveLCS ( t k- 1 ,t k ) for all 3 ≤ k ≤ l , then we output y t 1 ,y t 2 ,...,y t l . We can show that the total running time is O ( n n +2 ). Rubrik : • By the definition, a sequence of at most two numbers is a calculation sequence. Hence the algorithm should output say y 1 if n = 1, and say y 1 ,y 2 if n = 2. However, this time, we will not deduct any point if the algorithm outputs say “No LCS” when the LCS has a length of at most two. • 9pts: If the writeup gives a correct recursive algorithm. If the the algorithm is incomplete but gives a key idea that could lead to a correct answer, then it gets 6pts....
View
Full Document