This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology SingaporeMIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson October 17, 2001 6.046J/18.410J SMA5503 Quiz 1 Solution Quiz 1 Solution Figure 1: grade distribution Problem Points Grade 1 2 3 Total Name: 16 19 45 80 Initials MIT students: Circle the name of your recitation instructor: Bob Chong George Jennifer Rachel 6.046J/18.410J/SMA5503 Quiz 1 Solution
Problem 1. Matchup [16 points] Name 2 Fill in the following table by specifying, for each algorithm, the letter of the recurrence for its running time and the numeral of the solution to that recurrence. Points will be deducted for wrong answers, so do not guess unless you are reasonably sure. Algorithm Merge sort (worst case) Binary search (worst case) Randomized quicksort (expected) Strassen’s algorithm (worst case) Selection (worst case) Randomized selection (expected) Letter a b c d e f g h Recurrence 1 2 3 4 5 6 ¥¦£ihw¨'f¦¤£¢ g £¡ ¥ ¡ ¥ " ¨'f¦¤£§ £¡ ¥ ¡ ¥(¡ ¥ ¡ Av'f¦¤£¢ ¥usrq¨'f¦¤£§ t p £¡ ¥ ¡ ¥¦£ih¨'f¦¤£¢ g¡ ¥ ¡ ¥ £¡ ¥ ¡ ©¨'f¦¤£¢ ¥ £¡ ¥ ( b £¡ ¥ £¡ ¦¨'e#0dc¨§%©¨§ aH V`G ¥ £¡ ¥ U¡ X W ¦¨' ¦VT§ Y8F E £ ©¨§ ¥ £¡ ¥ (¡ ¥ £¡ ¥ £¡ 0)! ©©¨§©¨§ SRPH T" AE QIG ¥ ¡ ¥ U¡ ¦¤£'&¦V7¢ E F £ ©¨§ ¥ £¡ ¥ £¡ ¥ £¡ ¥ £¡ ¦¨' ©©¨§©¨§ ¥¦¨¡' ¦DC BA(9¦87¢6¦55©2£0§%©¨§ £ ¥ @ £ ¡ ¥ 4 3 1 ¡ ¥ £¡ ¥ (¡ ¥ £¡ ¥ £¡ 0)'&¦©¨§%©¨§ ¥ " ¡ ¥ £¡ ¥ £¡ $#¤£! ©©¨§©¨§ ¥¡ ¦¤£¢ Recurrence d b g a c e Solution 6 2 6 3 1 1 Numeral Solution 6.046J/18.410J/SMA5503 Quiz 1 Solution
Problem 2. Merging several sorted lists [19 points] Name 3 Professor Fiorina uses the following algorithm for merging sorted lists, each having elements. She takes the ﬁrst list and merges it with the second list using a lineartime algorithm for merging two sorted lists, such as the merging algorithm used in merge sort. Then, she merges the resulting list of elements with the third list, merges the list of elements that results with the fourth list, and so forth, until she ends up with a single sorted list of all elements. (a) Analyze the worstcase running time of the professor’s algorithm in terms of and . elements, takes time. MergSolution: Merging the ﬁrst two lists,each of ing the resulting elements with the third list of elements takes time, and so on. Thus for a total of list, we have: U ©£ U U ©£ £ U£ ¦8 £ U ©£ ¥ U £¡ V©¨V U ¥( U ¥ U Adb 7¡ " T¡ £ H ¡ F U£ G U " H £ 7¡ G F U § § § U £U £ U A©©¨ £ 8 U ¦£ U U ¦£ ¦¤¢ ¥ £¡ U U£ ¦8 U£ ¦5 6.046J/18.410J/SMA5503 Quiz 1 Solution Name 4 (b) Brieﬂy describe an algorithm for merging sorted lists, each of length , whose worstcase running time is . Brieﬂy justify the running time of your algorithm. (If you cannot achieve , do the best you can for partial credit.) Solution: There are several possible answers (only one is required). One method is to repeatedly pair up the lists, and merge each pair. This method can also be seen as a tail component of the execution merge sort, where the analysis is clear. Finally, a conceptually simple method is to store a min priority queue of the minimum elements of each of the lists. At each step, we output the extracted minimum of the priority queue, and using sattelite data determine from which of the lists it came, and insert the next element from that list into the priority queue. U ¦£ U U ¥VU h w¤£¡ g ¥g VU h w¤£¡ U 6.046J/18.410J/SMA5503 Quiz 1 Solution
(c) Argue that there are Name 5 different ways to interleave Solution: lists, each of length , into a single list of length . For the ﬁrst list, Note that of the possible permutation of the elements in the list, only 1 permutation is valid because the elements in the list is already sorted. Hence the term in the denominator. For the second list, And so on. Therefore, total number of ways of forming the from elements is: lists, each of size This is exactly the number of different ways of interleaving the list of elements list, into the single U ©£ ¢¥ GE ¡ ¢¥ E G G c¨¡ ¢¥ E ¡ ¢¥ E § YW G ©b b £ G XG ¥ X¥ ¢¥ E § YG W G ©b ¨¡ £¥ E ¨" G W G ¦c¨¡ £ § ¥b£ U U GG GG £ ¨¡ § § E¢ E ¥ E b £ b ¡ £ b £ ©©§ £¥ u¡ ¥ ¢G "c¨c¨¡ ¢¥ E ¡ ¢¥ ¤E £ c¨¡ U number of ways of choosing elements from the remaining elements b£ £¥ GE ¡ £¥ E G " c¤¡ GE b ¤£¡ , £¥ E ¢¥ GI¡ £¥ ¢¥ GE ¡ GE b ¤£¡ Ub £ c£ U number of ways of choosing elements from elements £ ¡£ U ¦£ £ ¥ £ G ¥ ¢VU ¦¨¡ ¡ ¡£ £ £ U U G ¥ ¢¥ ¦¤£¡ ¡ ¢¥ ¤£ GE ¡ £ £ 6.046J/18.410J/SMA5503 Quiz 1 Solution
(d) Prove a lower bound of algorithm for merging .) Name 6 Solution: Consider the decision tree used to make the comparisons. The worstcase running time corresponds to the height of the tree: ¥g VU h w¤£¡ ¥ ihw£ ¢U ihw£ gbg U h gw£ !£ ©¨£ b ¥ ihw6c£ ihw£ § g£b g U h gw£ !£ ihw6b E ¥ ¥ £ ¡ ihg £ g£ U h gw£ !fh w6¤£ ihg £ g £ b U g £ b £ h w6¤£ ihg ¥¤ U ¡ g b G 9¦¥¥ ¡ Yh 6¤£ ihg £ U£ ¡ g b G ¥ ¢¥ £ ¡ Yh 6¤£ ihg ¥ U £ G ¥ ¢V©¨¡ ¡ ihg ¦¤¢ ¥ £¡ U ¦£ ¡£ U ¥g VU h w¤£¡ on the worstcase running time of any comparison sorted lists each of length . (Hint: Use the fact that E £ ¡ ¡£ ¢E 0¥ ¦¤£¡ ¡¥ 6.046J/18.410J/SMA5503 Quiz 1 Solution
Problem 3. True or False, and Justify [45 points] Name 7 Circle T or F for each of the following statements, and brieﬂy explain why. The more content you provide in your justiﬁcation, the higher your grade, but be brief. Your justiﬁcation is worth more points than your trueorfalse designation. (a) T F A constant exists such that for any , there is an array of such that insertion sort runs faster than merge sort on that input. Solution: true. If the elements are already in sorted order, the running time for insertion sort is , whereas that of merge sort is . (b) T F Consider the algorithm from the textbook for building a maxheap: B UILD M AX H EAP 1 heapsize length 2 for length downto 3 do M AX H EAPIFY On an array of elements, this code runs in time in the worst case, because there are calls to M AX H EAPIFY , and the worstcase time for any call is . Solution: false. is not a tight bound. In fact, BuildMaxHeap runs in during recitation. time, as proven £ ¥ £¡ ©¨' ¥£ g ¡ ¦fh w¤£' a£ £ £ ¥£ £ ©9 §© ¨¡ ( ¥ ¡ ¡ ¡ £ ¤¡ ¢ ¨ £ © ¤¡ ¢ ¥ ¡¡ ¥ £¡ ©¨' ¥£ ¦¤¡ ¢ ( £a£ £ elements ¥£ ¡ ¦fh ¤g! § ¥¡ £ ¥£ ©¨¡ £g fihw£ 6.046J/18.410J/SMA5503 Quiz 1 Solution Name 8 (c) T F Given a number and a positive integer , the value can be computed in time by repeated squaring. (Assume that multiplying two numbers takes constant time.) Solution: true.
£¡ ¦¥ (d) T F An adversary can force randomized quicksort to run in as input an already sorted or reversesorted array of size . Solution: false. The running time is not dependent on the input that the adversary provides. This is because radomized quicksort will randomly choose a pivot to partition, independent of the input. (e) T F For any integervalued random variable
§ © ¨ , we have Solution: true £ ¥ " ¤£¡ ¦ (£ ¢b £ § B( £ @ @£§ £ § ¢ %bB( b B( § © ¨ % Note: it is important to understand that = ¥£ g£ ©h w¨¡ ¥g ©£ h w¤£¡ ¥ £¡ ¦¨' ¥ (¡ ¥ ( £¡ 0v'&¦Adb ¨§5 £ $#¢ ! £ $#¢ ¥¡ ¦¤£' ¡ £ ¡ ¦ ! " ©¨§ ¥ £¡ (£ @ £ § © ¨ § © ¨ § © ¨ Letting = , we have: . time by providing § ¤ "¥ ¤" ¤" £¡ ¤¢ ¤" ¤" £ ¤" ¥ £¡ ©¨§ ¥£ g£ ©h w¨¡ 6.046J/18.410J/SMA5503 Quiz 1 Solution Name 9 (f) T F Bucket sort is a suitable auxiliary sort for radix sort. Solution: true. An auxilliary sort for radix sort must be stable. Bucket sort is stable and hence suitable for use as an auxilliary sort for radix sort. (g) T F Consider two implementations of a hash table with slots storing keys, where . Let be the expected time for an unsuccessful search in the table if collisions are resolved by chaining, using the assumption of simple uniform hashing. Let be the expected time for an unsuccessful search in the table if collisions are resolved by open addressing, using the assumption of uniform hashing. Then, we have . Solution: false. In the worst case, in the chaining version, we need to search through all the values in one particular slot. On the other hand, in the open addressing version, we need to search through every slot. Hence . £ ¥ ¦£ £ 0£¤ ' ©£ £ $¨ ¥ ¡ ¤ ¡ © ¥ ¡ ¢ b¡ ¥ E ( B( ! ¥ £ £ )! (¡ £ § ¥ ¦£ £ 0£¤ ' f©£ £ $¦ ¥ ¡¤ ¡ ¥ ¡¢ ©£ £ A¥ ¥ ¡¤ ©£ £ $£ ¥ ¡¢ ¥ ¡¤ ¦£ £ A¥ ¥ ¡¢ ©£ £ $£ £ ¡£ 6.046J/18.410J/SMA5503 Quiz 1 Solution Name 10 (h) T F Let be a class of universal hash functions for a hash table of size . Then, if we use a random to hash keys into the table, the expected number of collisions is at most . Solution: true. Number of ways to choose 2 keys out of keys is Therefore, the expected number of collisions, number of collisions Solution: true. Finding the distance of every point from the origin takes . We can then use Select to get the point. That again takes . Finally, partitioning around the point requies . Each of the steps takes . Therefore, total time is © (i) T F Given a set of points in the plane, the points closest to the origin (using normal Euclidean distance) can be found in time in the worst case. ¡ ¢£ £ £ ¥£ ©¨¡ ( ¡£ ( 0eb ¨¡ £ ¥( £ £ ( 0eb ¨¡ £ ¥( £ ¥£ ©¨¡ § Y8E ¥ XW " E ¥£ ©¨¡ £ ¡ £ £ ¥ ¥@ D@ 7¡ ¥ E ¨ E # ¡ ¥ " ¨ " # ¡ ¥ X ¨ X # ¡ § § § £ £ 0( ¤ 8¦¥£ ¥ ¦¤£¡ ¢ £ ! £ © £ ! ¥¦¤£¡ £© ¥£ ©¨¡ ...
View
Full
Document
This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.
 Spring '09
 Naver
 Algorithms

Click to edit the document details