quiz1sol

quiz1sol - Introduction to Algorithms Massachusetts...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Piotr Indyk and Charles E. Leiserson October 4, 2004 6.046J/18.410J Quiz 1 Quiz 1 • Do not open this quiz booklet until you are directed to do so. Read all the instructions first. • When the quiz begins, write your name on every page of this quiz booklet. • The quiz contains five multi-part problems. You have 80 minutes to earn 80 points. • This quiz booklet contains 5 pages, including this one. 1 �� • This quiz is closed book. You may use one handwritten 8 2 × 11�� crib sheet. No calculators or programmable devices are permitted. • Write your solutions in the space provided. If you need more space, write on the back of the sheet containing the problem. Do not put part of the answer to one problem on the back of the sheet for another problem, since the pages may be separated for grading. • Do not waste time and paper rederiving facts that we have studied. It is sufficient to cite known results. • Do not spend too much time on any one problem. Read them all through first, and attack them in the order that allows you to make the most progress. • Show your work, as partial credit will be given. You will be graded not only on the correct­ ness of your answer, but also on the clarity with which you express it. Be neat. • Good luck! Problem Parts 1 6 18 2 3 15 3 3 15 4 4 20 5 1 12 Total Name: Solutions Points Grade 80 Initials 6.046J/18.410J Quiz 1 Problem 1. Recurrences (6 parts) Name 2 [18 points] Solve the following recurrences by giving tight �-notation bounds. You do not need to justify your answers, but any justification that you provide will help when assigning partial credit. (a) T (n) = 3T (n/5) + lg2 n Solution: By Case 1 of the Master Method, we have T (n) = �(nlog5 (3) ). (b) T (n) = 2T (n/3) + n lg n Solution: By Case 3 of the Master Method, we have T (n) = �(n lg n). (c) T (n) = T (n/5) + lg2 n Solution: By Case 2 of the Master Method, we have T (n) = �(lg 3 n). 3 (d) T (n) = 8T (n/2) + n Solution: By Case 2 of the Master Method, we have T (n) = �(n2 log n). 3 (e) T (n) = 7T (n/2) + n Solution: By Case 3 of the Master Method, we have T (n) = �(n3 ). (f) T (n) = T (n − 2) + lg n n/2 n/2 Solution: T (n) = �(n log n). This is i=1 lg 2i � i=1 lg i � (n/4)(lg n/4) = �(n lg n). For the upper bound, note that T (n) � S (n), where S (n) = S (n − 1)+lg n, which is clearly O(n lg n). Problem 2. True or False, and Justify (3 parts) [15 points] Circle T or F for each of the following statements, and briefly explain why. The better your argument, the higher your grade, but be brief. Your justification is worth more points than your true-or-false designation. (a) T F If f (n) does not belong to the set o(g (n)), then f (n) = �(g (n)). Solution: False. For example f (n) = nsin(n) and g (n) = ncos(n) . 6.046J/18.410J Quiz 1 Name (b) T F An adversary can construct an input of size n to force R ANDOMIZED -M EDIAN to run in �(n2 ) time. Solution: False. The expected running time of R ANDOMIZED M EDIAN is �(n). This applies to any input. (c) T F A set of n integers in the range {1, 2, . . . , n} can be sorted by R ADIX -S ORT in O(n) time by running C OUNTING -S ORT on each bit of the binary representation. Solution: False. This results in �(log n) iterations of counting sort, and thus an overall running time of �(n log n). Problem 3. Short Answer (3 parts) [15 points] Give brief, but complete, answers to the following questions. (a) Consider any priority queue (supporting I NSERT and E XTRACT-M AX operations) in the comparison model. Explain why there must exist a sequence of n operations such that at least one operation in the sequence requires �(lg n) time to execute. Solution: Take an array A of n/2 elements. Insert the n/2 elements into the priority ` queue, and then extract-max n/2 times. This sorts the array a la Heapsort. If none of the operations in this sequence require �(lg n) time, then these n operations take o(nlogn) time, which is impossible in the comparison model, since we’ve shown an �(n/2 log(n/2)) = �(n log n) lower bound for any sorting algorithm. (b) Suppose that an array A has the property that only adjacent elements might be out of order—i.e., if i < j and A[i] > A[j ], then j = i + 1. Which of I NSERTION -S ORT or M ERGE -S ORT is a better algorithm to sort the elements of A? Justify your choice. Solution: I NSERTION -S ORT is a better choice: since its running time depends on the number of inversions in the array. We may swap each element with the element imme­ diately to its left, but that’s all that we do in I NSERTION -S ORT. So I NSERTION -S ORT is O(n) in this case, while M ERGE -S ORT remains �(n log n). 3 6.046J/18.410J Quiz 1 Name 4 (c) Suppose that a hash table with 3n slots resolves collisions by chaining, and suppose that n/4 keys are inserted into the table. For i = 1, 2, . . . , n/4 and j = 1, 2, . . . , 3n, define the indicator random variable Xij as Xij = � 1 if element i hashes to slot j , 0 otherwise . Under the assumption of simple uniform hashing (each key is equally likely to be hashed into each slot), give a formula in terms of the Xij for the expected number of keys that fall into slot 1, and solve your equation. Solution: E [Xij ] = P r[Xij = 1] = 1/(3n). Then the expected number of elements in n/4 n/4 slot 1 is E [ i=1 Xi1 ] = i=1 E [Xi1 ] = n/(4(3n)) = 1/12 by linearity of expectation. Problem 4. Finding the smallest elements of an array (4 parts) [20 points] In this question, we will explore several algorithms for finding the k smallest elements of an array of n integers, in sorted order. For example, given the array [5 2 1 9 6 7 3 4 8] and k = 3, such an algorithm should return [1 2 3]. (a) Algorithm A sorts the numbers using merge sort and outputs the first k elements in the sorted order. Analyze the worst-case running time of Algorithm A in terms of n and k . Solution: M ERGE -S ORT requires O(n log n) time to sort, and the extraction can be done in O(1) time each. Then the overall running time is �(n log n + k ). (b) Algorithm B builds a min-heap from the numbers and calls E XTRACT-M IN k times. Analyze the worst-case running time of Algorithm B in terms of n and k . Solution: B UILD -H EAP can be implemented in O(n) time, and E XTRACT-M IN takes O(log n) time. Thus overall the running time is �(n + k log n). (c) Algorithm C uses S ELECT to find the k th-largest element in the array, partitions around that number, and then insertion-sorts the k smallest numbers. Analyze the worst-case running time of Algorithm C in terms of n and k . Solution: We can S ELECT and PARTITION in �(n) time. Then insertion-sorting k numbers takes �(k 2 ) time. Overall the running time is �(n + k 2 ). (d) Briefly describe an algorithm that is asymptotically at least as good as Algorithms A, B, and C, and give its running time. You need not argue correctness. Solution: Do exactly the same thing as in Algorithm C, but use, say, mergesort instead of insertion sort. This gives a �(n + k log k ) running time, which asymptotically beats all three of A, B, C. 6.046J/18.410J Quiz 1 Name Problem 5. Mean 6.042 instructors (1 parts) 5 [12 points] There are two types of professors who have taught 6.042: nice professors and mean professors. The nice professors assign A’s to all of their students, and the mean professors assign A’s to exactly 75% of their students and B’s to the remaining 25% of the students. For example, for n = 8, the arrays [A A A B A B A A] and [A B B A A A A A] represent grades assigned by mean professors, and the array [A A A A A A A A] represents grades assigned by a nice profes­ sor. Given an array G[1 . . n] of grades from 6.042, we wish to decide whether the professor who assigned the grades was nice or mean. Give an efficient randomized algorithm to decide whether a given array G represents grades as­ signed by a mean or nice professor. Your algorithm should be correct with probability at least 51%. Solution: Here’s the algorithm: 1.Repeat the following t times: (a)choose a random index i � {1, . . . , n}. (b)if G[i] = B , then return “mean” 2.Return “nice”. First, note that with probability one, a nice professor will be noted as such, since there are no B ’s in G in this case. The probability that a mean professor makes it through an iteration of the loop is 3/4, since 1/4 of the entries of array are B ’s. The probability that the mean professor makes it through t iterations is (3/4)t . If we take t to be 3, then (3/4)3 < 0.49. Thus the probability of giving a correct answer is at least 51%. The running time is obviously O(1), since we do a constant number of iterations of constant-time loop. Comment: The above arguments show that •If a professor is nice, the probability of an incorrect answer is 0 •If a professor is mean, the probability of an incorrect answer is � 49% However, in several cases, people were making “reverse” statements, e.g.,: ”If we get an A, then the probability that the professor is mean is ...” Although intuitive, a closer inspection reveals that such a statement does not have any real meaning. This is because there is no probability distribution determining if the professor is mean or not. In case of randomized algorithms, the input is not random. Instead, for any input, the algorithm should report the correct answer with at least certain probability. ...
View Full Document

This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.

Ask a homework question - tutors are online