You are on page 1of 6

EECS 281 Data Structures and Algorithms Homework 2 Winter Semester, 2012

Assigned: 24Jan12 Due: 02Feb12, 5:00pm Instructions: Homework is worth 20 total points. Two, out of five, problems will be randomly selected for grading. Homework is to be completed individually. Include name and uniqname on the first page. Homework is to be handed in as an attachment to ctools in pdf format (note that we changed formats from the previous homework which was due in MS Word). If you are using MS Word you can save the document as a pdf by going to File->Save as and then changing the type to pdf. A key for the homework set will be posted after the due date for student use. Homework Goals Observe a recursive algorithm and derive the associated recurrence relation, solve the recurrence relation in closed-form, and determine big-oh based upon the solution to the recurrence; Reason about and understand heapsort

Problem 1: 10 points Implement the following function recursive algorithm to recognize palindromes in a substring of s (specified by left and right inclusive). A palindrome is defined to be any string that is identical when reading character-by-character forwards and backwards. As an example, the following are palindromes: bob a hannah madam im adam

// white spaces should be ignored

The following are not palindromes: apple hanna

You may make the following assumptions: string s is at least (right left + 1) long; left >= 0; right <= s.length() 1; and s is NOT null

bool isPalindrome(string s, int left, int right) { while(s[left] == ' ' && left < right) left++; while(s[right] == ' ' && left < right) right--; if(s[left] != s[right]) return false; else if(left == right || left == right-1) return true; else return isPalindrome(s, left+1, right-1); }

Problem 2: 10 points Given the following recurrences, derive the closed form solution to them:

A)

T(n) = T(n/2) + log2(n), T(1) = 1, where n is a power of 2

We apply the change of variables n = 2m. => T(2m) = T(2m-1) + m = T(2m-2) + (m-1) + m = = T(20) + m + (m-1) + .. + 1 = [m(m+1)]/2 + 1 Now we reverse the change of variables. n = 2m => m = log2(n) Thus, T(n) = [log2(n)(log2(n)+1)/2] + 1

B)

T(n) = 3 * T(n-1) + 5, T(0) = 0

First solve the homogenous equation T(n) 3T(n) = 0. Here the characteristic equation is q 3 = 0 => q = 3. Thus the general solution to the homogenous form is T(n) = c*3 n . We guess a particular solution in the form of a constant, which must satisfy a = 3a + 5 => a = -5/2 Thus the general solution to the nonhomogenous difference equation is T(n) = c*3 n (5/2). Plugging in the initial condition yields: T(n) = (5/2)(3n-1)

Problem 3: 10 points Suppose that there exists a new memory technology that supports rapidly identifying the largest value stored in a range of memory locations. A software interface is provided to this feature as follows: index = max(int a[], int left, int right) The max() function returns the index (not the value) in the array a[] of the largest element stored in the range left through right (inclusive). If there is a tie, it returns the index of one of the largest elements. Assume that n = right left + 1. A) You learn that max()requires O(log(log(n)) time to run, where n is the number of elements in the range. Given this performance, how quickly can you sort an array of n items? State your answer in bigOh notation in the box. Solution: O(n*log(log(n))) because you need to find the max of the list n times to create a sorted array.

B) Suppose that a new generation of this memory technology can identify the largest element in O(1) time. Now how quickly can you sort an array of n items (in big-Oh notation)? Solution: O(n) because you need to find the max of the list n times to create a sorted array.

C) Complete the pseudo-code below to efficiently sort an array A in descending order using this max() function. Algorithm memsort(A, left, right) Input: array of items a of size (right left + 1), integer left, integer right Output: sorted array of items a Memsort(A, left, right) While( left != right ) index = max(A, left, right) temp = left A[left] = A[index] A[index] = temp left++

Problem 4: 10 points A) Given the following algorithm derive the recurrence relation: int Fibo(int number) { if (number <= 2) // base case return 1; else return Fibo(number - 2) + Fibo(number - 1); } // end Fibo Solution: a(n) = a(n-1) + a(n-2) s.t. a(2) = 1 The complexity of this calculations is T(n) = T(n-1) + T(n-2) + 1 s.t. T(1) = T(2) = 1 B) Assume the recurrence relation to the above problem is: T(n) = T(n/2) + 3; T(1) = 3 (which by the way it is not!) Derive the solution to the recurrence. Solution: We apply the change of variable n = 2m. Thus the recurrence is T(2m) = T(2m-1) + 3 = .. . = T(20) + 3m = 3 + 3m Reversing the change of variables we get T(n) = 3(log2(n)) + 3 C) Assume the solution to the recurrence relation is: cn + n*(logn)2 + (10*n)c (which, by the way, is not!). Derive the big-oh complexity. Solution: The complexity of this calculation is defined by f s.t. ( ) {

( )

This is because the complexity is exponential if c > 1, does not achieve exponential complexity if c <= 1 (in these cases the nlogn^2 term dominates.

Problem 5: 10 points The array below contains numeric data values as shown. The first element of the array is at the left. The values in the array are to be sorted using the Heapsort algorithm as presented in lecture.

A) In the spaces provided below, list in order the pairs of the above data values (not the array indices) that are swapped in the process of building the heap from this array (heapifying it). If you need additional spaces, you may continue your answer on the back of this sheet.

(__8___, ___5__) (__4___, ___6__) (__1___, ___9__) (__3___, ___9__) (__3___, ___8__) (__3___, ___7__) (______, ______) (______, ______) (______, ______) (______, ______) (______, ______) (______, ______)
B) What is the total number of swaps needed to heapify this array? 6

C) Show the final contents of the array after it has been made into a heap (but before the actual sorting of the values) in the spaces below:

You might also like