This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Solutions to Quiz 1 “Points for a problem are generally equally divided among problem parts.
Answers needn‘t be as oomplete as here: we give liberal partial credit for
showing understanding of ideas.” Problem 1 [2!] points] Give asymptotiosiiy tight upper [big 0] bounds for Tin] in each of the fol
lowing recurrences. Justify your solutions by naming the particular case of
the master theorem, by iterating the recurrence, or by using the substitution
method. a. Tin] = Tfrt 2} + 1.
Answer: Tm} = 01:11.]
by iteration:
Gin} terms
Tm} = m = om;
b. Tin} = 2T{n{2} +_n lg“ :1.
Answer: Th} 2 Din [3‘3 n) [or Biznlganjj
by master theorem, case 2 {as generalized in Exercise 4.42].
c. Tm} = 91"{112'4} + 11*.
Answer: ﬂux} = Cling} {or Shah
by master theorem, case 3.
d. Tin] = 3T{nf2:l + 11..
Answer: Tin} = Din“: a} {or Ei{n'°h 3}}
by master theorem, case 1.
e. Tin} = lenf'E + ﬂ} + :1. Answer: Tin} = Din}
by substitution. {To make the guess, notice that when n is large, 11,52 + q? is close to nil
so 0(a) is a reasonable guess, based on master theorem case 3.] Show Th] 5 art by induction:  Initially true {for small a] if .1: large enough.
{{Note that you actually have to assume '1" is constant for n 5 4, since TH} = T{4,{2 + oi} + 4 = m} + 4.)}
. Assume true for numbers 6. n. Then
ﬂu} = This + ﬂ} + n 5 onﬂ + cﬁ+ :1 With a little aLg‘ebis.1 can see that this is 5 on for large enough a and 4::
(es. e = 4,1: 316}. Problem 2 [20 points] Consider the code for BUILDHEW. which operates on a heap stored in an
array AI]. . .11]: BUTLD HEAPIEA] 1 heepeize[A] :— longtithe]
‘2 for t‘ +— Liertyth[A]g'2] domto l
3 do HEAPIFYIIAJ} a. Show how this procedure can be implemented as a recursive divideend—
conquer procedure BUILDHEAPIIIA. i}, where Ah] is the root of the suhheep
to be built. To build the entire heap1 we would call BUILD~HEAPL£L 1}. Answer: BUILDrHEAPEA. t} Hi 5 [tangmmytj c or : 5 3:119:ij
then EU]LEIHEAP{A, 2i)
ELIILD HEAPLJL it + 1}
HEAPIFYM. t} 1:. Give a recurrence that describes the worstcsse running time of your pro wentthere+
Answer: Tin) = ZTﬁnfﬂl + G'ﬂgnj o. Solve the recurrence using the master method [or otherwise for partial
credit]. Answer: The} = 9(a) by case 1 of master theorem Problem 3 {2D points] Give a short answer for each problem below. The more concise and precise
your answer, the more points you will receive. a. Prose that [n + 1]2 = 01:71:} by giving the constants up and c in the
deﬁnition of {SJnotation. Justify your answer. Answer: c = 2.1% =3
Need [n+1}25m2fur aJlnEng.
{n+1}2 5 seinen+1; a“. which is true for n :3 a. b. Suppose that you want to sort n numbers, each of which is either [I or 1.
Brieﬂy describe an asymptotically eﬁcient algorithm for this problem.
What is its running time?I
Answer: Counthtg sort. 0(a) time (actually. Biol} Gr partition around any element. c. Brieﬂy describe what we mean by a randomized algorithm, and name
two examples. Anstver: Behavior determined in part by randomnumber generator.
E.g. randomised quicksort {Section 5.3} randomised select {Section 10.2] ship lists [Lecture 1E1] universal hashing {Section 12.3.3} randomized partition [Section 3.3] d. Consider a priority queue that supports the operations INssa'r and Earmar—
lvllx. Argue that in the worst case. if we perform a rnixturs of r: Insane" and ExTRACT~MIN operations on the priority queue. there is at least one
operation that takes ﬂﬂg n] time. Assume that the order of elements in
the priority queue is determined by comparisons only. (Hint: For any est
of numbers. at least one number must equal or exceed the average.) Answer: Can sort nﬂ numbers by doing of? Insssrs and of! ExTRaCT
Mme [n operations all together}. But comparison sorts take ﬂfnlg n} time, in this case
n n
a (5133) mean} The average time for this set of n operations {and hence for the Worstease
mix of r: operations] is thus ﬂﬂgn). so at least one takes i'tﬂgn} time. ...
View
Full Document
 Spring '09
 STAFF
 Algorithms

Click to edit the document details