Sunteți pe pagina 1din 4

CMPU241: Exam I Solution

1. (6 points) Answer the following questions by circling T (true) or F (false) or by lling in the blank (Note: stating your assumptions about the question may inuence the correctness of your answer. Explaining your answer is often a good idea): (a) (1 point) True or False: The average-case behavior of an algorithm is always better than its worst-case behavior. False. E.g., see insertion sort. (b) (1 point)True or False: The average-case behavior of an algorithm is never better than its best-case behavior. True. If the average-case were better, it would be the best-case. (c) (1 point) True or False: In general, the amount of work done by an algorithm depends on the size of the input. True. (d) (2 points) Suppose the running time of a divide-and-conquer algorithm is given by the following recurrence: n T (n) = 3T ( ) + (n) 4 This algorithm partititions the original problem into 3 subproblems, each of size to partition and/or recombine the data into the nal solution.
n 4.

It spends (n) time

(e) (1 point) True or False: If the input is a sorted array of n elements, the selection problem can be solved in (lgn) time. False. If the array is sorted, the selection problem can be solved in O(1) time and O(1) O(lgn). 2. (4 points) Insertion sort can be expressed as a recursive procedure as follows. In order to sort A[1 . . . n], we recursively sort A[1 . . . n 1] and then insert A[n] into the sorted array A[1 . . . n 1]. Write a recurrence equation for the running time of this recursive version of insertion sort and solve the recurrence to nd a tight asymptotic bound on the running time. T (n) = T (n 1) + n 1 = (n2 ) (from a homework problem). 3. (16 points) For each part, (a) - (h) circle all asymptotic bounds that apply. For example, if the running time is (n), then it is also (n), O(n), (1), and O(n2 ). (a) What is the worst-case time for Merge-Sort on n distinct elements?
(1) (n) (n log n) (n log n) O(n log n) O(n2 )

(b) What is the worst-case time for Max-Heapify on an array of n distinct elements?
(1) (log n) (log n) O(log n) O(n) O(n log n)

(c) What is the running time of Quick-Sort on n identical elements?


(1) (n) (n log n) (n2 ) (n2 ) O(n2 )

(d) What is the worst-case time for Counting-Sort on n distinct elements, each in the range 1 to lgn?
(1) (n) (n) O(n) O(n log n) O(n2 )

(e) What is the worst-case time for Counting-Sort on n distinct elements, each in the range 1 to n2 ?
(1) (n) (n log n) (n2 ) (n2 ) O(n2 )

(f) What is the best-case time for Heap-Sort on n distinct elements?


(n) (lg n) (n log n) (n log n) O(n log n) O(n2 )

(g) What is the amount of extra storage used by Merge-Sort on n distinct elements?
(1) (n) (n) O(n) O(n log n) O(n2 )

(h) What is the worst-case time for Build-Max-Heap on an unordered array of n distinct elements?
(1) (n) (n) O(n) O(n log n) O(n2 )

4. (10 points) Consider the following sorting algorithm Crazy-Sort:


Crazy-Sort(A) if (|A| > 1) then Crazy-Sort(1st Crazy-Sort(2nd Crazy-Sort(3rd Crazy-Merge(3

third of array A) third of array A) third of array A) sorted thirds of array A)

Assume that Crazy-Merge takes time (lgn) for an array A of size n. Write down the recurrence for the running time of Crazy-Sort and solve this recurrence to nd tight asymptotic bounds for the running time of this algorithm. If you use the Master Theorem, state the case you are using and why. The recurrence is T (n) = 3T ( n ) + (log n) 3 a = 3, b = 3, f (n) = log n. nlogb a = nlog3 3 = (n) Since f (n) O(n), case 1 applies and T (n) = (n) 5. (14 points) Consider the following algorithm.
Secret(array A[0 . . . n 1] of real numbers) 1. minval = A[0]; maxval = A[0] 2. for i = 1 to n-1 do 3. if A[i] < minval 4. minval = A[i] 5. if A[i] > maxval 6. maxval = A[i] 7. return maxval - minval

(a) (2 points) What is the basic operation in the above algorithm (give range of line numbers included in the basic operation, if necessary), and how many times is it executed? The basic operation is composed of lines 2 - 6. These lines are executed n-2 times. (b) (2 points) What is the asymptotic running time of this algorithm? O(n). (c) (10 points) State the purpose of this algorithm, give a loop invariant, and use the loop invariant to prove the algorithm correctness. The algorithm nds the range of data values (i.e., the dierence between the maximum value and the minimum value). Loop Invariant: At the start of each iteration i of the for loop, variables minval and maxval hold the minimum and maximum values of items in A[0 . . . i 1]. Initially, the values of minval and maxval are set to A[0], and this is trivially the min and max value in array A[0 . . . 0].

For maintenance, assume the invariant is true at the end of some iteration j when j 1 and show the invariant holds at the end of iteration j. At the beginning of iteration j, we have that minval and maxval hold the min and max elements in A[0 . . . j 1], respectively, by assumption (the IHOP). In iteration j, the element in position j is compared to minval and maxval. If A[j] is less than minval, minval is set to A[j] and if A[j] is greater than maxval, maxval is set to A[j]. Therefore, at the end of iteration j, minval and maxval hold the min and max value in A[0 . . . j], respectively. The loop terminates when i = n. At this point, minval and maxval hold the min and max values, respectively, in A[0 . . . n 1], which is the condition we require for algorithm correctness. 6. (20 points) Suppose you are given a list L to sort that contains only zeros (0s) and ones (1s). (a) (5 points) Assume that L contains n0 zeros (0s) and n1 = n n0 ones (1s). If you use insertion sort, what would be a worst-case input? Give an asymptotic upper bound on the number of comparisons that will be performed by insertion sort on your worst-case input. Justify your answer. One possible worst case input would contain n 0 s and n 1 s, with all the 1s preceding all 2 2 the 0s. The worst case number of compares in this case would be O(n2 ). (b) (10 points) Describe an in-place algorithm for this situation that runs in O(n) time in the worst-case. Your algorithm may use (call) any of the algorithms or subroutines that weve studied in class (you do not need to reproduce those algorithms here). Analyze the worst-case running time of your algorithm. One solution is to use the Partition function. After one iteration, it would have partitioned all the 0s into the left side and all the 1s into the right side of A. The worst-case running time is O(n), the running time of one call to Partition. (c) (5 points) Does your algorithm in part (b) constitute a counter-example to the (nlgn) lower bound known for sorting? Why or why not? Explain. No. This algorithm would not work for input instances that contained more than 2 values (arbitrary instances). 7. (15 points) Assume you are given k sorted lists, each containing n/k elements. Assume the lists are sorted in non-decreasing order (i.e., the smallest item is rst and the largest last). (a) (5 points) Describe an O(k)-time algorithm for nding the minimum of the n elements contained in all k lists. Go through all the k smallest elements and remember the smallest value found. Time = O(k 1).

(b) (10 points) Describe an O(ilgk + k) time algorithm for solving the general selection problem in this case (i.e., give an algorithm for nding the ith order statistic). (Hint: Your algorithm should make use of a heap containing k items.) State the algorithm (pseudo-code or text) and analyze its running time. Solution: First use Build-Min-Heap on the k smallest elements (O(k) time), carrying their lists along as satellite data. Then use Extract-Min i times to nd the i smallest of the smallest elements along with their attached lists (O(i log k) time). The actual ith order statistic will 3

be in one of these i lists. To see why, consider that if the ith smallest element was not in one of those lists, then some other list would have been included in the i smallest in place of one of the other lists we found. Then, starting on the list with the smallest key, setting counter j = 1, and key to the smallest element in that list: while (j < i) compare next element on the current list to the element at the top of the next list. Set key to smaller value (if equal, stay in current list). j = j + 1. end while. return key (O(i) time). Running time = O(k) + O(i log k) + O(i) = O(k + i log k). 8. (20 points) Give tight asymptotic bound for T (n) in each of the following recurrences. Assume that T (n) is constant for suciently small n. Justify your answers and show all your work. (a) (5 points) T (n) = 4T ( n ) + n2 n 2 5 a = 4, b = 2, f (n) = n2 n = n 2 . logb a 2 2 n = n = (n ) Since f (n) (n2 ), case 3 applies and T (n) = (n2 n) (b) (5 points) T (n) = T ( n ) + T ( n ) + T ( n ) + n 2 4 8 Use the good guess method to prove that T (n) cn for some constant c > 0. We have T (n) c = = = n n n +c +c +n 2 4 8 n cn c( + 1) + 5 + 8c + an 7 7 7cn +n 8 7c (1 + )n 8 cn. (1) (2) (3) (4) (5)

Where inequality (5) holds if c 8. Therefore, T (n) = O(n). Showing that T (n) = (n) is easier: T (n) = T ( n ) + T ( n ) + T ( n ) + n n. 2 4 8 Since T (n) = O(n) and T (n) = (n), we have that T (n) = (n). (c) (5 points) T (n) = 3T ( n ) + n 4 a = 3, b = 4, f (n) = n. nlogb a = nlog4 3 = (n1 ) Since f (n) (n1 ), case 3 applies and T (n) = (n) (d) (5 points) T (n) = 8T ( n ) + n2 2 a = 8, b = 2, f (n) = n2 . nlogb a = nlog2 8 = (n3 ) Since f (n) O(n3 ), case 1 applies and T (n) = (n3 )

S-ar putea să vă placă și