q1-s2009-sol

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

This preview shows pages 1–3. Sign up to view the full content.

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 ) .

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.

### Page1 / 12

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online