0% found this document useful (0 votes)
39 views24 pages

Big O Notation: Tight Upper Bound

The document provides an overview of asymptotic notations used in analyzing algorithms, including Big O, small o, Omega, and Theta notations, along with examples of their application. It also introduces recurrence relations, their definitions, and methods for solving them, such as the substitution method and the Master method. Additionally, it covers specific examples like the Fibonacci sequence and the Merge Sort algorithm, illustrating their time complexities.

Uploaded by

vaxshgadd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views24 pages

Big O Notation: Tight Upper Bound

The document provides an overview of asymptotic notations used in analyzing algorithms, including Big O, small o, Omega, and Theta notations, along with examples of their application. It also introduces recurrence relations, their definitions, and methods for solving them, such as the substitution method and the Master method. Additionally, it covers specific examples like the Fibonacci sequence and the Merge Sort algorithm, illustrating their time complexities.

Uploaded by

vaxshgadd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA STRUCTURES

CSO102
WEEK-2 (DRAFT)
ASYMPTOTIC NOTATIONS

❑ Big ‘O’ Notation:(Tight Upper Bound)


Consider two functions f and g these functions are from N to R*.
For an Algorithm of input size n f(n) is said to be O(g(n)) if there are
two constants c,n0 such that for c>0 and n ≥n0 ,c g(n) ≥ f(n)
and = constant(>0) for all n greater than n0

O(1) < O(log n) <O(n) <O(n log n)<O( n2 ) <O( n3 ) <O(kn )


❑ Small ‘o’ Notation:(Loose Upper Bound)
Consider two functions f and g these functions are from N to R*.
For an Algorithm of input size n f(n) is said to be o(g(n)) if there are
two constants c,n0 such that for c>0 and n ≥n0 ,c g(n) ≥ f(n) and
=0
ASYMPTOTIC NOTATIONS
ASYMPTOTIC NOTATIONS
ASYMPTOTIC NOTATIONS

= O (n^m)
ASYMPTOTIC NOTATIONS

❑ ‘𝛺’ Notation:(Tight Lower Bound)


Consider two functions f and g these functions are from N to R*.
For an Algorithm of input size n f(n) is said to be O(g(n)) if there are
two constants c,n0 such that for c>0 and n ≥n0 ,f(n) ≥ cg(n)
and = constant(>0) for all n greater than n0

❑ ‘’𝟂’
Notation:(Loose Lower Bound)
Consider two functions f and g these functions are from N to R*.
For an Algorithm of input size n f(n) is said to be o(g(n)) if there are
two constants c,n0 such that for c>0 and n ≥n0 ,f(n) ≥ cg(n) and
=∞
ASYMPTOTIC NOTATIONS

= Ω (n^m)
ASYMPTOTIC NOTATIONS
ASYMPTOTIC NOTATIONS

❑ ‘𝛉’ Notation:
❑ If the Maximum running time of algorithm is equal to minimum
running time then we use this notation.
❑ Ex: f(n)= 2n+6

=O(n)= 𝛺(n)
Therefore f(n)= 𝛉(n)

‘’
ASYMPTOTIC NOTATIONS

= θ (n^m)
EXAMPLES

Ex1: Let f(n)= 3n+5 <= 4n, n>=5


and =3/4. (g(n)=n) ,c=4, n0=5.

If g(n)= n2 then =0 ,c=1, n0 =5.


Therefore

f(n) =O(n)=o( n2 ) = 𝛺(n)= 𝟂(1)

Ex2:Quadratic Function
f(n) =27 n2 +16 n+25
<= 27 n2 +16 n+ n for n>=25
<= 27 n2 +17 n for n>=25
<= 27 n2 + n2 for n>=25
EXAMPLES

■ Therefore f(n) =O( n2 )


Cubic Function:
f(n)=3 n3 +4 n
<=3 n3 + n3 for all n>=4
<= 4 n3
Therefore f(n)= O( n3 ) , n0 =4
Exponential Function :
f(n) = 2n + 6n
<= 2n + 6 2n for all n>=1
<= 7. 2n
Therefore f(n)=O(2n)
EXAMPLES

Ex : Logarithmic Function f(n)= O(log n!)


log(n!) =log(1.2.3. .... n)
=log1+log 2+........+log n
<=log n+ log n+ logn +....+logn
<=nlogn
Therefore f(n)= O(nlogn)
log(n!)=log(1.2.3......n)
>=log n/2+log n/2+log n/2+.......logn/2 (n/2 times)
>=n/2 log n/2
f(n)= 𝛺(nlogn)
Therefore f(n)= 𝛉(nlogn)
RECURRENCE RELATIONS
TOPICS

◼ Definition of Recurrence Relation


◼ Problems
◼ Homogenous Relation
◼ In-Homogenous Relation
◼ Change of Variable Me
◼ Recursion Tree Method
◼ Master Method
◼ Some Examples of Master Method
DEFINITION OF RECURRENCE RELATION

◼ An Advance Counting Technique


◼ Counting Problems can be Solved by Finding Relationships

Example: If a colony begins with five bacteria and the number of


bacteria in the colony doubles every hour ,how many will be
present after n hours.
Sol: an =2an-1 , a0=5 .
◼ A sequence is a discrete structure used to represent ordered list of
elements.
◼ Def : A Recurren​​ce relation for the sequence {an} is a equation that
n

expresses an in terms of one or more of the previous terms of the


sequence , namely a0,a1,.....,an-1 for all integers n with n > n0,where
n0 is a non negative integer.
Problems

◼ Tower of Hanoi
– Hn = 2Hn-1+ 1
– H1=1
◼ To Count Bit Strings of Specified Length
– Number of Bit String of length n that do not have 2 consecutive 0’s
– Write recurrence relations we will assume that n> 3 so bit string
has at least 3 bits
– End with 1 |any bit string of length n-1 and no 2 consective 0’s|
– End with 0 |any bit string of length n-2 and no 2 consective 0’s|10
– An=an-1+an-2 for n> 3
– Initial Conditions a1=2,a2=3
Solutions to some Recurrence Relations

◼ Substitution Method:
– Problem 1:
•tn=tn-1+n n>2, t1=1
•tn=tn-2+n-1+n
• =1+2+3+.................+n=O(n2)
– Problem 2:
•tn=tn/2+n
• =tn/4+n/2+n , k=log(n)
• =1+2+..........+2k=2k+1-1=O(n)
Homogenous Recurrence

◼ a0tn+a1tn-1+------+aktn-k=0
k
◼ tn = ∑ ci r ni
i= 1
◼ ci’s are determined by intial conditions
– Let tn=xn ,where x is a constant
– a0xn+a1xn-1+-----+akxn-k=0
– X=0 (trivial solution )
– If r1,r2,r3,..rk, are k roots of above characteristic equation.
◼ Problem 1:
– tn-3tn-1-4tn-2=0,t0=0 and t1=1
– x2-3x-4=0 => tn=c1(-1)n+c24n
T(n) = O(4n)
Homogenous Recurrence

F(n) = F(n-1) + F(n-2) with F(0)=0, F(1)=1

int fibbonacci(int n) {
if(n == 0){
return 0;
} else if(n == 1) {
return 1;
} else {
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}

Time Complexity:~ O(1.6n)


Homogenous Recurrence

F(n) = F(n-1) + F(n-2) with F(0)=0, F(1)=1


int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;

nextTerm = t1 + t2; Time Complexity: O(n)


Merge Sort
Merge Sort
step: start
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)

step 4: Stopmid, right)


step 4: Stop
Example: T(n) = 2T(n/2) + cn

T(n) = O (n log n)

You might also like