Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Similar documents
Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Introduction to Algorithms March 10, 2010 Massachusetts Institute of Technology Spring 2010 Professors Piotr Indyk and David Karger Quiz 1

CS361 Homework #3 Solutions

CS173 Running Time and Big-O. Tandy Warnow

CSCI Honor seminar in algorithms Homework 2 Solution

Lecture 2: Divide and conquer and Dynamic programming

CPSC 320 Sample Final Examination December 2013

Assignment 5: Solutions

CS383, Algorithms Spring 2009 HW1 Solutions

An analogy from Calculus: limits

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Introduction to Divide and Conquer

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Algebra Exam. Solutions and Grading Guide

Homework 1 Submission

Algorithm efficiency analysis

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

CS 161 Summer 2009 Homework #2 Sample Solutions

CS Data Structures and Algorithm Analysis

Proof Techniques (Review of Math 271)

1 Substitution method

Fall 2016 Test 1 with Solutions

Review Of Topics. Review: Induction

University of California Berkeley CS170: Efficient Algorithms and Intractable Problems November 19, 2001 Professor Luca Trevisan. Midterm 2 Solutions

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

1 Maintaining a Dictionary

Lecture 18 April 26, 2012

CSCB63 Winter Week10 - Lecture 2 - Hashing. Anna Bretscher. March 21, / 30

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

INF2220: algorithms and data structures Series 1

data structures and algorithms lecture 2

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CS325: Analysis of Algorithms, Fall Midterm

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

Week 5: Quicksort, Lower bound, Greedy

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Module 1: Analyzing the Efficiency of Algorithms

Computational Complexity - Pseudocode and Recursions

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

1 Closest Pair of Points on the Plane

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Methods for solving recurrences

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Introduction to Hash Tables

Priority queues implemented via heaps

(Refer Slide Time: 0:21)

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Lecture 10: Powers of Matrices, Difference Equations

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

How many hours would you estimate that you spent on this assignment?

Midterm 1 for CS 170

CSCI 3110 Assignment 6 Solutions

Algebra. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Design and Analysis of Algorithms March 12, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 1

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

Divide-and-Conquer Algorithms Part Two

Divide-and-conquer: Order Statistics. Curs: Fall 2017

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Dynamic Programming( Weighted Interval Scheduling)

Mergesort and Recurrences (CLRS 2.3, 4.4)

NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

Problem Set 1 Solutions

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

Lecture 8 HASHING!!!!!

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

CS1800: Mathematical Induction. Professor Kevin Gold

Linear Selection and Linear Sorting

Problem Set 2 Solutions

Lecture 3. Big-O notation, more recurrences!!

Math 391: Midterm 1.0 Spring 2016

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

COMP 382: Reasoning about algorithms

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

1 Caveats of Parallel Algorithms

Homework 1 Solutions

15-451/651: Design & Analysis of Algorithms October 31, 2013 Lecture #20 last changed: October 31, 2013

Lecture 6 September 21, 2016

Chapter 1 Review of Equations and Inequalities

Topic 17. Analysis of Algorithms

N/4 + N/2 + N = 2N 2.

15.1 Introduction to Lower Bounds Proofs

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

1 Reductions and Expressiveness

AN ALGEBRA PRIMER WITH A VIEW TOWARD CURVES OVER FINITE FIELDS

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Selection and Adversary Arguments. COMP 215 Lecture 19

Transcription:

Introduction to Algorithms October 13, 2010 Massachusetts Institute of Technology 6.006 Fall 2010 Professors Konstantinos Daskalakis and Patrick Jaillet Quiz 1 Solutions Quiz 1 Solutions Problem 1. We hope you mastered this question. Your name is that thing you include at the top of your problem set when you submit. Found yourself tongue-tied? This question is bound to show up on future quizes, so feel free to put it on your crib sheet for free points. Not that two points is enough to really dominate the quiz, but it s a start. Problem 2. Asymptotics & Recurrences [20 points] (3 parts) (a) [10 points] Rank the following functions by increasing order of growth. That is, find any arrangement g 1, g 2, g 3, g 4, g 5, g 6, g 7, g 8 of the functions satisfying g 1 = O(g 2 ), g 2 = O(g 3 ), g 3 = O(g 4 ), g 4 = O(g 5 ), g 5 = O(g 6 ), g 6 = O(g 7 ), g 7 = O(g 8 ). f 1 (n) = n log 2 n f 2 (n) = n + ( ) n n log 4 n f 3 (n) = 4 ( ) n f 5 (n) = f 6 (n) = 2 log2 n f 7 (n) = n log n n 2 ( ) n f 4 (n) = n/3 ( ) n f 8 (n) = n 3 2 Solution: f 2, f 1, f 5, f 3, f 8, f 7, f 6, f 4 (b) [5 points] Find an asymptotic solution of the following functional recurrence. Express your answer using Θ-notation, and give a brief justification. T (n) = 16 T (n/4) + n 2 log 3 n (c) Solution: Using Master Theorem, we compare n 2 log 3 n with n log 4 16 = n 2. This is case 2 of the generalized version of the theorem as treated in class, so we increment the log k n for Θ(n 2 log 4 n). [5 points] Find an asymptotic solution of the following recurrence. Express your answer using Θ-notation, and give a brief justification. (Note that n 1 log n = 1.) T (n) = T ( n) + 1 Solution: T (n) = Θ(log log n). To see this, note that... n = n 1/2i. So, once i becomes log log n we will have }{{} i times n 1/2i = n 1/ log n = 1. Thus the recursion stops after log log n levels and each level contributes 1, hence T (n) = Θ(log log n).

6.006 Quiz 1 Solutions Name 2 Problem 3. True/False [18 points] (9 parts) Circle (T)rue or (F)alse. You don t need to justify your choice. (a) T F [2 points] Inserting into an AVL tree can take o(log n) time. Solution: False. To answer this question, we need to know the length of the shortest possible path from the root to a leaf node in an AVL tree with n elements. In the best possible case, for each node we pass, the heights of its two children differ by 1, and we move to the child with the lower height. The child s height is then 2 less than the current node s height. So in the best case, each time we move to a new node, the height decreases by 2. The number of times we do this to get to height 0 is then the height of the root divided by 2. The height of the root is Θ(log n), so it takes 1/2 Θ(log n) = Θ(log n) time to insert into an AVL tree, in the best case. Therefore it cannot take o(log n) time. (b) T F [2 points] If you know the numbers stored in a BST and you know the structure of the tree, you can determine the value stored in each node. Solution: True. You can do an inorder walk of the tree, which would order the nodes from smallest key to largest key. You can then match them with the values. (c) T F [2 points] In max-heaps, the operations insert, max-heapify, find-max, and findmin all take O(log n) time. Solution: False. The minimum can be any of the nodes without children. There are n/2 such nodes, so it would take Θ(n) time to find it in the worst case. (d) T F [2 points] When you double the size of a hash table, you can keep using the same hash function. Solution: False. If you double the size of the table, you need to change the hash function so that it maps keys to {0, 2m-1} instead of mapping them to {0, m-1}. However, some people answered True, but explained that this would be inefficient. This answer also received full credit.

6.006 Quiz 1 Solutions Name 3 (e) T F [2 points] We can sort 7 numbers with 10 comparisons. Solution: False. To sort 7 numbers, the binary tree must have 7! = 5040 leaves. The number of leaves of a complete binary tree of height 10 is 2 10 = 1024. This is not enough. (f) T F [2 points] Merge sort can be implemented to be stable. Solution: True. Whether it is stable or not depends on which element is chosen next in case there is a tie during the merge step. If the element from the left list (the list of elements that came earlier in the original array) is always chosen, then the merge sort is stable. (g) T F [2 points] If we were to extend our O(n) 2D peak finding algorithm to four dimensions, it would take O(n 3 ) time. Solution: True. The algorithm would break the 4-dimensional array into 16 subarrays, find the maximum element among 12 3-D planes (O(n 3 ) elements), and recurse into a subarray with a higher neighbor. The recurrence is T (n) = T (n/2) + O(n 3 ) = O(n 3 ). (h) T F [2 points] A Θ(n 2 ) algorithm always takes longer to run than a Θ(log n) algorithm. Solution: False. The constant of the Θ(log n) algorithm could be a lot higher than the constant of the Θ(n 2 ) algorithm, so for small n, the Θ(log n) algorithm could take longer to run. (i) T F [2 points] Assume it takes Θ(k) time to hash a string of length k. We have a string of length n, and we want to find the hash values of all its substrings that have length k using a division hash. We can do this in O(n) time. Solution: True. Use a rolling hash.

6.006 Quiz 1 Solutions Name 4 Problem 4. Runway Reservation Modifications [20 points] (1 part) Recall the Runway Reservation system used in Problem Set 2. functionality of this system to deal with more types of requests. We would like to expand the Suppose there is a plane which needs to request an emergency landing at some time t 1 but there is already a flight scheduled to land there. Rather than shift this existing flight to the next open space, we would like to just push each subsequent flight back as much as we re able to until a big enough gap is created. For example, assuming a window of 3 minutes with flights at times 28, 31, 35, 38, 42, 46, 49, 53, 57, 60, attempting to insert another flight at time 31 would cause a new set of flights at times 28, 31, 34, 37, 40, 43, 46, 49, 53, 57, 60. In this case we would say four flights needed adjusting - 31, 35, 38, and 42 - to accommodate the emergency landing. Give an algorithm to find the minimum number of adjustments needed. This algorithm should find only the number of flight times requiring adjusting, it does not need to perform the flight updates. Full points will be given for an algorithm which operates well on a very tight schedule and has an asymptotic running time independent of the number of flights needing adjustment. You may use data structure augmentations provided that you explain the augmentation. Its maintenance may not increase the asymptotic running time of other operations but you are not required to prove this. Solution: Augment the tree with Rank and do binary search on Select, comparing the total amount of time since t 1 with the number of flights which have landed until the boundary of enough time to insert another flight is found. Running time of log 2 n since each Select takes log n time and we do log n of these. A more efficient solution is to augment the tree with Rank and simply walk down the tree, again comparing the number of flights since the insertion with the amount of time that has passed and recursing on the right child if there has not been enough time or the left child if there already has until the boundary is found. This means going down the tree only once, for a total of log n time. Solutions running in time which was not polynomial in log n and potentially touched every node in the tree were given half-credit if correct. Solutions with incorrect or missing time analysis were also given half-credit. Common incorrect solutions looked only at children of the flight being collided with and didn t actually traverse the tree, thus failing to find the correct place to stop if the collided flight was a leaf. Some students provided a clevel solution of Augment each node with the number of flights which need to be moved if there is a collision at that node. Sadly, the course staff was unable to find an augmentation capable of doing this without increasing the asymptotic running time of other operations, since any insertion may cause all n nodes to need to have their augmentation updated.

6.006 Quiz 1 Solutions Name 5 Problem 5. Computing Fibonacci numbers [20 points] (3 parts) The Fibonacci numbers are defined by the following recurrence: F 0 = 0, F 1 = 1, F i = F i 1 + F i 2 for i 2, yielding the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,... There is a closed-form formula for F n given by F n = φn (1 φ) n 5, where φ = (1 + 5)/2 1.618 is the golden ratio. However, this formula isn t practical for computing the exact value of F n as it would require increasing precision on 5 and φ as n increases. In this problem we are interested in obtaining practical algorithms for computing the n th Fibonacci number F n for any given n. Assume that the cost of adding, subtracting, or multiplying two integers is O(1), independent of the size of the integers we are dealing with. (a) [5 points] From the recurrence definition of the Fibonacci sequence, one can use the following simple recursive algorithm: F IB1(n): if n 1 then return n else x F IB1(n 1) y F IB1(n 2) return x + y end if Give the running time of this algorithm. Express your answer using Θ-notation. Solution: Let T (n) be the time taken to compute F n. We have T (n) = T (n 1) + T (n 2) + Θ(1), and so T (n) = Θ(F n ) = Θ(φ n ).

6.006 Quiz 1 Solutions Name 6 (b) [5 points] Give an algorithm that computes F n in Θ(n) and justify its running time. Solution: To obtain the desired algorithm we develop an iterative (non-recursive) version of the previous algorithm in which we memorize the last two computed Fibonacci numbers, so we are able to compute the next number in constant time. An example code looks as follows: F IB2(n): i 1 j 0 for k = 1 to n do temp i + j j i i temp end for return j To analyze the complexity of this algorithm it is sufficient to note that we have n iterations of the loop and each iteration can be performed in O(1) time.

6.006 Quiz 1 Solutions Name 7 ( ) ( ) 0 1 (c) [10 points] Consider the matrix A =. Show that for i 1, A 1 1 i Fi 1 F = i. F i F i+1 From the fact that A 2i = (A i ) 2, and A 2i+1 = A (A i ) 2, use divide and conquer to show that A n can be calculated in time Θ(log n). Conclude by giving a Θ(log n) algorithm for computing F n. Solution: We first show how to compute A n in Θ(log n) time. Consider the following algorithm: M atrix power(a, n): C M atrix power(a, n/2 ) C C C if n odd then C C A end if return C The recurrence describing the running time T (n) of this algorithm is T (n) = T ( n/2 )+ O(1). (Note that we work here with 4-by-4 matrices so multiplying them takes O(1) time.) Using Master Theorem we get T (n) = Θ(log n). Now, to compute F n we just call C := Matrix power(a, n) and return the entry C[1, 2] of the computed answer. Clearly, this takes Θ(log n) time.

6.006 Quiz 1 Solutions Name 8 Problem 6. One Hash, Two Hash [20 points] (4 parts) We talked in class about two methods for dealing with collisions: chaining and linear probing. Cornelius Beefe decided to come up with his own method. He creates two hash tables T 1 and T 2, each of size m, and two different hash functions h 1 and h 2 and decides to use T 2 to resolve collisions in T 1. Specifically, given an element x, he first calculates h 1 (x) and tries to insert x into T 1. If there is a collision, he calculates h 2 (x) and inserts x into T 2. (a) [4 points] Assume T 2 uses chaining to deal with collisions. Given an element y, give an algorithm for deciding whether or not y is in the hash table. Solution: Hash y using h 1 (y) and check if T 1 [h 1 (y)] contains an element. If this element is y, return true. Otherwise, hash y using h 2 (y) and check the list at T 2 [h 2 (y)]. Scoring: 4 points: For anything like the above solution 2 points: If you forgot to say something about hashing first to T 1 (b) [2 points] Cornelius tries using the following hash functions for each table: 1) h 1 (x) = (a 1 ) x mod m for table T 1 2) h 2 (x) = (a 2 ) x mod m for table T 2 He first tries a 1 = a 2 and uses chaining in table T 2. Is this likely to result in fewer collisions than if he had just used one hash table of size m with chaining? Solution: No. Items that collide in T 1 also collide in T 2. Scoring: 2 points: If you said No. 1 point: If you said yes, but showed you understood something of how the two tables interact.

6.006 Quiz 1 Solutions Name 9 (c) [4 points] Assume m = 21. Circle the set of values that will result in the fewest collisions in T 2 and explain why you chose it. (1) a 1 = 9, a 2 = 5 (2) a 1 = 5, a 2 = 7 (3) a 1 = 4, a 2 = 16 (4) a 1 = 13, a 2 = 11 Solution: There were two correct solutions. Full credit was given for saying a 1 = 13 and a 2 = 11 because neither is a power of the other (also gave credit for saying a 1 and a 2 were relatively prime to each other) and both are relatively prime to 21. Full credit was also given if you worked out the residuals modulo 21 and saw that a 1 = 9, a 2 = 5 had the most possible residuals. Scoring: 4 points: If you gave either of the two above answers with a correct explanation. 3 points: If you argued that a 1 and a 2 were relatively prime to the size of the table, but did not mention their relationship to each other. 2 points: If you said a 1 and a 2 were prime and/or relatively prime, but did not say that they were relatively prime to the size of the table. 1 point: If you circled one of the two correct answers with no justification. (d) [10 points] Consider the case in which T 2 uses linear probing to deal with collisions. Also, assume that h 1 satisfies the simple uniform hashing assumption and h 2 satisfies the uniform hashing assumption. We insert 4 elements. What is the probability that while inserting the fourth element there are at least two collisions?

6.006 Quiz 1 Solutions Name 10 Solution: There must be one collision in the first hash table and (at least) one in the second. There are four ways this can occur: 1) k 1 and k 2 mapped to the same position in T 1 (1/m), k 3 did not collide in T 1 ((m 1)/m), k 4 collided with k 1 or k 3 in T 1 (2/m), and k 4 collided with k 2 in T 2 (1/m). The probability of this case is (1/m)((m 1)/m)(2/m)(1/m) = (2(m 1))/m 4. 2) k 1 and k 3 collide in T 1, while k 2 does not. Has the same probability as the first case (just different order), (2(m 1))/m 4. 3) k 2 and k 3 collide in T 1. Again the same probablity of (2(m 1))/m 4 4) k 1, k 2, and k 3 mapped to the same position in T 1 (1/m 2 ), k 4 collides with k 1 in T 1 (1/m), and with k 2 or k 3 in T 2 (2/m). The probability is (1/m 2 )(1/m)(2/m) = 2/m 4 The total probability is therefore 6m 4 m 4 Scoring: Many people solved the incorrect problem in this part. Rather than solving for two collisions during the fourth insertion they solved for two collisions over all four insertions. This is a much easier problem so one point was taken off. If you tried to solve this problem and made a mistake, you were generally penalized more harshly because it was an easier problem. 10 points: For the correct answer 9 points: If you came close to the correct answer but missed a case, evaluated a probability wrong, or made some other fairly major math mistake. If you solved the wrong problem correctly, you also received 9 points. 7-8 points: If you had the right idea, but missed a lot of cases or evaluated most of the probabilities incorrectly. 7 points was also given if you explained exactly how to solve the problem but showed no math. 5-6 points: If you understood that one collision needed to occur in T 1 before the fourth insertion and/or that only one collision could occur in T 1 during the fourth insertion, but did not explain your answer clearly. 3-4 points: If you showed some understanding of the simple uniform hashing assumption, but did not demonstrate much understanding of how the two tables worked together.

6.006 Quiz 1 Solutions Name 11 1-2 points: If you realized that the probability should decrease with table size, but showed little other work or explanation.

6.006 Quiz 1 Solutions Name 12 Problem 7. Extreme Temperatures [20 points] (1 part) Professor Daskalakis is interested in studying extreme temperatures on the Arctic Cap. He placed temperature-measuring devices at m locations, and programmed each of these devices to record the temperature of the corresponding location at noon of every day, for a period of n days. Moreover, using techniques that he learned while preparing the Heapsort lecture, he decided to program each device to store the recorded temperatures in a max-heap. To cut a long story short, Prof. Daskalakis now has m devices that he collected from the Arctic Cap, each of which contains in its hard-drive a max-heap of n elements. He now wants to compute the l largest temperatures that were recorded by any device, e.g., if m = 2, n = 5, l = 5, and the two devices recorded temperatures ( 10, 20, 5, 34, 7) and ( 13, 19, 2, 3, 4) respectively, the desired output would be ( 2, 3, 4, 5, 7). Can you help your professor find the l largest elements in O(m + l log l) time? Partial credit will be given for less efficient algorithms, as long as the run-time analysis is correct. Solution: Build a max-heap H containing the numbers at the root of every heap. This takes O(m) time. Now go through the nodes of H. Each of these nodes v is a root of a max-heap H v from some device. v has at most 2 subtrees rooted at it in H and two subtrees T vl and T vr rooted at it in H v. Add the subtrees T vl and T vr to node v of heap H, by adding a record at node v remembering the indices of the roots of these subtrees in the array representing heap H v. This preprocessing step takes overall time O(m), the resulting max-heap H contains all collected data, and all nodes of H have at least 2 and at most 4 children. Now in O(l log l) time find the l biggest elements of H as follows: Start at the root of H. This is the biggest among all elements, so you can output it as the biggest number. Now insert all of its children in a new max-heap H ; this takes constant time since there are at most 4 of them. For each element that is inserted into H throughout the execution of the algorithm, we are going to remember its location in H. Now looking at the root of H, we can find the second biggest element among all collected elements. We output it, extract it from the heap H, and add to H its at most 4 children in H. Then we extract the new root of H, and so on. Every time we output an element we insert at most 4 elements in H, hence the size of H won t grow beyond 4l. Hence, every time we insert into H we pay at most O(log l). So, our running time is l log l. It remains to argue that the proposed algorithm outputs the largest l elements. Notice that the largest elements of H form a subtree rooted at the root of H. Argue by induction that a supertree of this subtree is inserted into H ; indeed, all children of every output element are inserted into H.