Asymptotic Notation and Basic Efficiency Classes
Asymptotic Notation and Basic Efficiency Classes
Asymptotic Notation and Basic Efficiency Classes
Asymptotic Notation:
Asymptotic notations are the mathematical notations used to describe the running time of an
algorithm when the input tends towards a particular value or a limiting value. Execution time
of an algorithm depends on the instruction set, processor speed, disk I/O speed, etc. Hence, we
estimate the efficiency of an algorithm asymptotically.
Let t(n) and g(n) can be any nonnegative functions defined on the set of natural numbers. The
algorithm’s running time t(n) usually indicated by its basic operation count C(n), and g(n),
some simple function to compare with the count.
Different types of asymptotic notations are used to represent the complexity of an algorithm.
Following asymptotic notations are used to calculate the running time complexity of an
algorithm.
O − Big Oh
Ω − Big omega
θ − Big theta
Hence, function g(n) is an upper bound for function f(n), as g(n) grows faster than f(n).
Example
Let us consider a given function, f(n)=4.n3+10. 4.n2+5.n+1
Considering g(n)=n3,
f(n)⩽ 5.g(n) for all the values of n>2
We say that f(n)=Ω(g(n)) when there exists constant c that f(n)⩾ c.g(n) for all
sufficiently large value of n. Here n is a positive integer. It means function g is a lower bound
for function f; after a certain value of n, f will never go below g.
Example
Let us consider a given function, f(n)= 4.n3+10. 4.n2+5.n+1
3
Considering g(n)= , f(n)⩾ 4.g(n) for all the values of n>0
We say that f(n)=θ(g(n)) when there exist constants c1 and c2 that 1.g(n)⩽ f(n)⩽ 2.g(n)
for all sufficiently large value of n. Here n is a positive integer. This means function g is a
tight bound for function f.
Example
Let us consider a given function, f(n)= 4.n3+10. 4.n2+5.n+1
3
Considering g(n)= , 4.g(n)⩽ f(n)⩽ 5.g(n) for all the large values of n.
Asymptotic Analysis
EXAMPLE 1: Compute the factorial function F(n) = n! for an arbitrary non-negative integer
n. Since n! = 1•. . . . • (n − 1) • n = (n − 1)! • n, for n ≥ 1 and 0!= 1 by definition, we can
compute F(n) = F(n − 1) • n with the following recursive algorithm.
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
Algorithm analysis
Recurrence relations
The last equation defines the sequence M(n) that we need to find. This equation
This tells us two things. First, since the calls stop when n = 0, the smallest value
of n for which this algorithm is executed and hence M(n) defined is 0. Second, by
inspecting the pseudo code’s exiting line, we can see that when n = 0, the
algorithm performs no multiplications.
operation is executed.
5. Using standard formulas and rules of sum manipulation either find a
closed form formula for the count or at the least, establish its order
of growth.
EXAMPLE 1: Consider the problem of finding the value of the largest element in a
list of n numbers. Assume that the list is implemented as an array for simplicity.
Algorithm Analysis
The measure of an input’s size here is the number of elements in the array, i.e.,
n.
There are two operations in the for loop’s body: o The comparison A[i]>
maxval and o The assignment maxval←A[i].
The comparison operation is considered as the algorithm’s basic operation,
because the comparison is executed on each repetition of the loop and not
the assignment.
The number of comparisons will be the same for all arrays of size n;
therefore, there is no need to distinguish among the worst, average, and
best cases here.
Let C(n) denotes the number of times this comparison is executed. The
algorithm makes one comparison on each execution of the loop, which is
repeated for each value of the loop’s variable i within the bounds 1 and n
EXAMPLE 2:
Consider the element uniqueness problem: check whether all the Elements in a
given array of n elements are distinct.
Algorithm Analysis:
The measure of an input’s size here is the number of elements in the array, i.e.,
n.
There are two operations in the for loop’s body: o The comparison A[i]>
maxval and o The assignment maxval←A[i].
The comparison operation is considered as the algorithm’s basic operation,
because the comparison is executed on each repetition of the loop and not
the assignment.
The number of comparisons will be the same for all arrays of size n;
therefore, there is no need to distinguish among the worst, average, and
best cases here.
Let C(n) denotes the number of times this comparison is executed. The
algorithm makes one comparison on each execution of the loop, which is
repeated for each value of the loop’s variable i within the bounds 1 and n
Algorithm Analysis:
The natural measure of the input’s size here is again n (the number of elements in the
array).
Since the innermost loop contains a single operation (the comparison of two elements),
we should consider it as the algorithm’s basic operation.
The number of element comparisons depends not only on n but also on whether there
are equal elements in the array and, if there are, which array positions they occupy. We
will limit our investigation to the worst case only.
One comparison is made for each repetition of the innermost loop, i.e., for each value
of the loop variable j between its limits i + 1 and n − 1; this is repeated for each value of
the outer loop, i.e., for each value of the loop variable i between its limits 0 and n −2
Algorithm Design :
The important aspects of algorithm design include creating an efficient algorithm to
solve a problem in an efficient way using minimum time and space.
To solve a problem, different approaches can be followed. Some of them can be efficient
with respect to time consumption, whereas other approaches may be memory efficient.
However, one has to keep in mind that both time consumption and memory usage
cannot be optimized simultaneously. If we require an algorithm to run in lesser time, we
have to invest in more memory and if we require an algorithm to run with lesser memory,
we need to have more time.