Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson October 6,2005 6.046J/18.410J Handout 11 Practice Quiz 1 Solutions
Problem 1. Recurrences Solve the following recurrences by giving tight notation bounds. You do not need to justify your answers, but any justiﬁcation that you provide will help when assigning partial credit.
¦ ' % $ " ¤ ¢ ¦ ¤ ¢ ¡ ¦ ¤ ¢ ¡ ¨ ¦ ¤ ¢ ¡ (a) ¦ ¤ P f I a ¤ ¢ ¨ ¦ ¤ ¢ ¡ Solution: By Case 2 of the Master Method, we have a ¤ ¦ 3 ¤ ¢ ¡ e ¨ ¦ ¤ ¢ ¡ (f) ¦ ¤ a P I ¢ ¨ ¦ ¤ ¢ ¡ Solution: By Case 2 of the Master Method, we have ¤ R P I ¦ G ¤ ¢ ¡ ¨ ¦ ¤ ¢ ¡ (e) ¦ ¤ P I ¤ ¢ ¨ ¦ ¤ ¢ ¡ Solution: By Case 3 of the Master Method, we have ¤ P I ¤ ¦ ¤ ¢ ¡ 3 ¨ ¦ ¤ ¢ ¡ (d) . . ¦ c a Y W % U $ ¤ ¢ ¨ ¦ ¤ ¢ ¡ Solution: By Case 1 of the Master Method, we have ¤ R P I ¦ G ¤ ¢ ¡ ¨ ¦ ¤ ¢ ¡ (c) . . ¦ 3 ¤ ¢ ¤ Solution: Master method does not apply directly. But is much smaller than therefore ignore the lower order term and guess that the answer is Check by substitution. ¨ ¦ ¤ ¢ ¡ ¤ " ¤ ¦ ¤ " ¢ ¡ ¦ 3 ¤ ¢ ¡ ¨ ¦ ¤ ¢ ¡ (b) ¨ ¨ ¦ ¦ ¤ ¤ ¢ ¢ 0 0 Solution: Master method does not apply directly, but we have . Now apply case 3 of master method to get . Therefore, we have . Lower bound is obvious.
( ( ¦ ¦ ¤ ¢ ¤ ¢ ¡ ¡ ¦ ' % $ " ¤ ¢ A ¨ ¦ ¤ ¢ ¡ ¦ ' % $ " ¤ ¢ ¦ ' ¦ % $ " ¤ ¢ ¤ ¢ ¡ 3 , . Handout 11: Practice Quiz 1 Solutions
3 2 Problem 2. True or False Circle T or F for each of the following statements, and brieﬂy explain why. The better your argument, the higher your grade, but be brief. No points will be given even for a correct solution if no justiﬁcation is presented.
¦ ¦ ¤ ¢ $ ¢ ¨ ¦ ¦ ¤ ¢ $ ¢ % ¦ ¤ ¢ $ ¦ ¤ ¢ $ (a) T F For all asymptotically positive , . .
¦ (b) T F The worstcase running time and expected running time are equal to within constant factors for any randomized algorithm. (b) T F The collection of hash functions is universal, where the three of keys into the range hash functions map the universe according to the following table:
I 3 G " G 6 D ¦ Y ¢ a F I W ¦ G Y ¢ U G F R G ¦ P Y ¢ D F a F G Y R F G F D ¨ C ¦ R ¤ ¢ Solution: False. Randomized quicksort has worstcase running time of and expected running time of .
¦ ¤ P I ¤ ¢ I ¦ ¦ ¤ ¢ ¦ $ ¦ ¢ ¤ ¢ A $ ¦ ¢ ¨ ¤ % ¢ ¦ $ ¤ 0 ¢ ¢ ¦ ) ¤ ¢ ¨ ) ¦ ¦ ¤ ¢ $ ¦ @ ¢ ¦ ¤ % is Solution: True. Clearly, , for all for some any . Thus, whence
¤ ¢ $ ¦ ¢ ¤ ¢ $ ¦ ¦ @ ¤ ¤ ¢ $ ¢ ¦ % ¤ ¦ ¤ ¢ ¦ $ ¢ ¤ ¢ A $ ¦ ¦ ¨ ¤ ¦ ¢ ¦ $ ¤ ¢ ¢ 3 $ ¢ ( % ¦ ¤ ¢ ¦ ) ¤ ¢ 6 $ 4 3 . Let . Hence, . For , ¤ P I ¦ " ¢ ¤ ¢ 0 ¨ ¦ ¤ ¢ 0 , where
¦ ¨ ¦ ¤ P I ¢ ¦ ¤ ¢ P I R ¥ ¨ § ' ¤ 3 3 6 6 ¤ ¢ 0 3 P " " ( 6 6 I R R ¦ ¥ ¨ ¤ § ' ¢ ¤ ¡ Solution: . This is . For the upper bound, note that . which is clearly
¦ ¤ P f ¦ I ¤ ¤ ¢ P I ¤ ¢ ¨ A ¦ ¤ ¢ ¡ ¦ ¤ P I ¤ ¢ " " 6 6 P U R W ¤ P I ¦ 3 ¢ ¤ ¢ ¡ ¨ ¦ ¤ ¢ ¡ (h) ¦ a ¤ ¢ ¨ ¦ ¤ ¢ ¡ Solution: By Case 3 of the Master Method, we have a ¤ ¦ ¤ ¢ ¡ ¨ ¦ ¤ ¢ ¡ (g) . , Handout 11: Practice Quiz 1 Solutions
Solution: True. A hash family that maps a universe of keys into slots is , the number of hash functions universal if for each pair of distinct keys for which is exactly . In this problem, and . Therefore, for any pair of the four distinct keys, exactly hash function should make them collide. By consulting the table above, we have:
3 a . 3 Problem 3. Short Answers Give brief, but complete, answers to the following questions. (a) Argue that any comparison based sorting algorithm can be made to be stable, without affecting the running time by more than a constant factor. Solution: To make a comparison based sorting algorithm stable, we just tag all elements with their original positions in the array. Now, if , then we compare and , to decide the position of the elements. This increases the running time at a factor of 2 (at most). (b) Argue that you cannot have a Priority Queue in the comparison model with both the following properties.
¦ ¢ ¨ 2 ¨ ¨ 2 Solution: If such priority queues existed, then we could sort by running BUILD H EAP ( ) ). This algorithm would and then extracting the minimum times ( sort time in the comparison model, which violates the lower bound for comparison based sorting.
¦ ¤ ¢ ¦ ¤ P f I ¤ ¢ ¦ ¤ ¢ ¨ ¦ ¢ ¤ ¤ ¦ ¤ ¢ ¦ ¤ ¢ E XTRACTM IN runs in BUILD H EAP runs in time. time. $ $ $ R R a . . . . . only for only for only for only for only for only for
¦ mapping into slot mapping into slot mapping into slot mapping into slot mapping into slot mapping into slot ¨ ¥ ¡ + ¥ ¡ ¥ ¢ / + ¥ ; + ¦ ¦ ¦ ¦ ¦ ¦ ¢ ¢ 7 7 9 9 4 9 . ¢ ¢ ¢ ¢ ¢ ¢ . . . . . . ¨ ¨ ¨ ¨ ¨ ¨ ¨ ¦ ¦ ; ¦ ¦ ¦ ¦ ¦ ¢ 2 2 4 2 7 4 . ¢ ¢ ¢ ¢ ¢ ¢ . . . . . . + ¨ . ¡ Handout 11: Practice Quiz 1 Solutions
(c) Given a heap in an array with as the maximum key (the heap is a max heap), give pseudocode to implement the following routine, while maintaining the max heap property. D ECREASE K EY – Decrease the value of the key currently at by . Assume . Solution:
¦ G ¢ ¡ ¨ P " ¨ P ¤ " ¨ P ¦ ¡ G ¢ 6 ¡ 4 D ECREASE K EY
¢ ¨ P ¢ ¨ P (d) Given a sorted array of distinct integers, some of which may be negative, give an and provided such an index algorithm to ﬁnd an index such that exists. If there are many such indices, the algorithm can return any one of them. Solution: The key observation is that if and , then . Similarly if and , then . So if we look at the middle element of the array, then half of the array can be eliminated. The algorithm below (I NDEX S EARCH ) is similar to binary search and runs in time. It returns 1 if there is no answer.
¦ © G § G P ¢ ¤ ¨ P ¤ ¨ ¨ P 4 ¨ P ¨ ¨ P ¤ ( ( " ¤ P I NDEX S EARCH if return 1
¡ ¨ R ¡ ¨ P if
P Problem 4. Suppose you are given a complete binary tree of height with leaves, where each node and each leaf of this tree has an associated “value” (an arbitrary real number). the set of ancestors of (including as one of its own ancestors). If is a leaf, we denote by That is, consists of , ’s parent, grandparent, etc. up to the root of the tree.
¢ Y ¦ ¢ G Y ¢ P ¢ Y 3 ¨ ¤ F Y ! Y ¦ Y ¢ Y P Y Y Y Similarly, if is, and are distinct leaves we denote by
" ¦ Y ¢ P ¨ ¦ ¢ G Y ¢ P ¦ ¢ ¢ P ¦ ¦ © G ¡ G § ¡ G G P P ¢ ¢ then return I NDEX S EARCH else return I NDEX S EARCH ¡ 4 ¡ ¨ if ¡ then return ¦ ¤ P f I ¢ ¦ G 4 P ¢ M AX H EAPIFY ¡ ¡ ¦ § ¨ 4 © ¨ ¨ ¢ ¦ P ¡ ¢ P the ancestors of either or . That Handout 11: Practice Quiz 1 Solutions 5 36 21 6 15 30 11 10 10 19 20 14 5 9 2 7 x y A(x,y) shown in bold f(x,y) = 19+15+21+36+20+30 = 141 Solution: There are several different styles of solution to this problem. Since we studied divideandconquer algorithms in class, we just give a divideandconquer solution here. There were also several different quality algorithms, running in , , and . These were worth up to , , and points, respectively. A correct analysis is worth up to points. First, let us look at an solution then show how to make it . For simplicity, the solution given here just ﬁnds the maximum value, but it is not any harder to return the leaves giving this value as well. We deﬁne a recursive function M AX 1 to return the maximum value of —the sum of the to be a ancestors of a single node—over all leaves in ’s subtree. Similarly, we deﬁne M AX 2
¦ ¡ ¢ ¦ ; ¢ ¡ ; ¦ ¡ ¢ ¦ ¤ ¢ A ¦ ¤ P I ¤ ¢ A ¦ ¤ P I R ¤ ¢ A ¦ ¤ P I ¤ ¢ A ¦ ¤ ¢ A ( ¢ ( ; Give an algorithm (pseudocode not necessary) that efﬁciently ﬁnds two leaves is as large as possible. What is the running time of your algorithm?
¦ ( ¢ / ( ; ¢ ¦ ¢ / ; ¢ 2 ¦ ¢ / ; ¢ Deﬁne the function to be the sum of the values of the nodes in . and such that Handout 11: Practice Quiz 1 Solutions
Y 6 in ’s subtree. Calling
¡ ¢ Y First, let us implement M AX 1 . The maximum path can either be in ’s left subtree or ’s right subtree, so we end up with a straightforward divide and conquer algorithm given as: M AX 1 1 return value
¢ ¦ ¡ ¢ ¢ ¦ ¡ ¢ ¡ ¡ ¦ ¡ ¢ For M AX 2 , we note that there are three possible types of solutions: the two leaves are in ’s left subtree, the two leaves are in ’s right subtree, or one leaf is in each subtree. We have the following pseudocode: M AX 2 1 return value
¢ ¢ ¦ ¡ ¢ ¡ ¡ Analysis: For M AX 1, we have the following recurrence
¦ " ¢ § " 3 ¢ ¤ ¦ ¡ 3 ¨ ¦ ¤ ¢ ¡ (1)
¦ by applying the Master Method. For M AX 2, we have
¦ " ¢ § " 3 ¢ ¤ ¦ ¡ 3 § " 3 ¢ ¤ ¦ ¡ 3 ¨ ¦ ¤ ¢ ¡ (2)
¦ by case 2 of the Master Method. To get an solution, we just deﬁne a single function, M AX B OTH , that returns a pair—the answer to M AX 1 and the answer to M AX 2. With this simple change, the recurrence is the same as M AX 1 Problem 5. Sorting small multisets For this problem is an array of length objects that has at most distinct keys in it, where . Our goal is to sort this array in time faster than . We will do so in two phases. In the ﬁrst phase, we will compute a sorted array that contains the distinct keys occuring in . In the second phase we will sort the array using the array to help us.
P ¨ ¨ ¦ ¤ P f I R ¤ ¢ R ¤ P P ¤ " ¤ ¨ ¦ ¤ ¢ A ¦ ¦ ¡ ¨ ¢ ¦ ¡ ¨ ¢ G ¦ ¡ ¨ ¦ ¤ ¢ ¢ § ¤ " ¢ G 3 ¢ ¦ ¤ ¡ ¤ P ¨ M AX 2 left
¢ D M AX 2 right M AX 1 left M AX 1 right I ¦ ¦ ¡ ¨ ¢ G ¦ ¡ ¨ M AX 1 left
¢ D M AX 1 right G function returning the maximum value of over all pairs of leaves M AX 2 on the root will return the answer to the problem.
¦ ¢ G ¢ $ I I ¦ ¨ ¤ R R ¢ ¡ 3 ¨ ¨ ¤ ¤ ¢ ¢ R ¦ ¦ ¡ ¡ Handout 11: Practice Quiz 1 Solutions
Note that might be very small, like a constant, and your running time should depend on as . The objects have satellite data in addition to the keys.
¨ ¤ ¨ ¤ 7 as well Your goal is to design and analyse efﬁcient algorithms and analyses for the two phases. Remember, the more efﬁcient your solutions, the better your grade! (a) Design an algorithm for the ﬁrst phase, that is computing the sorted array of length containing the distinct keys. The value of is not provided as input to the algorithm. Solution: The algorithm adds (nonduplicate) elements to array while maintaining sorted at every intermediate stage. For , element is binary searched in occurs in , then it need not be inserted. Otherwise, binary search array . If also provides the location where should be inserted into array to maintain in sorted order. All elements in to the right of this position are shifted by one place to . make place for (b) Analyse your algorithm for part (a). Solution: Binary search in array for each element of array takes time since size of is at most . This takes a total of time. Also, a new element is inserted into array exactly times, and the total time over all such insertions is . Thus, the total time for the algorithm is since . (c) Design an algorithm for the second phase, that is, sorting the given array , using the array that you created in part (a). Note that since the objects have satellite data, it is not sufﬁcient to count the number of elements with a given key and duplicate them. Hint: Adapt Counting Sort. Solution: containing the number of elements in Build the array as in counting sort, with that have values less than or equal to . Counting sort will not work as is since
¨ 7 ¨ 4 7 2 2 4 ¦ ¨ 3 P I ¤ ¢ ¢ A A ¨ ¦ R ¨ ¦ ¨ ¨ P P I I ¢ ¤ ¢ A A 2 ¦ ¨ P I ¤ ¢ A 4 ¨ ¦ R ¨ ¨ ¢ ¤ A " 4 ¨ ¤ ¦ ¨ ¨ 4 4 4 4 ¨ 2 4 ¤ / / 3 / ¨ ¨ 2 4 4 ¨ 2 ¨ 2 4 ¨ 4 ¨ ¨ ( $ / ( $ / ( $ / G / G / ¢ / ¢ / ¢ / ¨ ¤ ¦ R R / ¤ ¨ ¦ R R The output after the second phase should be ( $ / G / ¢ / ¤ ¨ ¦ R R ¨ 4 In the ﬁrst phase we compute . . ¨ ¨ $ ¨ ¤ ¨ ¤ ¦ R R / ¢ / ( $ / G / ¢ / ( $ / ¨ ¤ ¦ R R / ¢ / ( $ / G ¨ 2 Example: Let . Then and . Handout 11: Practice Quiz 1 Solutions
is necessarily an integer. Or, it may be some integer of very large value (there is is an invalid index into our array . no restriction on our input range). Therefore What we would like to do is assign an integral “label” for the value . The label we in the array calculated in the last part of the choose is the index of the value problem. How do we ﬁnd this index? We could search through from beginning to end, looking for the value , then returning the index of that contains . This would take time. But, since is already sorted, we can use B INARYS EARCH to speed this up to . Let B INARYS EARCH be a procedure that takes a sorted array and an item within the array, and returns such that . The modiﬁed version of C OUNTING S ORT is included below, with modiﬁed lines in bold:
8 /* Construct Sorted List AOut */ ;
¦ (d) Analyse your algorithm for part (c). Solution: The running time of the modiﬁcation to C OUNTING S ORT we described can be broken down as follows: First Loop: . Second Loop: iterations, each iteration performing a B INARYS EARCH on an array of size . Total Work: . Third Loop: . Fourth Loop: iterations, each iteration performing a B INARYS EARCH on an array of size . Total Work: .
¦ ¦ ¨ ¨ P P f f I I ¤ ¤ ¢ ¢ A A ¦ ¦ ¤ ¤ ¦ ¢ ¢ ¦ ¨ ¨ ¨ ¢ A ¨ A ¢ A A ¤ ¦ " ¨ ¨ ¨ C OUNTING S ORT /* Uses Arrays , , and out For to do ; to do For B INARYS EARCH Location C[Location] C[Location] ; ; For to do ; downto do For Location B INARYS EARCH OutLocation D[Location]; D[Location] D[Location] ; out[OutLocation] ; Output( out);
P P G G R R ¢ ¢ " P " ¢ ¨ P ¨ ¨ ¢ " U ¨ 6 W ¢ " ¦ ¨ ¢ ¨ P ¢ ¢ ¢ ¢ " " U ¨ ¨ ¢ ¢ U W ¨ ¨ ¤ " ¢ ¨ U 3 " " ¤ ¨ P ¨ W P ¨ ¨ ¨ ¢ " ¨ W */ /* Initialize */ /* Count number of elements */ ; /* Build cumulative counts */ 0 U ¨ P ¨ P Y ¨ ¨ 0 R R ¨ R P ¦ Y G 0 ¢ ¨ P R ¨ P ¦ Y ¨ P f I ¢ A ¦ ¨ ¢ ¨ A P Handout 11: Practice Quiz 1 Solutions
The running time is dominated by the second and fourth loops, so the total running time is .
¦ ¨ P f I ¤ ¢ A 9 ...
View
Full
Document
This note was uploaded on 11/03/2009 for the course CS 6.033 taught by Professor S during the Winter '09 term at MIT.
 Winter '09
 s
 Algorithms

Click to edit the document details