Algo - Lecure3 - Asymptotic Analysis
Algo - Lecure3 - Asymptotic Analysis
Algo - Lecure3 - Asymptotic Analysis
ASYMPTOTIC ANALYSIS
Tanjina Helaly
Algorithms
MEASURE TIME TO RUN AN ALGORITHM
One naïve way is to implement the algorithm and
run it in a machine.
This time depends on
the speed of the computer,
the programming language,
the compiler that translates the program to machine code.
the program itself.
And many other factors
So, you may get different time for the same algorithm.
Hence, not a good tool to compare different
algorithm.
To overcome these issues, need to model Time
complexity.
TIME COMPLEXITY
Developing a formula for predicting how fast an
algorithm is, based on the size of the input
To compare algorithms
Measure of efficiency/goodness of algorithm
3 types of complexity
Best case
Lower bound.
Minimum number of steps/operations to execute an
algorithm.
Measure the minimum time required to run an algorithm.
Main focus
execution
Average case
the amount of some computational resource (typically time)
used by the algorithm, averaged over all possible
inputs.
Difficult to determine
For n = 1000000
2n2 = 2000000000000
10 n = 10000000
Order of growth:
How the time grow with input size
Leading term/Highest order term in the equation
MORE TO THINK
Tell me what types of equation are the following
2?
f(n) = 2n2
f(n) = 5n2
Obviously
2n2 <= 2n2 + 10n + 3 for any n
c1 = 2
Obviously
5n2 <= 5n2 + 6n + 35 for any n>0
c1 = 5
n! ≥ 2n ≥ n3 ≥ n2 ≥ n log n ≥ n ≥ log n ≥ c
TRY THESE
Is 2n+1 = O(2n)?
Is 22n = O(2n)?
For each of the following pairs of functions, either f(n)
is in O(g(n)), f(n) is in Ω(g(n)), or f(n) = Θ(g(n)).
Determine which relationship is correct and briefly
explain why.
f(n) = logn2; g(n) = logn + 5
f(n) =√n; g(n) = logn2
f(n) = log2 n; g(n) = logn
f(n) = n; g(n) = log2 n
f(n) = n log n + n; g(n) = logn
f(n) = 10; g(n) = log10
f(n) = 2n; g(n) = 10n2
f(n) = 2n; g(n) = 3n
THE BIG OH NOTATIONS
THE BIG OH NOTATION
3n2 − 100n + 6 = O(n2), Because for c = 3, 3n2 > 3n2 − 100n + 6;
3n2 − 100n + 6 = Ω(n2), Because for c = 2, 2n2 < 3n2 − 100n + 6 when
n > 100;
3n2 − 100n + 6 ≠ Ω(n3), Because for c = 3, 3n2 − 100n + 6 < n3 when n > 3;
3n2 − 100n + 6 = Ω(n), Because for any c, cn < 3n2 − 100n + 6 when
n > 100c;