Unit 1asymptotic Notation
Unit 1asymptotic Notation
Structures
Unit 1: Asymptotic analysis (Big-O notation)
Algorithm: Outline, the essence of a computational procedure, step-by-step
instructions
Algorithmic Problem
Specification of Specification of
input ? output as a
function of input
( Infinitely many correct algorithms for the same algorithmic problem )
Constant Ο(1)
Logarithmic Ο(log n)
Linear Ο(n)
Log Linear Ο(n log n)
Quadratic Ο(n2)
Cubic Ο(n3)
Polynomial nΟ(1)
Exponential 2Ο(n)
Asymptotic Notation
e.g. If f(n) = n2 + 3n, then as n becomes very large, the term 3n becomes
insignificant compared to n2.
The function f(n) is said to be "asymptotically equivalent to n2, as n → ∞".
Commonly used asymptotic notations to calculate the running time
complexity of an algorithm are
the best case performance of an algorithm is generally not useful, the Omega notation is the least used notation
among all three.
Θ Notation – bounds a functions from above and below
It measures the average case time complexity
Basic Rules for calculating
complexity
1. Nested loops are multiplied together.
2. Sequential loops are added.
3. Only the largest term is kept, all others are dropped.
4. Constants are dropped.
5. Conditional checks are constant (i.e. 1).
Measuring Efficiency of an algorithm
We are more interested in finding out the Worst Case Complexity or Big O
Analysis of Algorithms
• // Here c is a constant
for (int i = 1; i <= c; i++)
{
// some O(1) expressions
}
Reviewing Complexity
#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
int a = 0;
for (i = 0; i < N; i++) {
for (j = N; j > i; j--) {
a = a + i + j;
}
}
Reviewing Complexity
--
for(i=1; i<=n;i=*2){
stmt;
}
• i=1 1*2 Assume i>=n
• i=2 2*2=22 2k >=n
• i=3 22* *2=23 2k =n
• ---- k=o(logn)
• ----
• ----
• 2k
Reviewing Complexity
--
for(i=n; i>=1;i=i/2){
stmt;
}
Reviewing Complexity
--
p=0;
for(i=1; p<=n;i++){
p = p+i;
}
• i=1 p=0+1 Assume p>=n
• i=2 1+2 k(k+1)/2
• i=3 1+2+3
k2>n
• ---- k=o(sqrt(n))
• ----
• ----
• k k(k+1)/2
Reviewing Complexity