0% found this document useful (0 votes)
108 views

Asymptotic Analysis PDF

This document discusses algorithm analysis and asymptotic analysis, explaining how to determine the upper and lower time bounds of algorithms (O, Ω, Θ notation). It covers analyzing loops and recursion using techniques like substitution method, unrolling, recurrence trees, and the master method. The goal is to classify algorithms into asymptotic complexity classes like O(n), O(n^2), O(log n) to understand how their running time scales with input size.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Asymptotic Analysis PDF

This document discusses algorithm analysis and asymptotic analysis, explaining how to determine the upper and lower time bounds of algorithms (O, Ω, Θ notation). It covers analyzing loops and recursion using techniques like substitution method, unrolling, recurrence trees, and the master method. The goal is to classify algorithms into asymptotic complexity classes like O(n), O(n^2), O(log n) to understand how their running time scales with input size.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Algorithm Analysis

eka
What define performance?

Architecture

Design of a program
Asymptotic Analysis

Define bound of its run-time performance.

Defined in term of its operation scaled with input.
– f(n): scaled linearly
– n(n2): Scaled quadratically and so on

Considering best case, average case, and worst
case
Asymptotic Notation

O Notation

Ω Notation

Θ Notation
Big Oh, O

Define upper bound of an algorithm’s running
time.

Ο f n = { g n : there exists c > 0 and n0 such that f n ≤ c.g n for all n > n0. }
Omega, Ω

Define lower bound of an algorithm’s running
time

Ω f n ≥ { g n : there exists c > 0 and n0 such that g n ≤ c.f n for all n > n0. }
Theta, Θ

Define Both lower bound and upper bound of
an algorithm’s running time.

θ f n = { g n if and only if g n = Ο f n and g n = Ω f n for all n > n0. }


Common Notation
Asymptotic Bound

Worst case situation?

Average case: what are
all the case?

Best case situation?
Merge Sort

Merge sort is a
stable algorithm

It doesn’t has
worst case,
average case,
best case
Analysis of Loops

O(1): Set of non-recursive or non-loop
statements

O(n): a loop is considered as O(n)
Analysis of Loops

O(nc): if a loop is nested
Analysis of Loops

O(log n): if the loop is divided
Analysis of Loops

O(log n Log n): if the loop is divided/reduced
exponentially by a constant

Remember recursive equals loop, imagine the
same situation in recursive case
Binary Search
Analysis of Recursion

Substitution Method

Unrolling/Recursion Tree

Master Method
Substitution Method

Consider the recurrence of T(n) = 2T(n/2) + n

First, make a guess
– T(n) = O(n log n)

Thus use induction to prove T(n) <= cn log n
Exercise

What is the asymptotic of recurrence T(n) =
7T(n/7) + n
Unrolling

Selection sort consume T(n) = T(n-1) + cn

By unrolling we have
– T(n) = cn + c(n-1) + c(n-2) + … + c

By arithmetic rule, this was at most cn 2, O(n2)

Remove the data by half then it was at least (n/2)(cn/2) =
cn2/4, Ω(n2)

Therefore it is Θ(n2)
Recurrence Tree

Draw a recurrence tree in full form, then sum
the work done at all levels

Consider T(n) = T(n/4) + T(n/2) + cn2

To know the value of tree
nodes level by level

T(n) = c(n^2 + 5(n^2)/16 +
25(n^2)/256) + .…

The above series is
geometrical progression with
ratio 5/16.

We get the sum as (n2)/(1 -
5/16) which is O(n2)
Master Theorem

Recurrence of the form
– T(n) = aT(n/b) + cnk

Where a, b, c, and k are all constant solves to:
– T(n) = Θ(nk) if a < bk
– T(n) = Θ(nk log n) if a = bk
– T(n) = Θ(n^logba) if a > bk
Exercise

Using master theorem solve

T(n) = 3T(n/2) + n2

T(n) = T(n/2) + 2n

T(n) = 16T(n/4) + n
Exercise

What is the time complexity of this codes?

You might also like