Documente Academic
Documente Profesional
Documente Cultură
2.93
4
For each one, write down the letter of the correct answer. Assume the base case T (x 5) = 1.
(No proofs are required for this problem.)
T (n) = 8T (n/2) + n2
T (n) = 8T (n/2) + n2.93 (log n)93
T (n) = n + T (n/3) + T (n/4) + T (n/5) + T (n/6).
(Note that 1/3 + 1/4 + 1/5 + 1/6 = .95)
b. (8 pts) How many elements must be larger than the maximum of p1 and p2 ?
Your answer may be within 1 of the exact answer. Explain in a sentence.
d. (9 pts) What is the best upper bound you can give for T (n)? Explain in a sentence.
(a) O(log n)
(b) O(n)
(d) O(nlog2 3 )
(b) 4
(c) 5
(d) 6
2. (11pts) Consider the following data structure: Usually, inserting an element to this datastructure requires (1) work. However, the data-structure has a parameter k, s.t. every
k elements we insert, it requires additional (k) work. That is, upon insertion of the k-th
element, 2k-th element, 3k-th element and so on, we take additional (k) steps.
What is the amortized cost per operation of inserting k 33 elements into the data structure?
(a) (1)
(b) ( k)
(d) (k 33 )
(c) (k)
3. (9pts) We are given n coins. n/2 of them are fair. n/3 fall on HEADS w.p. 0.4, and the rest
always fall on TAILS. We toss them all. What is the expected number of TAILS?
(a) n/6
(b) 0.5 (n/2) + 0.5 (n/3) + 0.5 (n/6)
(c) 0.5 (n/2) + 0.4 (n/3)
Extra Credit
5. (Extra credit: 1pt) Algorithm A gets as input n elements (denoted a1 , a2 , . . . , an ), and
outputs two numbers, x and y. x is the largest element out of the first n/2 elements, y is the
largest out of the latter n/2 elements. That is, x is the largest out of a1 , a2 , . . . , an/2 , and y
is the largest out of a n2 +1 , a n2 +2 , . . . , an .
How many comparisons must algorithm A make?
(a) n 2
(b) n 1
is impossible to tell.
(c) n + blog nc 3
(d) n + blog nc 4
(f) It
(c) n + blog nc 3
(d) n + blog nc 4
(f) It
Solution - Q1
E
T (n) = 8T (n/2) + n2
10 points each.
Solution - Q2
1. TRUE.
Comparing all elements to p1 and p2 alone takes (n) steps.
Writing TRUE = 2pts, identifying any element of the alg that actually takes (n) = 2pts.
2. n/4.
Both p1 and p2 are smaller than half of the elements in their set, and both their sets consist
of n/2 numbers.
Writing n/4 = 4pts, identifying p1 and p2 as medians in sets of n/2 elements = 4pts.
3. T (1) = 1 and T (n) = n + T (n/2).
Base case=1pt, recursion formula=3pts. Explanation sentence 5pts. Need to appear: identifying that all 3 steps take (n) time, identifying that you got rid of at least n/4 + n/4
elements.
4. (b)
P n i
2n.
This recursion unfolds to n + (n/2) + (n/4) + . . . + 1 = n log
i=0 2
Writing (b) = 4pts, unfolding / using master thm / using guess and check via accurate
induction (not an induction with hypothesis of the sort runtime = O(n)) = 5pts.
Someone writing the wrong recursion in previous section and solving it correctly = all points.
Solution - Q3
1. (c)
2. (a)
3. (d)
4. (a)
5. (a)
6. (a)
Points as specified in the section. Partial credit if they get the wrong answer but write something
that shows understanding (no more than half of the points).
5