4/26/2020
Analysis of Algorithm
Department of Computer Science &
Software Engineering
University of Swat
CS Course : Analysis of Algorithm
Course Instructor : Muzammil Khan
Class Task
Group Task
Make group of 2 students
Design an algorithm
That find
Maximum value &
Minimum value
In the array of size n
Also find the time complexity
Best, Worst and Average case running time
Design and Analysis of Algorithm
1
4/26/2020
Chapter 3
Asymptotic Analysis
Order of Growth
We require some simplifying assumptions to ease our
analysis
We do this by
Ignoring the actual cost of each statement, and
Even the abstract costs
Another simplifying assumption is that
It is only the rate of growth or order of growth of a function
that interests us
Design and Analysis of Algorithm
2
4/26/2020
Growth Functions – Algorithm Functions
The behavior of algorithms, for
Different range of problems of different Input Size
Often describe by Standard Mathematical Functions
Mostly referred as Growth Functions
Growth Function classified as
Polynomial
Functions of positive powers of an integer
i.e. f(n) = nc
Where c is positive constant
Examples are: n0.5, n, n2, n5 etc…
Design and Analysis of Algorithm
Growth Functions (Cont...)
Polylograrithmic
Functions are powers of logarithmic functions
i.e. f(x) = (log x)c
Examples are: f(x) = (log x)0.5, log x, (log x)2, (log x)5 etc…
Exponential
Functions are powers of a constant
i.e. f(x) = Ckn
Where k and c are constant
Examples are: f(x) = 2x, 34x etc…
Design and Analysis of Algorithm
3
4/26/2020
Growth Functions (Cont...)
Algorithms are classified as Polynomial, Logarithmic and
Exponential based on
Running time expressed in terms of growth function
Examples
An algorithm with running time T(n) = 2n is said to be
Exponential Algorithm
An algorithm with running time T(n) = n2 is said to be
Polynomial Algorithm
Design and Analysis of Algorithm
Standard Growth Functions
Standard Growth Functions use in Analysis of Algorithm are
Design and Analysis of Algorithm
4
4/26/2020
Growth Functions - Initially
Design and Analysis of Algorithm
Growth Functions – Asymptotically
Or Next Slide
Design and Analysis of Algorithm
5
4/26/2020
Growth Functions – Ranking
Design and Analysis of Algorithm
Comparison of Algorithms
1000 means, 1000 instruction (or n as problem size) in 1 second and so
on
Design and Analysis of Algorithm
6
4/26/2020
Asymptotic Analysis
When we consider rate of growth (algorithm behavior)
We need to consider only
The Leading Term of a formula
Since lower order terms are insignificant in comparison
Consider
An Algorithm1 with running time c1n2 and
Another Algorithm2 with running time c2nlgn
Even if c2 is larger than c1
Once n is large, Algorithm1 is beaten by Algorithm2
Design and Analysis of Algorithm
Asymptotic Analysis (Cont...)
We may try to determine the exact running time of an
algorithm
But the extra precision is not worth the effort
For large inputs
The multiplicative constants and lower order terms are
dominated by the effects of input size
When we are looking at input sizes that are large enough to
make only order of growth of the running time relevant
We are studying asymptotic efficiency of algorithms
We are concerned with
How running time of an algorithm increases with the size of
the input in the limit
Design and Analysis of Algorithm
7
4/26/2020
O-Notation (Worst Case Running Time)
Worst case Running Time
The behavior of the algorithm with respect to the worst
possible case of the input instance
Asymptotically tight upper bound for f(n)
Cannot do worse
Can do better
n is the problem size
It gives us a guarantee that the algorithm will never take any
longer
Represented by
O-Notation (read as big O)
Design and Analysis of Algorithm
O-Notation (Cont...)
If f(n) is a growth function of an algorithm
And g(n) is a function such that
For some positive real constants C and for all n ≥ n0
Then
0 < f(n) ≤ Cg(n)
Then we say
f(n) = O (g(n))
Read as f(n) in Big-Oh of g(n)
The behavior of f(n) and g(n) is shown
Follows that
For n < n0, f(n) is either above or below then g(n)
Design and Analysis of Algorithm
8
4/26/2020
O-Notation (Cont...)
But for all n ≥ n0, f(n) falls consistently below Cg(n)
The function g(n) is said to be asymptotic Upper bound for
f(n)
Design and Analysis of Algorithm
O-Notation (Cont...)
There may be many functions for which g(n) is asymptotically
Upper bound
All such functions are said to be belonging to the group
identified by g(n)
Symbolically we can denote the relationship as
f(n) ϵ O(g(n))
Design and Analysis of Algorithm
9
4/26/2020
O-Notation Example 2n2 Є O(n3)
If f(n) ≤ cg(n), c > 0, ∀ n ≥ n0 then f(n) ∈ O(g(n))
f(n) ≤ cg(n) Definition of O(g(n))
2n2 ≤ cn3 Substitute
2n2/n3 ≤ cn3/n3 Divide by n3
Determine c
2/n ≤ c if ninfinity then 2/n 0
2/n maximum when n=1
0 ≤ 2/1 ≤ c = 2 Satisfied by c=2
Determine n0
0 ≤ 2/n0 ≤ 2 => 0 ≤ 2/2 ≤ n0
0 ≤ 1 ≤ n0 = 1 Satisfied by n0=1
0 ≤ 2n2 ≤ 2n3 ∀ n ≥ n0=1
Design and Analysis of Algorithm
O-Notation Example (Cont...)
3n2 + 10n Є O(n2)
Design and Analysis of Algorithm
10
4/26/2020
O-Notation Example (Cont...)
Show that 2n2+n is in O(n2) by finding c and n0
Show that 1000n2 + 50n is in O(n2) by finding c and n0
Show that n is in O(n lg n) by finding c and n0
Show that lg n is in O(n) by finding c and n0
Show that
5n3+10n ≠ O(n2)
2n3+n ≠ O(n2)
Solve as many example you can
Design and Analysis of Algorithm
Ω -Notation (Best Case Running Time)
Best case Running Time
The behavior of the algorithm with respect to the best
possible case of the input instance
Asymptotically tight Lower bound for f(n)
Cannot do better
Can do worse
n is the problem size
It gives us a guarantee that the minimum time the algorithm
will take
Represented by
Ω –Notation (read as big Omega)
Design and Analysis of Algorithm
11
4/26/2020
Ω -Notation (Cont...)
If f(n) is a growth function of an algorithm
And g(n) is a function such that
For some positive real constants C and for all n ≥ n0
Then
0 < Cg(n) ≤ f(n)
Then we say
f(n) = Ω (g(n))
Read as f(n) in Big-Omega of g(n)
The behavior of f(n) and g(n) is shown
Follows that
For n < n0, f(n) is either above or below then g(n)
Design and Analysis of Algorithm
Ω -Notation (Cont...)
But for all n ≥ n0, f(n) falls consistently above Cg(n)
The function g(n) is said to be asymptotic Lower bound for
f(n)
Design and Analysis of Algorithm
12
4/26/2020
Ω –Notation Example 3n2 + n = Ω(n2)
If cg(n) ≤ f(n), c > 0 and ∀ n ≥ n0, then f(n) ∈ Ω(g(n))
0 ≤ cg(n) ≤ h(n)
0 ≤ cn2 ≤ 3n2 + n
0/n2 ≤ cn2/n2 ≤ 3n2/n2 + n/n2
0 ≤ c ≤ 3 + 1/n 3+1/n = 3
0 ≤ c≤ 3 c=3
0 ≤ 3 ≤ 3 + 1/n0
-3 ≤ 3-3 ≤ 3-3 + 1/n0
-3 ≤ 0 ≤ 1/n0 n0=1 satisfies
Design and Analysis of Algorithm
Ω –Notation Example 1
n2 – 10n Є Ω (n2)
Design and Analysis of Algorithm
13
4/26/2020
Ω –Notation Example 1 (Cont...)
n2 – 10n Є Ω (n2)
Design and Analysis of Algorithm
Ω –Notation Example 2
3n2 – 25n Є Ω (n2)
Design and Analysis of Algorithm
14
4/26/2020
Ω –Notation Example 2 (Cont...)
3n2 – 25n Є Ω (n2)
Design and Analysis of Algorithm
θ-Notation (Average Case Running Time)
Average case Running Time
The expected behavior, when the input is randomly drawn
from a given distribution
Is an estimate of the running time for an “average” input
Computation of running time entails knowing all possible
input sequences
The probability distribution of occurrence of these
sequences
Often it is assumed that all inputs of a given size are equally
likely
Represented by
θ-Notation (read as Theta)
Design and Analysis of Algorithm
15
4/26/2020
θ-Notation (Cont...)
If f(n) is a growth function for an algorithm
And g(n) is a function such that
For all positive real constants C1, C2 and for all n ≥ n0
Then 0 < C2g(n) ≤ f(n) ≤ C1g(n)
C2g(n) is Lower bound and C1g(n) is Upper bound of f(n)
Then we say
f(n) = θ (g(n))
Read as f(n) in Theta of g(n)
The behavior of f(n) and g(n) is shown
Follows that
For n < n0, f(n) is either above or below then g(n)
Design and Analysis of Algorithm
θ-Notation (Cont...)
But for n ≥ n0, f(n) falls consistently between C1g(n) and
C2g(n)
g(n) is said to be asymptotic tight bound for f(n)
Design and Analysis of Algorithm
16
4/26/2020
θ-Notation (Cont...)
There may be many functions for which g(n) is asymptotically
tight bound
All such functions are said to be belonging to the group
identified by g(n)
Symbolically we can denote the relationship as
f(n) ϵ θ(g(n))
Design and Analysis of Algorithm
θ-Notation Example
5n2 – 6n ϵ θ (n2)
To prove that 5n2 – 6n = θ(n2), we need to show that 5n2 – 6n = O(n2)
and 5n2 – 6n = Ω(n2)
First, show 5n2 – 6n = O(n2)
5n2 – 6n <= 5n2 = O(n2) (as LHS is less then RHS by “6n” entity)
for all n >= n1, n1 = 1 and c1 = 5
Now, show 5n2 – 6n = Ω(n2)
5n2 – 6n >= n2
for all n >= n2, n2 = 2 and c2 = 1
Therefore, 5n2 – 6n = θ(n2)
for all n >= n0, n0 = 2, c1 = 5 and c2 = 1
Design and Analysis of Algorithm
17
4/26/2020
θ-Notation Example (Cont...)
5n2 – 19n ϵ θ (n2)
Design and Analysis of Algorithm
o – Notation, Small Oh
If f(n) is a growth function for an algorithm and g(n) is
some function such that
0 ≤ f(n) ≤ C.g(n)
For all C > 0 and all n ≥ n0
Then we say
f(n) = o (g(n))
Read as f(n) in Small-Oh of g(n)
Symbolically
f(n) Є o (g(n))
Design and Analysis of Algorithm
18
4/26/2020
o – Notation Example
n Є o (n2)
Design and Analysis of Algorithm
o – Notation Example (Cont...)
n Є o (n2)
Design and Analysis of Algorithm
19
4/26/2020
ω - Notation, Small Omega
If f(n) is a growth function for an algorithm and g(n) is
some function such that
f(n) ≥ C.g(n) or C.g(n) ≤ f(n)
For all C > 0 and all n ≥ n0
Then we say
f(n) = ω (g(n))
Read as f(n) in Small-Omega of g(n)
Symbolically
f(n) Є ω (g(n))
Design and Analysis of Algorithm
Using Limit O - Notation
Design and Analysis of Algorithm
20
4/26/2020
Using Limit o - Notation
Design and Analysis of Algorithm
Asymptotic Notation Constants
If C is a constant then by convention
O(c) Є O(1)
θ (c) Є θ (1)
Ω(c) Є Ω(1)
The convention implies that
The running time of an algorithm which does not depends on
input size can be expressed in any of the above ways
And
O(c.f(n)) Є O(f(n))
θ (c.f(n)) Є θ (f(n))
Ω(c.f(n)) Є Ω(f(n))
The relation ship implies that the multiplier constant can be ignore
Design and Analysis of Algorithm
21
4/26/2020
Ɵ - O - Ω Relation
Design and Analysis of Algorithm
o - O Relation
Design and Analysis of Algorithm
22
4/26/2020
ɷ - Ω Relation
Order Theorem
Design and Analysis of Algorithm
2nd Approach to Solve T(n) Asymptotically
Another approach to find o, O, Q, W and w
lim
n
[f(n) / g(n)] = 0 f(n) o(g(n))
lim
n
[f(n) / g(n)] < f(n) O(g(n))
0 < nlim
[f(n) / g(n)] < f(n) Q(g(n))
0 < nlim
[f(n) / g(n)] f(n) W(g(n))
lim
n
[f(n) / g(n)] = f(n) w(g(n))
lim [f(n) / g(n)] undefined can’t say
n
Design and Analysis of Algorithm
23
4/26/2020
Some Important Properties
Transitivity
f(n) = Q(g(n)) & g(n) = Q(h(n)) f(n) = Q(h(n))
f(n) = O(g(n)) & g(n) = O(h(n)) f(n) = O(h(n))
f(n) = W(g(n)) & g(n) = W(h(n)) f(n) = W(h(n))
f(n) = o (g(n)) & g(n) = o (h(n)) f(n) = o (h(n))
f(n) = w(g(n)) & g(n) = w(h(n)) f(n) = w(h(n))
Reflexivity
f(n) = Q(f(n))
f(n) = O(f(n))
f(n) = W(f(n))
Design and Analysis of Algorithm
Some Important Properties (Cont…)
Symmetry
f(n) = Q(g(n)) iff g(n) = Q(f(n))
Complementarily
f(n) = O(g(n)) iff g(n) = W(f(n))
f(n) = o(g(n)) iff g(n) = w((f(n))
f(n) is
Monotonically increasing if m n f(m) f(n).
Monotonically decreasing if m n f(m) f(n).
Strictly increasing if m < n f(m) < f(n).
Strictly decreasing if m > n f(m) > f(n).
Design and Analysis of Algorithm
24
4/26/2020
End
End of the Chapter
You may have quiz next week
Design and Analysis of Algorithm
25