Transcript

Complexity Analysis : Asymptotic Analysis

Complexity Analysis : Asymptotic AnalysisNattee Niparnan1RecallWhat is the measurement of algorithm?How to compare two algorithms?Definition of Asymptotic Notation2Today TopicFinding the asymptotic upper bound of the algorithm3Interesting Topics of Upper BoundRule of thumb!We neglectLower order terms from additionE.g. n3+n2 = O(n3)ConstantE.g. 3n3 = O(n3)Remember that we use = instead of (more correctly) 4Why Discard Constant?From the definitionWe can use any constant

E.g. 3n = O(n)BecauseWhen we let c >= 3, the condition is satisfied5Why Discard Lower Order Term?Considerf(n) = n3+n2 g(n) = n3If f(n) = O(g(n))Then, for some c and n0c * g(n)-f(n) > 0Definitely, just use any c >1

6Why Discard Lower Order Term?Try c = 1.1

Does 0.1n3-n2 > 0 ? It is when 0.1n > 1E.g., n > 10

1.1 * g(n)-f(n) = 0.1n3-n20.1n3-n2 > 00.1n3 > n20.1n3/n2 > 10.1n > 17Lower Order only?In fact,Its only the dominant term that count

Which one is dominating term?The one that grow faster

Why?Eventually, it is g*(n)/f*(n)If g(n) grows faster, g(n)/f*(n) > some constantE.g, lim g(n)/f*(n) infinityThe non-dominant termThe dominant term8What dominating what?nanb (a > b)n log n nn2 log nn log2 ncnncLog n1nlog nLeft side dominates9Putting into PracticeWhat is the asymptotic class of

0.5n3+N4-5(n-3)(n-5)+n3log8n+25+n1.5

(n-5)(n2+3)+log(n20)

20n5+58n4+15n3.2*3n210Putting into PracticeWhat is the asymptotic class of

0.5n3+N4-5(n-3)(n-5)+n3log8n+25+n1.5

(n-5)(n2+3)+log(n20)

20n5+58n4+15n3.2*3n2O(n4)O(n3)O(n5.4)11Asymptotic Notation from Program FlowSequenceConditionsLoopsRecursive Call12SequenceBlock ABlock Bf (n)g (n)f(n) + g(n) =

O(max (f(n),g(n))13ExampleBlock ABlock BO(n)O(n2)O(n2)14ExampleBlock ABlock B(n)O(n2)O(n2)15ExampleBlock ABlock B(n)(n2)(n2)16ExampleBlock ABlock BO(n2)(n2)(n2)17ConditionBlock ABlock Bf (n)g (n)O(max (f(n),g(n))18Loopsfor (i = 1;i


Recommended