Asymptotic Notations and Complexity Analysis
Asymptotic Notations and Complexity Analysis
By
Amit Sharma
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Complexity of Algorithm
• Asymptotic Notations
• Review Questions
Basic Terminology
• Algorithm: is a finite step by step list of well-defined
instructions for solving a particular problem.
Specification of Output
Specification of Input
(e.g. any sequence of natural Algorithm as a function of Input
numbers) (e.g. sequence of sorted
natural numbers)
Characteristics of Good Algorithm
• Efficient
• Running Time
• Space used
• Worst Case: The maximum value of f(n) for any possible input.
Example:
• 50n log n is O(n log n)
• 8n2 log n + 5 n2 + n is O(n2 log n)
Big-Omega (Ω) Notation
• Asymptotic lower bound
#include <iostream>
int main(){
int c = 4;
cin>>n;
int d = c*5;
}
Reviewing Complexity
#include <iostream>
int main(){
int c = 4; 1
cin>>n; 1
int d = c*5; 1
}
O(1+1+1) = constant time
Reviewing Complexity
int a = 0, b = 0;
for (i = 0; i < N; i++) {
a = a + rand();
}
for (j = 0; j < M; j++) {
b = b + rand();
}
int a = 0; 1
int b = 0; 1
for (i = 0; i < N; i++) { n+1
a = a + rand(); n
}
for (j = 0; j < M; j++) { m+1
b = b + rand(); m
}
total steps : 4+2n+2m
=2(n+m) +4
ignore constant ->O(N+M)
Reviewing Complexity
--
p=0;
for(i=1; p<=n;i++){
p = p+i;
}
Reviewing Complexity
mlogk<logn
m<logn/logk
m<logkn
Analysis of Algorithms
• for (int i = 1; i <= n; i += c)
{ // some O(1) expressions }