Asymptotic Analysis and Time Complexity: Analysis of Algorithm Lecture # 2 Mariha Asad
Asymptotic Analysis and Time Complexity: Analysis of Algorithm Lecture # 2 Mariha Asad
Time Complexity
Analysis of Algorithm
Lecture # 2
Mariha Asad
Big-Oh (O)
Example 1: Let a function f(n) = 2n+3, find out the upper bound of this
function
f(n) =O( g(n) )if there exist a positive constants c and n0 such that
0 ≤ f(n) ≤ c g(n) for all n ≥ n0
2n+3 ≤ 9* n2
n ≥1 f(n) = O(n)
f(n) = O(n2)
2n+3 ≤ 5n 2n + 3 ≤ 10 * n for all n ≥1 f(n) = O(n3)
f(n) = O(logn)
c = 10 , g(n) = n
So f(n) = O(n)
Big-Omega(Ω)
Example : Let a function f(n) = 2n+3, find out the lower bound of this
function
f(n) = Ω(g(n)) if there exist positive constants c and n0 such that
0 ≤ c g(n) ≤ f(n) for all n ≥ n0
2n +3 ≥ 5* n
2n +3 ≥ 1* n for all n ≥1 f(n) = Ω(n)
n=2; 4+ 3 ≥ 2*2 c = 1 , g(n) = n f(n) = Ω(logn)
n=3; 6+3 ≥ 2*3 f(n) = Ω(n2)
n=4; 8+3 ≥ 2*4 So f(n) = Ω(n)
Big-Theta (θ)
Example : Let a function f(n) = 2n+3, find out the tight bound of this
function
f(n) = θ(g(n)) if there exist positive constants c1,c2 and n0 such that
0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0
1 * n ≤ 2n + 3 ≤ 10 * n for all n ≥ 1 We cannot use any other
When c1 = 1, c2 = 10, g(n) = n thing except n here
f(n) = θ(n)
So f(n) = θ(n) f(n) = θ(n2)
Example 2
Let a function f(n) = 2n2 + 3n + 4 find out upper, lower and tight bound
of it
For Big-Oh (O)
2n2 + 3n + 4 ≤ 2n2 + 3n2 + 4n2
2n2 + 3n + 4 ≤ 9 n2 for all n ≥ 1
c = 9 , g(n)= n2
So f(n) = O(n2)
Example 2
For Big-Omega (Ω):
2n2 + 3n + 4 ≥ 1 * n2 for all n ≥ 1
c=1, g(n) = n2
So f(n) = Ω(n2)
For Big-Theta (θ) :
1 * n2 ≤ 2n2 + 3n + 4 ≤ 9 * n2 for all n ≥ 1
When c1 = 1, c2=2, g(n) = n2
So f(n)= θ(n2 )
Example 3
Let a function f(n) = n2logn + n find out upper, lower and tight bound of
it
1 * n2 logn ≤ n2logn + n ≤ 10 n2 logn for all n ≥ 1
When c1= 1, c2 =10, g(n) = n2 logn
So f(n)= θ(n2 logn )
f(n)= o(n2 logn)
f(n) = Ω(n2 logn)
Example 4
f(n) = n!
1 ≤ n! ≤ n*n*n*…*n for all n ≥ 1
1 ≤ 1*2*3*4*….*n ≤ n*n*n*…*n
1 ≤ n! ≤ nn
When c1= 1, c2 =1, g(n) = ? We are not getting a
single value of g(n) so
So f(n)= O(nn) we cannot find θ
f(n) = Ω(1)
Example 5
f(n) = logn!
1 ≤ logn! ≤ log(n*n*n*…*n) for all n ≥ 1
1 ≤ log(1*2*3*4*….*n) ≤ log(n*n*n*…*n)
Lognn = nlogn
1 ≤ log n! ≤ lognn