HW1 SolutionKey
HW1 SolutionKey
Considerthealgorithm
representedbythefollowingprogramfragment.
MYSTERY(intx,intn)
{
intsum=0
if(x<=100)then{
while(x>10){
x=x1
}
}else{
for(inti=1i<=ni++){
sum=sum+i+x
}
}
returnx+sum
}
a)(2points)Whatvalue(asafunctionofxandn)doesMYSTERYcompute?
b) (2 points) Assume that the value of x is constant, but unknown. Use only arithmetic
operations as primitive operations. Compute the worstcase running time of MYSTERY
asafunctionofxandn.
c) (2 points) Assume that x remains constant while n goes to infinity. Derive a tight,
bigOh expression (dependent on x) for the running time of MYSTERY. Justify your
solution.
d) (2 points) Assume that n remains constant while x goes to infinity. Derive a tight,
bigOh expression (dependent on n) for the running time of MYSTERY. Justify your
solution.
Solution:
n(n+1)
a) f (x, n) = {xf orx < 1010f or10 x 100 2 + x(n + 1)otherwise
b.)Wecountthefollowingarithmeticoperations:
i.
ii.
iii.
iv.
x = x 1
i++
sum=sum+i+x
x+sum
Thentheworstcaserunningtimewouldbe:
T (f (x, n)) = {1f orx < 10x 9f or10 x 1002n + 1otherwise
Question2:
Solution:
a.TherunningtimeisdeterminedbytheforloopfromlinesH2tolineH4.
Asymptoticallywegetarunningtimeof(n).
H1y=0
H2for(i=ni>=0i)
H3
y*=x
H4y+=a[i]
b.Morenaivepseudocode.
N1y=0
N2for(i=ni>=0i)
N3z=a[i]
N4for(j=0j<ij++)
N5z*=x
N6y+=z
TherunningtimeisdominatedbythenestedforloopinlinesN4andN5.Asymptotically,
wegetarunningtimeof
(n2).Thisisalargerasymptoticrunningtimethan
Hornersrule.
c.
Initialization:Priortothefirstloopiteration,wehavei=n,sothatthesumgoesfrom
k=0tok=n(n+1)=1,anditdoesnotcontainanytermandsoequalsto0.Thisis
identicaltotheinitializationvalueofy.
Maintenance:Assumethatbeforetheiterationi>i1wehave
InlineH3wemultiplyybyx,resultingin
InlineH4weadda[i]toy,resultingin
Thisequatesto
Hencetheloopinvariantispreservedatthebeginningofthenextiterationwithi1.
Termination:Thealgorithmterminateswheni=1.Theloopinvariantguaranteesthat
atthistimewehave
d.Firstly,lineH1guaranteesthattheloopinvariantisinitializedcorrectlyfori=n.When
theloopinvariantismaintainedwhiletheloopinlineH2simplycountsdownuntilthe
algorithmterminateswithi=1.Atthistime,theloopinvariantguaranteesthatyis
identicaltothevalueofthepolynomewehadtoevaluate,i.e.
3. Purpose: practice working with asymptotic notation. Please solve (12 points)
32 on page 61 , (8 points) 34 [ac] on page 62. For full marks justify your
solutions.
32
Solution:sorryforthebaddisplayofformulasinthetable.
Lgkn
Nk
Yes
No
No
No
No
Nk
Cn
Yes
Yes
No
No
No
sqrt(n
nsin(n
No
No
No
No
No
2n
2n/2
No
No
Yes
Yes
No
nlgc
Clgn
Yes
No
Yes
No
Yes
lg(n!)
lg(nn)
Yes
No
Yes
No
Yes
a) ApplyLHospitalsrule
k
lnn
lg k n = (log 2 n)k = ( ln2
)
k
1
limn lgnn = ( ln2
) limn
k
1
= ( ln2
) limn kk!n =
(lnn)k
k 1
1
= ( ln2
) limn k(lnn)
k!
limn n1
(ln2)k k
= 0
b) ApplyLHospitalsrule
k 1
k!
cn (lnc)k
k!
1
k limn cn
(lnc)
= 0
c) The value of sin n fluctuates between 1 and 1. Therefore, this function is not
comparable.
d) ApplyLHospitalsrule
n
n
i=1
n
i=1
Because
34
Solution:
n
i=1
lg(i)
n
i= n2 +1
lg(i)
n
i= n2 +1
lg( n2 ) = ( n2
1)lg( n2 ) ,lg(n!)isBigomegaoflg( nn ) .
a)f(n)=O(g(n))impliesg(n)=O(f(n))
Letf(n)=nandg(n)= n2 ,thenwehaven=O( n2 ) but n2 O(n).
=>False
b)f(n)+g(n)= (min(f(n),g(n)))
Letf(n)=nandg(n)= n2 ,thenmin(f(n),g(n))= {n2 , n < 1n, n 1} .
Therefore,for n 1 ,wehavemin(f(n),g(n))=n= (n) .
However,f(n)+g(n)=n+ n2 (n) .
=>False
c)f(n)=O(g(n))implieslg(f(n))=O(lg(g(n))),wherelg(g(n)) andf(n) a 1forallsufficiently
largen.
f(n)=O(g(n))impliesthatforsomec>0and n n0 0, wehave 0 f (n) cg(n).
If 0 f (n) cg(n) istrue,then lg(f (n)) lg(cg(n) )istrueandalso lg(f (n)) lgc + lg(g(n)) is
true.
Since lg(g(n)) 1, lgc * lg(g(n)) lgc.
lg(f (n)) lgc + lg(g(n)) lgc * lg(g(n)) + lg(g(n)) = (1 + lgc)lg(g(n))
Therefore, lg(f (n)) = O(lg(g(n))) .
=>True
a) (5points)Puttheminincreasingorderofasymptoticgrowth.
Solution:
The order would be: 2n, lg(n), sqrt(n), n/2, n2 n+5, 4lg(n), n2 +nlg5(n), n2 lg(n), n3 ,
n3 lg3(n)
b) (2point)TheorderisNOTunique.Whichfunctionscanbeexchanged?
Solution:
4lg(n)=22*lg(n)=2lg(n)*2=n2,So,4lg(n),n2 +nlg5(n),n2 n+5canbeexchanged.
k1
The algorithm calculates a(a0 2 ) ,a(a0 2 +a1 2 ) ,... a(ak 2 +ak1 2 +. . . +a0 2 )
and stores these values in the variable result. For example,for n = 5 =
1 22 + 0 21 + 1 20 , algorithm F astP ower(a, 5) computes successively:
Initialization:
result = 1, n = 5, power of 2 = a
for-loop:
i = 1, result = a, n = 2, power of 2 = a2
i = 2, result = a3 , n = 1, power of 2 = a4
i = 3, result = a5 , n = 0, power of 2 = a8 The final value is in the result
variable as shown above.
In an alternate recursive version the recurrence could be formulated as:
T (n) =
(1),
T ( n2 ) + (1),
if n = 1 or n = 0
otherwise
(1)