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 Erik Demaine and Shafi Goldwasser October 7, 2002 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. For this quiz, you need not provide rigorous proofs of correctness. Instead, give informal arguments for why you believe your algorithms are correct. Pseudocode is only required when explicitly indicated, but you may include it if it clarifies your answers. When the quiz begins, write your name on every page of this quiz booklet. The quiz contains 5 multi-part problems. You have 80 minutes to earn 80 points. This quiz booklet contains 11 pages, including this one. Two extra sheets of scratch paper are attached. Please detach them before turning in your quiz. This quiz is closed book. You may use one handwritten A4 or 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 correctness of your answer, but also on the clarity with which you express it. Be neat. Good luck! Problem Points Grade 1 2 3 4 5 Total Name: Solutions Circle the name of your recitation instructor: Moses Jen Steve 12 12 30 13 13 80 Initials & # # B r q p i g A %$YyxRwd vutsp GpB I d hY b V B D Ed cG b V a E` V V U (c) (b) (a) Solution: We solve this recurrence by Akra-Bazzi, with fe Y%$XW & #V U R&# TE2ER$# DBS& Q451 P451 I I G451 HG FEC D DB Solve the following recurrences. Give tight, i.e. 6.046J/18.410J Quiz 1 Problem 1. Recurrences [12 points] Solution: We use the Master Theorem: note that . ) ' & 0 ( %$# Solution: First, notice that sides). Since . Name (this can be seen by taking of both , we use case (ii) of the Master Theorem: , bounds. . Then we get R&# 975 1 A@86432 , so the solution is 2 " ! 6.046J/18.410J Quiz 1 Name 3 Problem 2. Short Answer [12 points] Give brief, but complete, answers to the following questions. (a) Briefly describe the difference between a deterministic and a randomized algorithm, and name two examples of algorithms that are not deterministic. Solution: On identical inputs, a deterministic algorithm always performs exactly the same computations and returns the same output. A randomized algorithm is one which "flips coins," i.e. one which makes random choices that may cause it to perform different computations, even on the same input. R ANDOMIZED -Q UICKSORT and R ANDOMIZED -S ELECT are two examples of non-deterministic algorithms. (b) Describe the difference between average-case and worst-case analysis of deterministic algorithms, and give an example of a deterministic algorithm whose average-case running time is different from its worst-case running time. Solution: An average-case analysis assumes some distribution over the inputs (e.g., uniform), and computes the expected (average) running time of an algorithm subject to that distribution. A worst-case analysis considers those inputs which force an algorithm to run for the longest amount of time, and computes the running time under those inputs. Q UICKSORT has a worst-case running time of (on an alreadysorted or reverse-sorted array), but has an average-case running time of (assuming all input permutations are equally likely). (c) If you can multiply 4-by-4 matrices using 48 scalar multiplications, can you multiply matrices asymptotically faster than Strassen's algorithm (which runs in time)? Explain your answer. Solution: Our algorithm breaks an matrix into a 4-block by 4-block matrix (where each block is ). It then multiplies the appropriate blocks using 48 recursive calls (corresponding to the scalar multiplications) and combines their products. Our new algorithm's running time is , which is by the Master Theorem. For comparison with Strassen's algorithm, note that . Therefore our algorithm is asymptotically better. 451 ( & # R$b " HG b D D D G P D D S R$# & 7 $ &ER# R# & $ 5 Q S 6431 ( 6.046J/18.410J Quiz 1 Name 4 Problem 3. True or False, and Justify [30 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 Every comparison-based sort uses at most comparisons in the worst case. comSolution: False. I NSERTION -S ORT , for example, uses parisons in the worst-case (a reverse-sorted array). The statement would be true if it read ". . . at least comparisons in the worst case." (b) T F R ADIX -S ORT is stable if its auxiliary sorting routine is stable. Solution: True. If two numbers are equal, then they have the same digits. Each intermediate sort is stable, so the two equal numbers never change relative positions. (c) T F It is possible to compute the smallest elements of an -element array, in time. sorted order, in Solution: True. We can S ELECT the th smallest element and partition around it, then sort those elements in time. Alternately, we can build a minheap in time and call E XTRACT-M IN times, for a total runtime of . Some incorrect solutions amounted to: "we must do order statistic queries, each of which take time, for a total running time of ." However, this argument does not preclude us from coming up with a more clever algorithm (like the one above) that is more efficient. In fact, a similar argument would "prove" that sorting must take time (despite the existence of M ERGESORT etc.), because we must do order statistic queries! %$( b & # ! b ! tR$b & # ! ! b ! b Y%$ & # ( b ! b Y &ER$ 0 ( # ! b 6.046J/18.410J Quiz 1 Name 5 (d) T F Consider hashing the universe , , into the hash table . Consider the family of hash functions , where is the th bit of the binary representation of . Then is universal. Solution: False. Take . Then all of 's binary digits are the same as 's, except for the least significant one. Thus . If were universal, the probability would be at most . Some incorrect solutions said that the probability of a collision, taken over random choices of , , and is 1/2. This is true, but the universality condition demands something stronger: it says that for every fixed (distinct) pair , their probability of collision over the random choice of must be at most 1/2. (e) T F R ANDOMIZED -S ELECT can be forced to run in time by choosing a bad input array. Solution: False. R ANDOMIZED -S ELECT runs in expected time; the only way it can take longer is if its random choices of pivots are unlucky. The input array cannot force these unlucky choices. and , either or (f) T F For every two functions . Solution: False. Let and ; then neither case holds. Another example is and . Finally, one could let and be any strictly-negative functions; by a technical condition of the definition, must be at least 0 to be . Many incorrect answers argued that one of the statements , , or must be true. This is correct for any particular value of , but it doesn't mean that the same statement is true for all sufficiently large values of , which is the condition needed in the definition of big- . ' dWE Ed b tD W1ec tD ! 2bp6" 8 4 2 0 ( B9 X@A %9 $u2pb 867531)' p D &%Cp 8 WFE b h ' Y%$ & # b 5IfWE W1d Dd b E yWaE( b D WE I `YXWV bE U ! D H R sT$ S WE RwQI D PH D A p bWE b &%$ % D p p D 8 b G( # % 6.046J/18.410J Quiz 1 Name 6 (g) T F Suppose that we have a hash table with slots, with collisions resolved by chaining, and suppose that keys are inserted into the table. Each key is equally likely to be hashed into each slot (simple uniform hashing). Then the expected number of keys for each slot is . Solution: True. Define (for ) to be the indicator which is 1 if element hashes to slot , and 0 otherwise. Then . Then the expected number of elements in slot is by linearity of expectation. Solution: False. See that max-heap property. (i) T F Suppose we use H EAPSORT instead of I NSERTION -S ORT as a subroutine of B UCKET-S ORT to sort elements. Then B UCKET-S ORT still runs in averagecase linear time, but its worst-case running time is now . Solution: True. Even if all the elements land in the same bucket (the worst-case input), H EAPSORT sorts them in time. & # R$b 0d YU 6 U 6 @ @ D G E0TU D %$( & # (h) T F The following array is a max-heap: , which is a violation of the 1 6 I W@ @ G @ )' 6( I 6 & f @ 6 # D G G # D P G D 6.046J/18.410J Quiz 1 Name 7 (j) T F If memory is limited, one would prefer to sort using H EAPSORT instead of M ERGESORT . Solution: True. M ERGESORT is not in-place, which means it requires an auxiliary array as big as the input. H EAPSORT is in-place, which means it only uses auxiliary space. ( 6.046J/18.410J Quiz 1 Name 8 Problem 4. Perfect Powers [13 points] You have just discovered a breakthrough result in number theory that will quickly become worldfamous, but there is one step left for you to complete. You need to find a fast algorithm to tell whether a number is a perfect power. That is, you want a fast algorithm which, on an input integer that is bits long, finds whether there exist integers and such that . If so, your algorithm should output the values of and . (a) If is a perfect power, how large can be? Express your answer as a function of . Solution: (First, some trivia: the scenario from this problem is entirely real! The ground-breaking deterministic primality-testing algorithm by Agrawal et al, discovered with great fanfare this past summer, performs the perfect-power test as one of its first steps.) [3 points] Note that , and since , . A common error was to write only , which is insufficient because we asked for as a function of , and also because the value of (if any) is not known in advance. (b) Suppose that your computer had an -time operation ROOT that returns the th root of an integer , if that root is an integer (and returns otherwise). Give an algorithm to solve the perfect-power problem and analyze its running time. Solution: [5 points] We simply test all values of up to : for , if ROOT where is an integer, then return the pair . If no such root is . an integer, return . The running time of this procedure is Some solutions looped over all possible values of both and , and only returned if ROOT . This solution is wasteful, because ROOT returns the proper base, so there is no need to guess it in advance. This solution is also too inefficient, because could be as large as , so an exponential number of iterations would be peformed. y d @ "b I 451 b %&# I %&# ! @ @ 6.046J/18.410J Quiz 1 Name 9 (c) In reality, there is no such -time ROOT procedure. Still it is possible to solve the perfect power problem in by using a suitable algorithm for ROOT . Describe such an algorithm and analyze its running time. You may assume that multiplying two integers takes time (no matter how large they are). Solution: [5 points] We will implement a ROOT procedure that runs in time when used in the above algorithm, so that we solve the perfect power problem in time. On input , our ROOT procedure performs a binary search for the th root of in the range . When testing the midpoint of the range, if then we recursively search the upper half of the range; if then we search the lower half; if then we return as the th root of . In our usage of ROOT from the previous part, and . Computing each takes mulitiplications by the repeated-squaring technique from class, and the binary search does iterations, each with one exponentiation, for the claimed runtime of . Many solutions did a brute-force search for , but this is too inefficient. In the worst case, , so all values of from 1 to would have to be checked, which is an exponential number of tests. All exponential-time algorithms received no points for this part. Other incorrect solutions made mathematical errors: assuming that the th root of must be smaller than , or returning by repeatedly dividing by or multiplying by itself (this treats as the base, instead of its proper role as the exponent). ' b I T ! ! & # R$b R$b# & I b %$# & @e x 6 ( u&ER$# ( & R$b# ER$# & )%$#( & YR$# b & ER$( & # 6.046J/18.410J Quiz 1 Name 10 Problem 5. Assigning Grades [13 points] It is the not-too-distant-future, and you are a computer science professor at a prestigious northeastern technical institute. After teaching your course, "6.66: Algorithms from Hell," you have to assign a letter grade to each student based on his or her unique total score. (Scores can only be compared to each other.) You are grading on a curve, and there are a total of different grades possible. You want to rearrange the students into equal-sized groups, such that everybody in the top group has a higher score than everybody in the second group, etc. However, you don't care how the students are ordered within each group (because they will all receive the same grade). (a) Describe and analyze a simple algorithm that takes an unsorted -element array of scores and an integer , and divides into equal-sized groups, as described above. Your algorithm should run in time . (If you find a faster algorithm, see part (c).) You may assume that is divisible by . Note: is an input to the algorithm, not a fixed constant. Solution: [5 points] Our algorithm first uses S ELECT to find the th order statistic, then partitions around it. At this point, the first elements of the array form the bottom group. Then it uses S ELECT to find the th order statistic of the remainder of the array, and partitions around it, etc., until all the groups have been separated. Each S ELECT and PARTITION requires linear time in the number of remaining elements, which is at most , so the running time is . b ( 6.046J/18.410J Quiz 1 Name 11 (b) In the case that , prove that any algorithm to solve this problem must run in time in the worst case. Recall that we are only considering comparison-based algorithms, i.e., algorithms that only compare scores to each other as a way of finding information about the input. Hint: There is a very short proof. Solution: [3 points] Any algorithm for this problem can fully sort an array of elements if we provide it with an input where . Since sorting requires comparisons in the worst case, the algorithm must run in time in the worst case. (c) Now describe and analyze an algorithm for this problem that runs in time . You may also assume that is a power of , in addition to assuming that is divisible by . Solution: [5 points] We use a recursive algorithm G ROUP , which takes an array and a value , and works as follows: if , return. Otherwise, S ELECT and PARTITION around the median of the array. Then call G ROUP on the lower half of the array with , and again on the top half with . To see that this works, note that after partitioning, all grades in the upper half of the array are greater than those in the lower half. By induction, the two recursive calls divide each half into groups, for a total of groups. Finally, note that the base case satisfies the problem statement. We now analyze the running time: the recurrence describing the algorithm's running time is because S ELECT and PARTITION are lineartime. The base case of the recurrence is for any . Therefore the recurrence tree does work at each level, and has levels, for a total running time of . Some students correctly observed that this solution is essentially an "early quitting" Q UICKSORT , where the pivot is always chosen to be the median, and the algorithm terminates once the recursion depth reaches . %& YR&b # # # u%& R$b & # R$b & # R&# b B E P u B R&# R$b & # E SCRATCH PAPER -- Please detach this page before handing in your quiz. SCRATCH PAPER -- Please detach this page before handing in your quiz. ...
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.

Ask a homework question - tutors are online