12.07.2015 Views

Practice Quiz 1 Solutions

Practice Quiz 1 Solutions

Practice Quiz 1 Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Handout 11: <strong>Practice</strong> <strong>Quiz</strong> 1 <strong>Solutions</strong> 3Solution: True. A hash family +that maps a universe of keys into ¡ slots isuniversal if for each pair of distinct keys ; / ¢ , the number of hash functions. + for which . ¢ ; ¦ ¨ . ¢ ¢ ¦ is exactly ¥+ ¥ ¡ . In this problem, ¥ + ¥ ¨ and¡ ¨ . Therefore, for any pair of the four distinct keys, exactly hash functionshould make them collide. By consulting the table above, we have:¢ 2 ¦ ¨ . ¢ 4 ¦ only for . =mapping into slot !.¢ 2 ¦ ¨ . ¢ 7 ¦ only for .4mapping into slot $.¢ ¦ . 9 . . 2 ¨ ¢ ¦. 4 ¢ ¦ ¨ . ¢ 7 ¦ . $only for mapping into slotonly for mapping into slot¢ 4 ¦ ¨ . ¢ 9 ¦ only for .4mapping into slot .¢ 7 ¦ ¨ . ¢ 9 ¦ only for . =mapping into slot $.Problem -3. Short AnswersGive brief, but complete, answers to the following questions.(a) Argue that any comparison based sorting algorithm can be made to be stable, withoutaffecting the running time by more than a constant factor.Solution:To make a comparison based sorting algorithm stable, we just tag allelements with their original positions in the array. Now, if 2 ¨ ¨compare and , to decide the position of the elements. This increases the runningtime at a factor of 2 (at most).2 ¨ ,thenwe(b) Argue that you cannot have a Priority Queue in the comparison model with both thefollowing properties.EXTRACT-MIN runs in ¢ ¦ time.BUILD-HEAP runs in¢ ¤ ¦ time.Solution:If such priority queues existed, then we could sort by running BUILD-HEAP ¢ ¤ ¦ ( )and then extracting the ¤ minimum (¤ ¢ ¦ ¨ ¢ ¤ ¦ times ). This algorithm would¢ ¤ ¦ sort time in the comparison model, which violates ¢ ¤ 1 B 2 ¤ ¦ the lower boundfor comparison based sorting.


4 /¡¦Handout 11: <strong>Practice</strong> <strong>Quiz</strong> 1 <strong>Solutions</strong> 4(c) Given a heap in an array 2 ¨ ¤ with 2 ¨ as the maximum key (the heap is a maxheap), give pseudo-code to implement the following routine, while maintaining themax heap property. /¡¦ – Decrease the value of the key currently at 2 ¨ by¡. AssumeDECREASE-KEY¢. $¡ Solution:DECREASE-KEY¢¨ ¢2 ¨ ¡22 / ¦MAX-HEAPIFY¢(d) Given a sorted 2 array ¤ of distinct integers, some of which may be negative, give analgorithm to find an index such that ¤ and 2 ¨ ¨ provided such an indexexists. If there are many such indices, the algorithm can return any one of them.Solution:The key observation is that 2 ¨ " if 2 ¨ ¨ and2 ¨ ¨ and ,then " . So if we look at the middle element of the array, then half,then ¤ . Similarly if 2 ¨ ¤ of the array can be eliminated. The algorithm below (INDEX-SEARCH) is similar tobinary search and runs in¢ 1 B 2 ¤ ¦ time. It returns -1 if there is no answer.2 /§ /© ¦INDEX-SEARCH¢if " § ¦return -1¢©¨ ¡if 2 ¨ ¡ ¨ ¡then ¡ return2 ¨ ¡ " ¡ ifthen INDEX-SEARCH¢ 2 return / ¡ ¦else return INDEX-SEARCH¢ 2 / ¡ /© ¦/§Problem -4.each node and each leaf of this tree has an “value” associated (an arbitrary real number).Suppose you are given a complete binary tree of height . with ¤ ¨! leaves, whereIf is a leaf, ¢ ; ¦ we denote by the set of ancestors of (including as one of its own ancestors).2 ; ; ; ; That is, consists of , ’s parent, grandparent, etc. up to the root of the tree.2 ¢ ¦Similarly, ; if and are distinct leaves we ; ¢ ; / ¢ ¦ denote by the ancestors of either ¢ or . That2 ¢is,; ;2 ¢ ; / ¢ ¦ ¨ 2 ¢ ; ¦ 2 ¢ ¢ ¦


Handout 11: <strong>Practice</strong> <strong>Quiz</strong> 1 <strong>Solutions</strong> 53621 615 30111010 19 20 14 5 9 2x yA(x,y) shown in bold7f(x,y) = 19+15+21+36+20+30 = 141Define the ¢ ; / ¢¦ function to be the sum of the values of the nodes 2 ¢ ; / ¢¦ in .Give an algorithm (pseudo-code not necessary) that efficiently finds two leaves ( and ¢ ;( ¦ is as large as possible. What is the running time of your algorithm?( such that ¢ ; ( / ¢Solution:There are several different styles of solution to this problem. Since we studied divide-and-conqueralgorithms in class, we just give a divide-and-conquer solution here. There were also severaldifferent quality algorithms, running ) ¢ ¤ ¦ in ) ¢ ¤ 1 2 ¤ ¦ ,and) ¢ ¤ 4 1 2 ¤ ¦ , . These were worth up to , ,and points, respectively. A correct analysis is worth up to points.First, let us look at ) ¢ ¤ 1 2 ¤ ¦ an solution then show how to make ) ¢ ¤ ¦ it . For simplicity, thesolution given here just finds the maximum value, but it is not any harder to return the leavesgiving this value as well.We define a recursive MAX1¢ ¡ ¦ function to return the maximum value ¢ ; ¦ of —the sum of theancestors of a single node—over all leaves in ¡ ’s subtree. Similarly, we define MAX2¢ ¡ ¦ to be a;


¡ ¦ MAX1¢1 ¢ value¢ ¡ ¦ return¡ ¦ MAX2¢1 ¢ value¢ ¡ ¦ return¡ 4 ¢ ¤ ¦ ¨ ! ¡ 41¦1¦Handout 11: <strong>Practice</strong> <strong>Quiz</strong> 1 <strong>Solutions</strong> 6function returning the maximum value of ¢ ; / ¢¦ over all pairs of leaves ; / ¢in ¡ ’s subtree. CallingMAX2 on the root will return the answer to the problem.First, let us MAX1¢ ¡ ¦ implement . The maximum path can either be ¡ in ’s left subtree ¡ or ’s rightsubtree, so we end up with a straightforward divide and conquer algorithm given as:MAX2¢ ¡ ¦ For , we note that there are three possible types of solutions: the two leaves are ¡ in ’s leftsubtree, the two leaves are ¡ in ’s right subtree, or one leaf is in each subtree. We have the followingpseudocode: ¢ ¤ , MAX1¢ left¨ ¡ ¦ / MAX1¢ right¨ ¡ ¦Analysis:For MAX1, we have the following recurrence ¢ ¤ , MAX2¢ left¨ ¡ ¦ / MAX2¢ right¨ ¡ ¦ / MAX1¢ left¨ ¡ ¦ MAX1¢ right¨ ¡ ¦ ¢ ¤ ¦ ¨ ! ¡ ¦ ¤ ! § ¢ ¦¡¢ ¤ ¦ (1)¨by applying the Master Method.For MAX2, we have! § ¢ ¦¦ ¤ ! § ! ¡ ¦ ¤ ! ¡ 4¦ ¤ ¨¢ ¤ 1 2 ¤ ¦ (2)¨by case 2 of the Master Method.To get ) ¢ ¤ ¦ an solution, we just define a single function, MAXBOTH, that returns a pair—theanswer to MAX1 and the answer to MAX2. With this simple change, the recurrence is the same asMAX1! § ¢ ¤ ¦Problem -5.Sorting small multisetsFor this 2 problem is an array of ¤ length objects that has most¨ at distinct keys in it, where¤ ¤ . Our goal is to sort this array in time faster than ¢ ¤ 1 B 2 ¤ ¦ . We will do so in two phases.In the first phase, we will compute a sorted array 4 that contains the¨ distinct keys occuring in 2 .In the second phase we will sort the array¨using the array 4 to help us.2


¤Handout 11: <strong>Practice</strong> <strong>Quiz</strong> 1 <strong>Solutions</strong> 7Note that ¨ might be very small, like a constant, and your running time should depend on ¨ as wellas ¤ . The ¤ objects have satellite data in addition to the keys.Example: Let 2 ¨ / / $( ¢ / /In the first phase 4 ¨we computeThe output after the second phase should be ( / ¢ / 4 ¤4 ¦ ¨/ $ ( / ¢ / / / $¤ 4¤ / ¢ / / / $ 4¤ 44 ¦ ¨( /4 ¦ ¨ . Then ¤ ¨ $ and ¨ ¨ .Your goal is to design and analyse efficient algorithms and analyses for the two phases. Remember,the more efficient your solutions, the better your grade!4 ¦ ¨.4 ¤4 ¦ ¨/ ¢ / ¢ / ¢ / / / / / $ ( / $ ( / $ (.(a) Design an algorithm for the first phase, that is computing the sorted array 4 of length ¨containing the ¨ distinct keys. The value of ¨ is not provided as input to the algorithm.Solution:The algorithm adds (non-duplicate) elements to array 4 while maintaining 4 sortedat every intermediate stage. For ¨ / ! / / ¤ ,element2 is binary searched inarray 4 .If2 occurs in 4 , then it need not be inserted. Otherwise, binary searchalso provides the location where 2 should be inserted into array 4 to maintain 4 insorted order. All elements in 4 to the right of this position are shifted by one place tomake place for 2 .(b) Analyse your algorithm for part (a).Solution:Binary search in array 4 for each element of array 2 takes ) ¢ 1 2 ¨time since size of ¦is at most ¨ . This takes a total of ) ¢ ¤ 1 2 ¨ ¦ time. Also, a new element is inserted4into 4 array ¨ exactly times, and the total time over all such insertions ) ¢ ! is ¨ ¦ ¨ ) ¢ ¨ 4 ¦ . Thus, the total time for the algorithm is ) ¢ ¤ 1 2 ¨ ¨ 4 ¦ ¨ ) ¢ ¤ 1 2 ¨ ¦¨ ¤ since .(c) Design an algorithm for the second phase, that is, sorting the given 2 array ,usingthe4 array that you created in part (a). Note that since the objects have satellite data, itis not sufficient to count the number of elements with a given key and duplicate them.Hint: Adapt Counting Sort.Solution:Build the array 7 as in counting sort, with 7 containing the number of elements in2 that have values less than or equal to 4 . Counting sort will not work as is since


Handout 11: <strong>Practice</strong> <strong>Quiz</strong> 1 <strong>Solutions</strong> 9The running time is dominated by the second and fourth loops, so the total runningtime is ) 1 B 2 ¨ .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!