You are on page 1of 1

CSE 101 Quiz 1

(a) 300n + 24n0.6 + (log n)10 + 1 O ( n)

Solutions and feedback

Problem 1. (3 points) Rewrite the following functions in minimal big-O notation.

A common answer was (log n)10 . Remember that any polynomial function (here n) grows faster than any logarithmic function (here (log n)10 ). (b) 1 + 4 + 16 + 64 + + 4n 1 + 4 + 16 + 64 + + 4n = (c) log n + n10 + O(10n ) A common (wrong) answer was n10 which is polynomial, whereas 10n is exponential. Problem 2. (4 points) Suppose A() is a subroutine that takes as input a number in binary, and takes cubic time (that is, O(n3 ), where n is the length (in bits) of the number). Consider the following piece of code, which starts with an n-bit number x. while x > 1: call A(x) x = x/2 Assume that the division by two takes O(n) time on an n-bit number. (a) How many times does the inner loop iterate? Leave your answer in big-O form. As in the problem 8, part (a) of the homework 1, on each iteration of the loop, x is halved halved and thus shrinks by one bit. Thus there are at most n iterations. (b) What is the overall running time (as a function of n), in big-O form? Each iteration takes O(n3 ) + O(n) = O(n3 ) time and there are n iterations. Thus the overall complexity is O(n4 ), polynomial. A common mistake was to write that the time spent at each iteration was O(n) O(n3 ) = O(n4 ), whereas it was O(n3 ) + O(n) = O(n3 ). Problem 3. (3 points) True or false? (a) n2 is (nlog2 3 ) True. Just compare the exponents: 2 > log 3, therefore n2 > nlog2 3 that leads to the Omega bound. (b) 3n is O(2n ) False. If 3n = O(2n ), then there exists a constant c > 0 so that 3n c2n . But 3c = 1.5c 2c > c2c for every c > 0, which contradicts 3n = O(2n ). n 2n 2 Another way to see it is that lim n = lim = 0 gives us 2n = O(3n ) but not the other way around. n!1 3 n!1 3 (c) The depth of a 3-ary tree with n nodes is (log n) True. Some of you were confused about the base of the log. When it is not specied, the logarithm is always base 2. But it even doesnt matter for big-O, big- or big- bounds because logartihms base 2 and 3 just dier by the multiplicative constant log2 3 log3 n = log2 n log2 3 p n + 10n 4n+1 1 = O(4n ) 4 1

You might also like