You are on page 1of 9

PROBLEM SET 1

YEON JIN LEE; TA: HOA LONG TAM CS170-FALL2011

1. DPV0.1 In each of the following situations, indicate whether f = O(g) or f = (g) or both (in which case f = (g)) (a) n 100, n 200 n100 f = (g) because n200 approaches 1 as n . (b) n1/2 , n2/3 f = O(g) because na = O(nb ) if a < b (c) 100n + log n, n + (log n)2 f = (g). Simplifying by only keeping the dominant terms we get f = 100n and g = n. |f (n)/g(n)| approaches 100 as n . (d) n log n, 10n log 10n f = (g). Divide both f and g by n. We get f = log n, g = 10 log 10n. We can ignore the coecient 10 in g. Then f = log n, g = log 10n. Break up g into addition of two logs: g = log 10 + log n. We ignore the constant term log 10. We see that both f and g are equal to log n. Therefore f = (g). (e) log 2n, log 3n f = (g) We can rewrite f and g as f = log 2 + log n and g = log 3 + log n. We can ignore the constant terms log 2 and log 3. We see that f = 1. Therefore f = (g). g (f) 10 log n, log(n2 ) f = (g). We ignore the coecient 10 as it becomes insignicant as n . Replace n log n with 2k . We get f = k, g = 2kThe log(n2 ) approaches nite nonzero constant as n . (g) n1.01 , n log2 n f = (g) Rewrite n1.01 as n n.01 . Then we can cancel out from f and g by dividing both of them by n. Now f = n.01 and g = log2 n. Replacing n with 2k (or any base raised to kth power), we get f = 2.01k , g = k 2 . f grows exponentially whereas g grows polynomially. Therefore, f is an upperbound of g.
1

YEON JIN LEE; TA: HOA LONG TAM CS170-FALL2011

(h) n2 / log n, n(log n)2 f = (g) First, divide both sides by n and we get f = n/ log n, g = (log n)2 . Replacing k n with 2k , we get 2k and g = k 2 . f grows exponentially whereas g grows polynomially. Therefore, f is an upper bound of g. (i) n0.1 , (log n)10 f = (g). Any polynomial dominates any logarithm. Therefore f is an upper bound of g. (j) (log n)log n , n/ log n f = (g). Replacing n with 2k , we get f = k k , g = 2k /k. f = k k grows much faster than g; therefore f is an upper bound of g. (k) n, (log n)3 f = (g). Any polynomial dominates any logarithm.Therefore, nonzero constant as n

|f (n)| |g(n)|

approaches a nite

(l) n1/2 , 5log2 n f = O(g). Replacing n with 2k , we get f = 2k/2 , g = 5k . g is an upper bound of f because na = O(nb ) if a < b (m) n2n , 3n n f = O(g). n2 = n (2/3)n . This approaches 0 as n because the exponential 3n term (2/3)n dominates the polynomial term n.We know that if |f (n)/g(n)| approaches 0 as n , then f = O(g). (n) 2n , 2n+1 2n f = (g). 2n+1 = 1/2. (o) n!, 2n f = (g). As n ,n! grows much faster then n2 (i.e. n! = 1 2 3 ....n and 2n = 2 2 2 2. |f (n)| approaches 0 as n ; therefore, g is the lower bound of f. |g(n)| (p) (log n)log n , 2(log2 n) 2 f = O(g). Replacing n with 2k , we get f = k k , g = 2k . Comparing just the exponents (since exponent term dominates polynomial term when n , we see that g grows faster then f. Therefore g is the upper bound of f. (q) n ik , nk+1 i=1 f = (g). Proof for f = O(g) f = n ik is essentially f = 1k + 2k + ...n 1k + nk . g = nk is essentially replacing each i=1 term of f with nk like so: nk + nk + ....nk , which equals n nk . As n approaches , we see
2

PROBLEM SET 1

that g is the upper bound of f. Therefore f = O(g) Proof for f = (g) k+1 k+1 k+1 Therefore f = (g). = i |n = n k+1 0 k+1 < n

n k i=1 i di

YEON JIN LEE; TA: HOA LONG TAM CS170-FALL2011

2. DPV0.2 Show that if c is a positive real number, then g(n) = 1 + c + c2 + ... + cn is: (a) (1) if c < 1. (b) (n) if c = 1. (c) (cn ) if c > 1 We can rewrite g(n) as a geometric series. g(n) = 1c . 1c We know that any exponential dominates any polynomial. Therefore the term in numerator, 1 cn dominates 1 c. If c < 1, as n , the numerator term approaches 1, since cn approaches 0. And the entire sum approaches a constant.Therefore, (a) holds true. If c = 1, then it is just multiplying 1 n times since 1 raised to any power equals 1. Therefore (b) holds true. If c > 1, we consider only the dominant terms which is the exponential in the numerator, cn+1 . This value approaches cn as n . So (c) holds true.
n+1

PROBLEM SET 1

3. DPV0.4 (a) Show that two 2 x 2 matrices can be multiplied using 4 additions and 8 multiplications. Say there are two matrices: a b A= . c d B= Then, AB = ae + bg af + bh . ce + dg cf + dh e f . g h

(b) Show that O(log n) matrix multiplications suce for computing X n . (Hint: think about computing X 8 . For X 8 case, store the X 2 matrix. Multiply this by itself to get X 4 matrix. Store this matrix. Multiply this by it self to get X 8 . In general, for X n case, it takes exactly c multiplications, where c is number of power of 2s that is less than n. This c value can be found by taking the log of n since log N is the power to which you need to raise 2 in order to obtain n.(TALK ABOUT WHEN N IS ODD) (c)Show that all intermediate results of b3 are O(n) bits long. Multiplication of d x d matrix by another d x d matrix, retains dimension (the result is d x d). b3s intermediate results are all 2x2 matrices whose elements are at most 2n bits long (this can be proved by multiplying out the matrix few times - the lower right element will be the biggest element - we track its length in bits.). (d) Let M (n) be the running time of an algorithm for multiplying n-bit numbers, and assume that M (n) = O(n2 ). Prove that the running time of b3 is O(M (n) log n). b3 does at most n bit multiplication (as proved in (c)), at most log(n) times (as proved in (b)). Therefore, b3 takes O(M(n)* log(n)) time. (e) Can you prove that the running time of b3 is O(M (n))? Assume M (n) = (na )) for some 1 a 2. x= 0 1 . 1 1

xn = (xn/2 )2 = (xn/2 )(xn/2 ) T (n) = T (n/2) + O(nd ) T (n) = T (n/2) + M (n) Using the Master Theorem, we set a= 1, b = 2, d = 1. 1 > log2 1. Therefore, running time of b3 is O(M (n)).

YEON JIN LEE; TA: HOA LONG TAM CS170-FALL2011

4. DPV1.4 Show that log(n!) = (n log n) (Hint: to show an upper bound, compare n! with nn . To show a lower bound, compare it with (n/2)n/2 . f = (g) i f = O(g) and f = (g). Proof for f = O(g) We can represent n! as 1 2 3 ....n 1 n. This value is clearly less than nn (which is n*n*....n) as n .. Therefore, n! = O(nn ) Proof for f = (g) n Assuming we know n! > n 2 , we can take log of both sides and get log n! > n/2 log n/2. 2 This is equal to log n! > (n/2 log n log 2); n/2 and log 2 are both constants so we can simplify this to log n! > n log n.

PROBLEM SET 1

5. 5 Assume we have T [i, j] = r=0 i for i = 0 to n-1 for j = 0 to n-1 S[i,j] = 0; imin = max(i-(d+1),0) imax = min(i+d, n-1) jmin = max(j-(d+1),0) jmax = min(j+d, n-1)
s=0 jA[r, s]

that is quickly computable.

S[i,j] += T[imax,jmax]-T[imax,jmin]-T[imin,jmax] +T[imin,jmin] end for end for

YEON JIN LEE; TA: HOA LONG TAM CS170-FALL2011

6. 6 (a) Prove: Given any sequence of digits in any base, there are innitely many primes that start with these digits. Say n is some scalar constant (i.e 1234). All numbers that start with n can be written as n 10d ... (n + 1) 10d 1, where d 0, 1, 2, ... Using Prime Number Theorem, we can prove that there is more than 1 prime between n 10d and (n) 10d+1 1. Prime Number Theorem states that (x) = x/(ln(x)). We can set x = n 10d and x = (n) 10d+1 1. If x x 1, we know that there is at least 1 prime between x = n 10d and x = (n) 10d+1 1, for some d 0..... (x = n 10d ) = ((n) 10d+1
n10d ln(n10d ) (n)10d+1 1 1) = ln((n)10d+1 1)

By replacing n with ek and getting rid of constant terms, we get k (x) = ek k +1 (x ) = e k Subtracting x from x, we see that it equals 1 (this is approximate calculation using big-Oh notation). Therefore, we can conclude that there are innitely many primes that start with given sequence of digits. (b) What is the probability that a random number, that is exactly n bits long, is prime? What is the expected number of iterations of this algorithm before it halts? n-bit number has value of 2n to 2n+1 1 in base 10. Using Prime Number Theorem, we can calculate the number of primes that exist between 2n+1 1 and 2n ; this is simply 2n+1 1) 2n (2n+1 1) (2n ), which can be approximated as ln(2n+1 1 ln 2n = (we will call this value ). Probability is then number of primes in given range divided by all values in range: prob = 2n+1 12n . Expected number of iterations of this algorithm is E = 1 + (1 prob) E. Solving for E, n+1 n we get E = 1/prob = 2 12 (c)Compute the expected number of iterations for n=200 bits. 2200+1 1) 2200 = ln(2200+1 1 ln 2200
12 p= 2 = 140 Answer: 140 iterations
200+1 200

PROBLEM SET 1

You might also like