Design & Analysis of Algorithms - Topic 3 - Time Complexity Basics
Design & Analysis of Algorithms - Topic 3 - Time Complexity Basics
ALGORITHMS
Topic 3
Asymptotic Notations,
Time Complexity BASICS
■ Hence f(n)=O(g(n))=O(n)
Big-Omega (Ω) Notation
■ The Big Ω notation defines a lower bound of an
algorithm.
■ It is the also called as tight bound & often called the Exact
Bound.
Ω(n)
ω(n)
Growth of functions
Usually growth of functions
can be categorized into the
following:
– Constant time
– Logarithmic time
– Linear time
– N-logarithmic time
– Polynomial time
– Exponential time
– Factorial time
Remarks
■ Most of the time, we are concerned with the
upper bound of the worst case complexity of
algorithms.
…
– Best Case Analysis 1 = (1)
– Worst Case Analysis n = (n)
– Average Case Analysis ≈ n/2+1 = (n) num_n
Time Complexity of Algorithms
■ We have already talked about how to get the time
complexity function of any algorithm.
A() A(n)
{ {
For j = 1 to n If (-----)
Print “Hello” A(n/2)
}
}
Time Complexity (Iterations)
For i=1 to n T(n)=n
<computational instruction>
=O(n)
-----------------------------------------------------------
For i=1 to n
For j=1 to n
T(n)=n.n=n 2
=O(n )
<computational instruction>
2
i 1 2 3 4 5 6 7 … n
J n n n n n n n … n
Time Complexity (Iterations)
For i=1 to n
T(n)=n.n.n=n 3
For j=1 to n
For k=1 to n
=O(n )
3
<computational instruction>
----------------------------------------------------------------
For i=1 to n
For j=1 to n
T(n)=n.n.n.n=n
For k=1 to n =O(n )
4
For l=1 to n
<computational instruction>
Time Complexity (Iterations)
i=1 T(n)=k
s=1
While(s<=n)
i=i+1
s=s+i
<computational
instruction>
S 1 3 6 10 15 21 28 … n
i 1 2 3 4 5 6 7 … k
Things to remember
Sum of 1st m natural numbers=m(m+1)/
T(n)=O() i <=n
2
i<=
Time Complexity (Iterations)
T(n)=100+200+…+n.10
For i=1 to n =100(1+2+…+n)
For j=1 to i =100.n(n+1)/2
For k=1 to 100 =O(n2)
<computational
instruction>
i 1 2 3 4 … n
j 1 times 2 times 3 times 4 times … n times
k 1x100 2x100 3x100 4x100 … nx100
times times times times times
Things to remember
Sum of squares of 1st m natural numbers=m(m+1)(2m+1
<computational instruction> Or
k=log2n
T(n)=O(logmn)
Time Complexity (Iterations)
For i=n/2 to n
T(n)=(n/2)(n/2)log2n
For j=1 to n/2 =O(n2.log2n)
For k=1 to n & k=k*2
<computational instruction>
-----------------------------------------------------------
For i=n/2 to n T(n)=(n/2).log2n.log2n
For j=1 to n & j=2*j =O(n.(log2n)2)
For k=1 to n & k=k*2
<computational instruction>
Time Complexity (Iterations)
While(n>1)
n=n/2
T(n)=O(log 2 n)
<computational instruction>
i = 1 to n i = n to 1
←same→
i=i*2 i=i/2
where = 0.5772156649…
i 1 2 3 … n
j n times n/2 n/3 … n/n
times times times
End of Lecture
THANK YOU