You are on page 1of 3

CSE332 Week 2 Section Worksheet Solutions 1. Prove f(n) is O(g(n)) where a. f(n)= n2!

3n g(n)=n" Solution# $ccor%ing to the %efinition of O( )& we nee% to fin% 'ositive real ()s n* + c so that f(n),=c-g(n) for all n.=n* Pick n*=1 + c=1*/ f + cg are e0ual at n=1& an% g rises 1ore 0uickl2 than f after that. 3. f(n)=n!2nlogn g(n)=nlogn Solution# n*=2 + c=1* --4 think 4 sai% n*=1 + c=1* in class& 3ut that won)t work/ sorr2# consi%er w5 f(1)=1!2-1-log1=1!2-1-*=1 g(1)=1-log1=1-*=* so f(1) . 1*-g(1) --6he values we choose %o %e'en% on the 3ase of the log/ here we)ll assu1e 3ase 2 c. f(n)=1*** g(n)=3n3 Solution# n*=1 + c="** works %. f(n)= n g(n)=n51* Solution# n*=1 + c=1** works 2. 6rue or false& + e7'lain a. f(n) is 8(g(n)) i1'lies g(n) is 8(f(n)) Solution# 6rue# 4ntuitivel2& 8 is an e0uals& an% so is s211etric. 9ore s'ecificall2& we know f is O(g) + f is :(g) so 6here e7ist 'ositive ( c& c)& n* + n*) such that f(n),=cg(n) for all n.=n* an% f(n).=c)g(n) for all n.=n*) so

g(n),=f(n)5c) for all n.=n*) an% g(n).=f(n)5c for all n.=n* so g is O(f) an% g is :(f) so g is 8(f) 3. f(n) is 8(g(n)) i1'lies f(n) is O(g(n)) Solution# 6rue# ;ase% on the %efinition of 8& f(n) is O(g(n)) c. f(n) is :(g(n)) i1'lies f(n) is O(g(n)) Solution# <alse# Counter e7a1'le# f(n)=n2 + g(n)=n/ f(n) is :(g(n))& 3ut f(n) is =O6 O(g(n)) 3. <in% functions f(n) an% g(n) such that f(n) is O(g(n)) an% the constant c for the %efinition of O( ) 1ust 3e .1. 6hat is& fin% f + g such that c 1ust 3e greater than 1& as there is no sufficient n* when c=1. Solution# Consi%er f(n)=n!1 g(n)=n we know f(n) is O(g(n))/ 3oth run in linear ti1e >et f(n).g(n) for all values of n/ no n* we 'ick will hel' with this if we set c=1. 4nstea%& we nee% to 'ick c to 3e so1ething else/ sa2& 2. n!1 ,= 2n for n.=1 ". Write the O( ) run?ti1e of the functions with the following recurrence relations a. 6(n)=3!6(n?1)& where 6(*)=1 Solution# 6(n)=3!3!6(n?2)=3!3!3!6(n?3)=@=3k!6(*)=3k!1& where k=n& so O(n) ti1e. 3. 6(n)=3!6(n52) & where 6(1)=1 Solution# 6(n)=3!3!6(n5")=3!3!3!6(n5A)=@=3k!6(n52k) we want n52k=1 (since we know what 6(1) is)& so k=log2n so 6(n)=3logn!1& so O(logn) ti1e. c. 6(n)=3!6(n?1)!6(n?1) & where 6(*)=1 Solution# 6(n)=3 ! (3!6(n?2)) ! (3!6(n?2))= 3 ! (3!(3!6(n?3))!(3!6(n?3))) ! (3!(3!6(n?3))!(3!6(n?3))) We can tell the ( of calls to 6( ) is %ou3ling ever2 ti1e we e7'an% it further& so it runs in O(2n) ti1e. B. What)s the O( ) run?ti1e of this co%e frag1ent in ter1s of n#

int 7=*/ for(int i=n/i.=*/i??) if((iC3)==*) 3reak/ else 7!=i/ Solution# $t a glance we see a loo' an% it looks like it shoul% 3e O(n)/ it looks like we go through the loo' n ti1es. However& that D3reak) 1akes things a 3it weir%er. Consi%er how the loo' will work for an2 real %ata/ we start at so1e n& count 3ackwar%s until the value is a 1ulti'le of 3& at which 'oint we 3reak. So the loo')s co%e will run at 1ost 3 ti1es (not a function of n)/ so the whole thing is O(1). --Eecall that DC) is the re1ain%er o'erator/ iC3 %ivi%es i 32 3 an% returns the re1ain%er (which will 3e *& 1 or 2).