Asymptotic Notation | Data Structure & Algorithms

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. Consider the following example: When the input list has already been sorted, the time taken by the algorithm is linear, i.e. best case, worst case, and so on.

Types of Asymptotic Notation:-

- The time complexity of algorithms is commonly represented using the following three asymptotic notations.

1. Θ Notation:

A function is bounded by Theta notation from above and below, so it defines exact asymptotic behavior.
The best way to get Theta notation of an expression is to drop low order terms and ignore leading constants. For example, let's consider the following expression.
3a3 + 6a2 + 6000 = Θ(a3)

Dropping lower order terms is always fine because there will always be a number(a) after which Θ(a3) has higher values than Θ(a2) irrespective of the constants involved.
For a given function g(a), we denote Θ(g(a)) is following set of functions

The above definition means, if f(a) is theta of g(a), then the value f(a) is always between c1*g(a) and c2*g(a) for large values of a (a >= a0). The definition of theta also requires that f(a) must be non-negative for values of a greater than a0.

2) Big O Notation:

The upper bound of an algorithm is defined by Big O notation, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in the best case and quadratic time in the worst case. We can safely say that the time complexity of Insertion sort is O(n²). Note that O(n²) also covers linear time.

If we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and worst cases:
-1. The worst-case time complexity of Insertion Sort is Θ(n²).
-2. The best-case time complexity of Insertion Sort is Θ(n)

.

The Big O notation is useful when we only have upper bound on time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm.

3) Ω Notation:

Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an asymptotic lower bound.

Ω Notation can be useful when we have lower bound on time complexity of an algorithm. As discussed in the previous post, the best-case performance of an algorithm is generally not useful, the Omega notation is the least used notation among all three.

For a given function g(n), we denote by Ω(g(n)) the set of functions.

Let us consider the same Insertion sort example here. The time complexity of Insertion Sort can be written as Ω(n), but it is not a piece of very useful information about insertion sort, as we are generally interested in worst-case and sometimes in the average case.

Properties of Asymptotic Notation-

Let’s talk about some of the essential properties of these three notations now that we’ve gone through their meanings. The following are the most popular sorting algorithms:

· General properties

· Transitive properties

· Reflexive properties

· Symmetric properties

· Transpose symmetric properties

· Some other properties

General Properties-

If f(n) is O(g(n)) then a*f(n) is also O(g(n)) ; where a is a constant.

Example: f(n) = 4n²+3 is O(n²)
then 2*f(n) = 2(4n²+3) = 8n²+6 is also O(n²) .

Similarly this property satisfies for both Θ and Ω notation.

We can say
If f(n) is Θ(g(n)) then a*f(n) is also Θ(g(n)) ; where a is a constant.
If f(n) is Ω (g(n)) then a*f(n) is also Ω (g(n)) ; where a is a constant.

Transitive Properties-

If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) = O(h(n)) .

Example: if f(n) = n, g(n) = n² and h(n)=n³
n is O(n²) and n² is O(n³)
then n is O(n³)

Similarly, this property satisfies for both Θ and Ω notation.

We can say
If f(n) is Θ(g(n)) and g(n) is Θ(h(n)) then f(n) = Θ(h(n)) .
If f(n) is Ω (g(n)) and g(n) is Ω (h(n)) then f(n) = Ω (h(n))

Reflexive Properties-

Reflexive properties are always easy to understand after transitive.

If f(n) is given then f(n) is O(f(n)). Since MAXIMUM VALUE OF f(n) will be f(n) ITSELF !

Hence x = f(n) and y = O(f(n) tie themselves in reflexive relation always.

Example:

f(n) = n² ; O(n²) i.e O(f(n))

Similarly, this property satisfies for both Θ and Ω notation.

We can say that:

If f(n) is given then f(n) is Θ(f(n)).

If f(n) is given then f(n) is Ω (f(n)).

Symmetric Properties-

If f(n) is Θ(g(n)) then g(n) is Θ(f(n)) .

Example: f(n) = n² and g(n) = n²
then f(n) = Θ(n²) and g(n) = Θ(n²)

-This property will only satisfy Θ notation.

Transpose Reflexive properties-

If f(n) is O(g(n)) then g(n) is Ω (f(n)).

Example: f(n) = n , g(n) = n²
then n is O(n²) and n² is Ω (n)

-This property will only satisfy O and Ω notations.

Some Other Properties-

1.) If f(n) = O(g(n)) and f(n) = Ω(g(n)) then f(n) = Θ(g(n))

2.) If f(n) = O(g(n)) and d(n)=O(e(n))
then f(n) + d(n) = O( max( g(n), e(n) ))

Example: f(n) = n i.e O(n)
d(n) = n² i.e O(n²)
then f(n) + d(n) = n + n² i.e O(n²)

3.) If f(n)=O(g(n)) and d(n)=O(e(n))
then f(n) * d(n) = O( g(n) * e(n) )

Example: f(n) = n i.e O(n)
d(n) = n² i.e O(n²)
then f(n) * d(n) = n * n² = n³ i.e O(n³)

References-

-Abhay R. (2021, February 4) Analysis of Algorithms | Set 3 (Asymptotic Notations). https://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/