









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Explain briefly why your algorithm is correct, and analyze its running time. (Hint: Use divide and conquer.) Solution: The idea is to partition ...
Typology: Summaries
1 / 15
This page cannot be seen from the preview
Don't miss anything!










��
Introduction to Algorithms October 14, 2005
Massachusetts Institute of Technology 6.046J/18.410J
Professors Erik D. Demaine and Charles E. Leiserson Handout 14
calculators or programmable devices are permitted.
Problem Parts Points Grade Grader
Total 80
Name:
Problem 1. Asymptotic Running Times [12 points] (4 parts)
For each algorithm listed below,
You need not justify your answers.
(a) Binary search
Solution: T (n) = T (n/2) + �(1) = �(lg n)
(b) Insertion sort
Solution: T (n) = T (n − 1) + �(n) = �(n^2 )
Problem 2. Substitution Method [7 points]
Consider the recurrence
T (n) = T (n/2) + T (n/4) + n ,
T (m) = 1 for m � 5.
Use the substitution method to give a tight upper bound on the solution to the recurrence using
O-notation.
Solution: We guess T (n) = O(n), which leads to the induction hypothesis T (m) � cm for all
m < n. For c � 1 , we have the base cases T (n) = 1 � cn for n � 5. The induction hypothesis
yields
T (n) = T (n/2) + T (n/4) + n � cn/2 + cn/4 + n = (3c/4 + 1)n.
If we choose c = 4, then T (n) � (3 + 1)n = 4n = cn. By induction on n, T (n) � cn for c � 4
and all n � 1.
�
Problem 3. True or False, and Justify [44 points] (11 parts)
Circle T or F for each of the following statements to indicate whether the statement is true or
false, respectively. If the statement is correct, briefly state why. If the statement is wrong, explain
why. The more content you provide in your justification, the higher your grade, but be brief. Your
justification is worth more points than your true-or-false designation.
T F The solution to the recurrence T (n) = 3 T (n/3) + O(lg n) is T (n) = �(n lg n).
Solution: False. Case 3 of the master theorem applies: f (n) = O(nlog^3 3 )= O(n) for f (n) = O(lg n), hence, T (n) = O(n).
T F Let Fk denote the kth Fibonacci number. Then, the n^2 th Fibonacci number Fn 2 can be computed in O(lg n) time.
Solution: True. The n^2 th Fibonacci number can be computed in O(lg n^2 ) = O(lg n) time by using square and multiply method with matrix
T F The array
20 15 18 7 9 5 12 3 6 2
forms a max-heap.
Solution: True.
T F Heapsort can be used as the auxiliary sorting routine in radix sort, because it operates in
place.
Solution: False. The auxiliary sorting routine in radix sort needs to be stable, meaining that numbers with the same value appear in the output array in the same order as they do appear in the input array. Heapsort is not stable. It does operate in place, meaning that only a constant number of elements of the input array are ever stored outside the array.
T F There exists a comparison sort of 5 numbers that uses at most 6 comparisons in the worst
case.
Solution: False. The number of leaves of a decision tree which sorts 5 numbers is 5! and the height of the tree is at least lg(5!). Since 5! = 120 , 26 = 64 , and 27 = 128 , we have 6 < lg(5!) < 7. Thus at least 7 comparisons are required.
T F Suppose that a hash table with collisions resolved by chaining contains n items and has a
load factor of � = 1 / lg n. Assuming simple uniform hashing, the expected time to search for an item in the table is O(1/ lg n).
Solution: False. The expected time to search for an item in the table is O(1 + �) = O(1 + 1 / lg n) = O(1). At least a constant running time O(1) is needed to search for an item; subconstant running time O(1/ lg n) is not possible.
T F Let S be a set of n integers. One can create a data structure for S so that determining
whether an integer x belongs to S can be performed in O(1) time in the worst case.
Solution: True. Perfect hashing.
� �
Problem 4. Close Numbers [17 points] (3 parts)
Consider a set S of n � 2 distinct numbers. For simplicity, assume that n = 2k^ + 1 for some
k � 0. Call a pair of distinct numbers x, y ≤ S close in S if
|x − y| � max z − min z , n − 1 z�S^ z�S
that is, if the distance between x and y is at most the average distance between consecutive numbers
in the sorted order.
(a) Explain briefly why every set S of n � 2 distinct numbers contains a close pair of numbers.
Solution: Without loss of generality, assume S = {z 1 , z 2 ,... , zn}, with zi � zi+1. The average distance between two consecutive numbers zi and zi+1 is
n− 1 1 �^1
n − (^1) i=
(zi+1 − zi) = n − 1
(zn − z 1 ).
There exists at least one pair of consecutive numbers x and y whose distance between them is less than or equal to the avearge. The result then follows from the definition of the close pair.
(c) Describe an O(n)-time algorithm to find a close pair of numbers in S. Explain briefly
why your algorithm is correct, and analyze its running time. ( Hint: Use divide and conquer.)
Solution: The idea is to partition S recursively until we find a close pair.
Since each recursive step reduces the cardinality of the set by roughly a half, the recursion is guaranteed to terminate. After each recursive step, the remaining set contains a close pair of S.
Step 1 takes O(n) time in the worst case, if we use the deterministic median-finding algorithm. Step 2 takes O(n) time based on the result from Part (b). Therefore, the running time of the algorithm is given by the following recurrence:
T (n) = T (n/2) + O(n),
with the solution T (n) = O(n) according to the master theorem.
SCRATCH PAPER — Please detach this page before handing in your exam.