q1-s2009-sol

q1-s2009-sol - Introduction to Algorithms Massachusetts...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Solutions Quiz 1 Solutions Problem 1. Asymptotic orders of growth [10 points] (4 parts) For each pair of functions, circle the asymptotic relationships that apply. You do not need to give a proof. (a) f ( n ) = n g ( n ) = log n Circle all that apply: f = O ( g ) f = Θ( g ) f = Ω( g ) Solution: log n grows more slowly than any polynomial in n , so f = Ω( g ) . (b) f ( n ) = 1 g ( n ) = 2 Circle all that apply: f = O ( g ) f = Θ( g ) f = Ω( g ) Solution: All constants are related to each other by a constant factor, so they have the same order of growth. f = Θ( g ) , and therefore f = O ( g ) and f = Ω( g ) also. (c) f ( n ) = 1000 · 2 n g ( n ) = 3 n Circle all that apply: f = O ( g ) f = Θ( g ) f = Ω( g ) Solution: 2 n / 3 n approaches 0 as n → ∞ . Therefore, the only relation that holds is f = O ( g ) . (d) f ( n ) = 5 n log n g ( n ) = n log 5 n Circle all that apply: f = O ( g ) f = Θ( g ) f = Ω( g ) Solution: n log 5 n = n log 5 + n log n = Θ( n log n ) . Also, 5 n log n = Θ( n log n ) . These are the same order of growth, so f = Θ( g ) , f = O ( g ) , and f = Ω( g ) .
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6.006 Quiz 1 Solutions Name 2 Problem 2. True or False [16 points] (4 parts) For each of the following questions, circle either T (True) or F (False). Explain your choice. (No credit if no explanation given.) (a) T F Performing a left rotation on a node and then a right rotation on the same node will not change the underlying tree structure. Explain: Solution: False. The figure below shows what happens in this case. To undo the left rotation on x , we must do a right rotation on y . x y a c b x y a b c y c x a b (b) T F While inserting an element into a BST, we will pass the element’s predecessor and successor (if they exist). Explain: Solution: True. The predecessor of a node is either the maximum element of its left subtree, or one of its ancestors. A newly-inserted node has no descendants, so the predecessor must be one of its ancestors, and hence the predecessor is on the path that was traversed during the insertion procedure. (A similar argument holds for the successor.) (c) T F For a hash table using open addressing, if we maintain m = Θ( n ) , then we can expect a good search and insert runtime. Explain: Solution: False. If the hash table is nearly full, for example if n = m - 1 , then the runtimes will take 1 1 - α = O ( n ) . (d) T F If we know ahead of time all the keys that will ever be inserted into a hash table, it is possible to design a hash table that guarantees O (1) lookup and insertion times, while using O ( n ) space. Explain: Solution: True. Use perfect hashing.
Image of page 2
6.006 Quiz 1 Solutions Name 3 Problem 3. Short Answer [24 points] (4 parts) (a) Describe an efficient method to merge two balanced binary search trees with n ele- ments each into a balanced BST. Give its running time. Solution: We can start by doing an in-order walk of both trees concurrently. At each step, we compare the two tree elements and add the smaller one into a list, L, before calling that element’s successor. When we finish walking both trees, L will contain a sorted list of elements from both trees. This takes O ( n + n ) = O ( n ) total time.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern