1
CSE 101 Quiz 1 Solutions and feedback Problem 1. (3 points) Rewrite the following functions in minimal big-O notation. (a) 300n + 24n 0.6 + (log n) 10 +1 O(n) 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 + ··· +4 n 1 + 4 + 16 + 64 + ··· +4 n = 4 n+1 - 1 4 - 1 = O(4 n ) (c) log n + n 10 + p n + 10 n O(10 n ) A common (wrong) answer was n 10 which is polynomial, whereas 10 n 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(n 3 ), 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(n 3 )+ O(n)= O(n 3 ) time and there are n iterations. Thus the overall complexity is O(n 4 ), polynomial. A common mistake was to write that the time spent at each iteration was O(n) O(n 3 )= O(n 4 ), whereas it was O(n 3 )+ O(n)= O(n 3 ). Problem 3. (3 points) True or false? (a) n 2 is (n log 2 3 ) True. Just compare the exponents: 2 > log 3, therefore n 2 >n log 2 3 that leads to the Omega bound. (b) 3 n is O(2 n ) False. If 3 n = O(2 n ), then there exists a constant c> 0 so that 3 n c2 n . But 3 c =1.5 c 2 c >c2 c for every c> 0, which contradicts 3 n = O(2 n ). Another way to see it is that lim n!1 2 n 3 n = lim n!1 2 3 n = 0 gives us 2 n = O(3 n ) but not the other way around. (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 specified, the logarithm is always base 2. But it even doesn’t matter for big-O, big-or big-bounds because logartihms base 2 and 3 just dier by the multiplicative constant log 2 3 log 3 n = log 2 n log 2 3

quiz one

Embed Size (px)

DESCRIPTION

data structures

Citation preview

Page 1: quiz one

CSE 101 Quiz 1 Solutions and feedback

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

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

O(n)

A common answer was (log n)10. Remember that any polynomial function (here n) grows faster than anylogarithmic function (here (log n)10).

(b) 1 + 4 + 16 + 64 + · · ·+ 4n

1 + 4 + 16 + 64 + · · ·+ 4n =4n+1 � 1

4� 1= O(4n)

(c) log n+ n10 +pn+ 10n

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 thusshrinks 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 isO(n4), polynomial.

A common mistake was to write that the time spent at each iteration was O(n)⇥ O(n3) = O(n4), whereasit 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.5c2c > c2c for everyc > 0, which contradicts 3n = O(2n).

Another way to see it is that limn!1

2n

3n= lim

n!1

✓2

3

◆n

= 0 gives us 2n = O(3n) but not the other way around.

(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 specified, the logarithm is alwaysbase 2. But it even doesn’t matter for big-O, big-⌦ or big-⇥ bounds because logartihms base 2 and 3 justdi↵er by the multiplicative constant log

2

3

log3

n =log

2

n

log2

3