# Cs141 Home/Hwk1Soln

Cs141 Home | Cs141 Home | recent changes | Preferences

1. Ordered from slowest growing to fastest growing:

1/n < 2100 < log log n < log2 n < n0.01 < ⌈ sqrt(n) ⌉ < 2log2 n < 6n log n < 4n3/2 < 22n

2.
 1 Hour 2 Hours log n 236*108 272*108 sqrt(n) 1296*1019 5184*1019 n 36*108 72*108 n log n 1.33*108 2.58*108 n2 60000 8.49*104 n3 1532.62 1930.98 2n 31 32 n! 12 13

3.

We want the following inequality to hold for a constant c when n ≥ n0:

n5 + 5n4 + 10n3 + 10n2 + 5n + 1 ≤ cn5.
Dividing both sides by n5, this is the same as
(n5 + 5n4 + 10n3 + 10n2 + 5n + 1)/n5 ≤ (cn5)/n5.
Simplifying, this is the same as
1 + 5/n + 10/(n2) + 10/(n3) + 5/(n4) + 1/(n5) ≤ c.

The inequality holds when n=1 and c=32 (check).
All terms except 1 on the left side are decreasing functions of n.
Therefore when n ≥ n0 = 1, c = 32, the inequality holds. This completes the proof.

4.

Algorithm (Input: X = (a1, a2, a3,.. an), Output: A = (A1, A2, ..., An)
1. A = X
2. print A
3. for i=2 to n
4. ___ A[i] = A[i-1] + X[i]
5. ___ print A[i]
6. end for

Complexity: The algorithm clearly runs in O(n) time.

Correctness: Clearly, A[i] = a + a + ... + a[i]. (prove by induction on i if you want to be formal).

5.

(tasters) Let n be the number of bottles. Let k = ⌈ log2 n ⌉ and choose k testers, call them 1,2,..,k. There are 2k ≥ n subsets of {1,2,...,k}. For i = 1,2,...,n, pair the i'th bottle with the i'th subset Si of {1,2,...,k}, and have the tasters in that subset taste the i'th bottle.

If the i'th bottle is poisoned, then at the end of the month, exactly those tasters in the set Si will die. This will identify which bottle is poisoned.

(missing number) Here are two algorithms. Let the array be A[1..n-1].

Algorithm 1:

1. Compute T = 1+2+3+...+n and S = A + A + ... + A[n-1]. Then T-S is the missing number.

Algorithm 2:

1. Use the partition procedure from quicksort to reorder the array so that the numbers less than n/2 are in the first half of the array, while the other numbers are in the second half of the array.
2. By counting the numbers in the two halves of the array, determine whether the missing number is less than n/2 or ≥ n/2.
3. Recurse on whichever half of the array the missing number is missing from.

Total time: n + n/2 + n/4 + n/8 + ... = O(n)

Cs141 Home | Cs141 Home | recent changes | Preferences