L5 Asmptotic Notations

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Asymptotic Notations

Asymptotic notation is a way of comparing functions that ignores constant factors and
small input sizes. Three notations used to compare orders of growth of an algorithms
basic operation count are: O, , notations
Big Oh- O notation
Definition:
A function t(n) is said to be in O(g(n)), denoted t(n) O(g(n)), if t(n) is bounded above
by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant
c and some nonnegative integer n0 such that t(n) cg(n) for all n n0

Big Omega- notation

Definition:
A function t (n) is said to be in (g(n)), denoted t(n) (g (n)), if t (n) is bounded
below by some constant multiple of g (n) for all large n, i.e., if there exist some positive
constant c and some nonnegative integer n0 such that

t(n) cg(n) for all n n0


Big Theta- notation
Definition:
A function t (n) is said to be in (g (n)), denoted t(n) (g (n)), if t (n) is bounded both
above and below by some constant multiple of g (n) for all large n, i.e., if there exist some
positive constant c1 and c2 and some nonnegative integer n0 such that c2 g (n) t (n)
c1 g (n) for all n n0

Mathematical analysis (Time Efficiency) of Non-recursive Algorithms


General plan for analyzing efficiency of non-recursive algorithms:
1. Decide on parameter n indicating input size
2. Identify algorithms basic operation
3. Check whether the number of times the basic operation is executed depends only
on the input size n. If it also depends on the type of input, investigate worst,
average, and best case efficiency separately.
4. Set up summation for C(n) reflecting the number of times the algorithms basic
operation is executed.
5. Simplify summation using standard formulas

Example: Finding the largest element in a given array

ALOGORITHM MaxElement (A [0...n-1])


//Determines the value of largest element in a given array
//Input: An array A[0..n-1] of real numbers
//Output: The value of the largest element in A
currentMax A[0]
for i 1 to n 1 do
if A[i] > currentMax
currentMax A[i]
return currentMax

Analysis:
1. Input size: number of elements = n (size of the array)
2. Basic operation:
a) Comparison
b) Assignment
3. NO best, worst, average cases.
4. Let C (n) denotes number of comparisons: Algorithm makes one comparison on each
execution of the loop, which is repeated for each value of the loops variable i within the
bound between 1 and n 1.
5. Simplify summation using standard formulas

Mathematical analysis (Time Efficiency) of recursive Algorithms :

General plan for analyzing efficiency of recursive algorithms:


1. Decide on parameter n indicating input size
2. Identify algorithms basic operation
3. Check whether the number of times the basic operation is executed depends only on the
input size n. If it also depends on the type of input, investigate worst, average, and best
case efficiency separately.
4. Set up recurrence relation, with an appropriate initial condition, for the number of times
the algorithms basic operation is executed.
5. Solve the recurrence.

Example: Factorial function


ALGORITHM Factorial (n)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = = 0

return 1

else
return Factorial (n 1) * n

Analysis:
1. Input size: given number = n
2. Basic operation: multiplication
3. NO best, worst, average cases.
4. Let M (n) denotes number of multiplications.
M (n) = M (n 1) + 1 for n > 0
M (0) = 0 initial condition
Where: M (n 1) : to compute Factorial (n 1)
1 :to multiply Factorial (n 1) by n
5. Solve the recurrence: Solving using Backward substitution method:
M (n) = M (n 1) + 1
= [ M (n 2) + 1 ] + 1
= M (n 2) + 2
= [ M (n 3) + 1 ] + 3
= M (n 3) + 3

In the ith recursion, we have
= M (n i ) + i
When i = n, we have
= M (n n ) + n = M (0 ) + n
Since M (0) = 0
= n
M (n) (n)

Example: Find the number of binary digits in the binary representation of a positive
decimal integer
ALGORITHM BinRec (n)
//Input: A positive decimal integer n
//Output: The number of binary digits in ns binary representation
if n = = 1 return 1

else

return BinRec ( n/2 ) +1

Analysis:
1. Input size: given number = n
2. Basic operation: addition
3. NO best, worst, average cases.
4. Let A (n) denotes number of additions.
A (n) = A ( n/2 ) + 1 for n > 1
A (1) = 0 initial condition
Where: A ( n/2 ) : to compute BinRec ( n/2 )
1 : to increase the returned value by 1
5. Solve the recurrence:
A (n) = A ( n/2 ) + 1 for n > 1

Assume n = 2k (smoothness rule)


A (2k) = A (2k-1) + 1 for k > 0; A (20) = 0
Solving using Backward substitution method:
A (2k) = A (2k-1) + 1
= [A (2k-2) + 1] + 1
= A (2k-2) + 2
= [A (2k-3) + 1] + 2
= A (2k-3) + 3

In the ith recursion, we have
= A (2k-i) + i
When i = k, we have
= A (2k-k) + k = A (20) + k
Since A (20) = 0

A (2k) = k
Since n = 2k, HENCE k = log2 n
A (n) = log2 n

A (n) ( log n)

You might also like