q1-f2008-sol

# q1-f2008-sol - 6.006 Fall 2008 Quiz 1 Solutions...

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

Introduction to Algorithms October 15, 2008 Massachusetts Institute of Technology 6.006 Fall 2008 Professors Ronald L. Rivest and Sivan Toledo Quiz 1 Solutions Quiz 1 Solutions Problem 1. Asymptotic growth [10 points] For each pair of functions f ( n ) and g ( n ) given below: Write Θ in the box if f ( n ) = Θ( g ( n )) Write O in the box if f ( n ) = O ( g ( n )) Write Ω in the box if f ( n ) = Ω( g ( n )) Write X in the box if none of these relations holds If more than one such relation holds, write only the strongest one. No explanation needed. No partial credit. O , Θ , Ω or X f ( n ) g ( n ) O n 2 n 3 Ω n lg n n Θ 1 2 + sin n Ω 3 n 2 n Θ 4 n +4 2 2 n +2 O n lg n n 101 / 100 Θ lg 10 n lg n 3 O n ! ( n + 1)!

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. Miscellaneous True/False [15 points] (5 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 A hash table guarantees constant lookup time. Explain: Solution: False. It only has expected constant lookup time; if Θ( n ) elements collide, then lookup may take Θ( n ) time in the worst case (assuming chaining). (b) T F A non-uniform hash function is expected to produce worse performance for a hash table than a uniform hash function. Explain: Solution: True. A non-uniform hash function is more likely to result in colli- sions, which leads to slower lookup times. (c) T F If every node in a binary tree has either 0 or 2 children, then the height of the tree is Θ(lg n ) . Explain: Solution: False. One counterexample is a tree like the one shown here, extending down and to the right. It has Θ( n ) height. (d) T F A heap A has each key randomly increased or decreased by 1. The random choices are independent. We can restore the heap property on A in linear time. Explain: Solution: True. Simply call B UILD -H EAP at the root, which runs in Θ( n ) time. (e) T F An AVL tree is balanced, therefore a median of all elements in the tree is always at the root or one of its two children. Explain: Solution: False. An AVL tree doesn’t guarantee that the left and right subtrees will be equal sizes; it only guarantees that the heights of the trees are close.
6.006 Quiz 1 Solutions Name 3 Problem 3. Sorting short answer [10 points] (3 parts) (a) What is the worst-case running time of insertion sort? How would you order the elements in the input array to achieve the worst case? Solution: Reverse order. Θ( n 2 ) running time. (b) Name a sorting algorithm that operates in-place and in Θ( n log n ) time. Solution: Heapsort. (c) Write down the recurrence relation for the running time of merge sort. (You don’t need to solve it.) Solution: T ( n ) = 2 T ( n 2 ) + Θ( n )

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

View Full Document
6.006 Quiz 1 Solutions Name 4 Problem 4. Hashing [10 points] (4 parts) Give a hash table that uses chaining to handle collisions, how would using sorted python lists in place of unsorted chains affect the following run times? Explain the circumstances of each of the four cases and
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

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

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

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

Dana University of Pennsylvania ‘17, Course Hero Intern

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

Jill Tulane University ‘16, Course Hero Intern