網路題目

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. Θ(n 2 ), D. Θ(n 2 log n), E. Θ(n 3 ), 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) + n 2 T (n)=8T (n/2) + n 2.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) 1

Upload: jen

Post on 02-Dec-2015

219 views

Category:

Documents


2 download

DESCRIPTION

資結

TRANSCRIPT

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. Θ(n2.934 )

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)

1

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 nounderlying 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 itp1.

3. Similarly, find the median of the last n/2 elements (an2+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 thanmax(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)

2

Question 3 (40 pts)

1. (9pts) Give a best (highest) lower bound for the number of comparisons necessary for anyalgorithm to sort any list of 4 elements in the comparison-based model (i.e. you are onlyallowed to make comparisons).

(a) 3 (b) 4 (c) 5 (d) 6

2. (11pts) Consider the following data structure: Usually, inserting an element to this data-structure requires Θ(1) work. However, the data-structure has a parameter k, s.t. everyk elements we insert, it requires additional Θ(k) work. That is, upon insertion of the k-thelement, 2k-th element, 3k-th element and so on, we take additional Θ(k) steps.

What is the amortized cost per operation of inserting k33 elements into the data structure?

(a) Θ(1) (b) Θ(√

k) (c) Θ(k) (d) Θ(k33)

3. (9pts) We are given n coins. n/2 of them are fair. n/3 fall on HEADS w.p. 0.4, and the restalways 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 numberstake O(n). Left-shift or right-shift of an n bits number is free.

A 2n-bits number is of the form xx 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)? Choosethe best running time.

(a) M(n) + Θ(n) (b) M(n) + Θ(n2) (c) M(2n) + Θ(n) (d) 3M(n) + Θ(n)

3

—————– Extra Credit —————–

5. (Extra credit: 1pt) Algorithm A gets as input n elements (denoted a1, a2, . . . , an), andoutputs two numbers, x and y. x is the largest element out of the first n/2 elements, y is thelargest out of the latter n/2 elements. That is, x is the largest out of a1, a2, . . . , an/2, and yis the largest out of an

2+1, an

2+2, . . . , an.

How many comparisons must algorithm A make?

(a) n− 2 (b) n− 1 (c) n + blog nc − 3 (d) n + blog nc − 4 (e) log2(n!) (f) Itis impossible to tell.

6. (Extra Credit: 4pts) Algorithm B gets as input n elements (denoted a1, a2, . . . , an), andoutput three numbers, x, y and i. Any i from 1 to n is permissible. x is the largest elementout of the first i elements, y is the largest out of the latter n− i elements. That is – x is thelargest 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 (c) n + blog nc − 3 (d) n + blog nc − 4 (e) log2(n!) (f) Itis impossible to tell.

4

Solution - Q1

E T (n) = 8T (n/2) + n2

E T (n) = 8T (n/2) + n2.93 (log n)93

A 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 consistof 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: iden-tifying that all 3 steps take Θ(n) time, identifying that you got rid of at least n/4 + n/4elements.

4. (b)This recursion unfolds to n + (n/2) + (n/4) + . . . + 1 = n ·

∑log ni=0 2−i ≤ 2n.

Writing (b) = 4pts, unfolding / using master thm / using guess and check via accurateinduction (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 somethingthat shows understanding (no more than half of the points).

5