Sunteți pe pagina 1din 5

15-451 S10: Quiz

Name and Andrew id:

Question 1 - Recurrences (30 pts)


Each recurrence below solves to one of the following:
A. (n), B. (n log n), C. (n2 ), D. (n2 log n), E. (n3 ), F. (n

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)

Question 2 - Algorithm Analysis (30 pts)


We denote the linear-runtime algorithm studied in class for finding the median by Find-Median.
Here is a new algorithm ALG, that gets as input n numbers, denoted a1 , a2 , . . . , an . (It has no
underlying purpose. We are only interested in the running time analysis)

1. If the input consists of 1 or 2 numbers, return the first element.


2. Find the median of the first n/2 elements (a1 , a2 , . . . , an/2 ), using Find-Median. Call it
p1 .
3. Similarly, find the median of the last n/2 elements (a n2 +1 , . . . , an ), using Find-Median.
Call it p2 .
4. Find all elements that are between p1 and p2 (bigger than min(p1 , p2 ) yet smaller than
max(p1 , p2 )). Recursively run ALG over these elements.
Let T (n) denote the running time ALG over any input of n numbers.
a. (4 pts) TRUE or FALSE: The running time of ALG, T (n) (n).
Assume the comparison tree model. Explain your answer in a sentence.

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.

c. (9 pts) Write down the recursion representing T (n). 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)

(c) O(n log n)

(d) O(nlog2 3 )

Question 3 (40 pts)


1. (9pts) Give a best (highest) lower bound for the number of comparisons necessary for any
algorithm to sort any list of 4 elements in the comparison-based model (i.e. you are only
allowed to make comparisons).
(a) 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)

(d) 0.5 (n/2) + 0.6 (n/3) + 1 (n/6)


(e) It is impossible to tell.
4. (11 pts) Let M (n) be the time for multiplying two n-bits numbers. Adding two n bits numbers
take O(n). Left-shift or right-shift of an n bits number is free.
A 2n-bits number is of the form x x if its n MSBs (most significant bits) equal to its n LSBs
(least significant bits). For example: 100100, 1111, 1011010110.
How long does it take to multiply two 2n-bits number of the form (x x) (y y)? Choose
the best running time.
(a) M (n) + (n)

(b) M (n) + (n2 )

(c) M (2n) + (n)

(d) 3M (n) + (n)

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

(e) log2 (n!)

(f) It

6. (Extra Credit: 4pts) Algorithm B gets as input n elements (denoted a1 , a2 , . . . , an ), and


output three numbers, x, y and i. Any i from 1 to n is permissible. x is the largest element
out of the first i elements, y is the largest out of the latter n i elements. That is x is the
largest out of a1 , a2 , . . . , ai , and y is the largest out of ai+1 , ai+2 , . . . , an .
How many comparisons must algorithm B make?
(a) n 2
(b) n 1
is impossible to tell.

(c) n + blog nc 3

(d) n + blog nc 4

(e) log2 (n!)

(f) It

Solution - Q1
E

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)

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

S-ar putea să vă placă și