Data Structures using C PlusPlus: Chapter 3: Analysis of Algorithms and Arrays

Asymptotic Notations

Questions: 1. Elaborate on asymptotic notations with examples. 2. Prove that for any two functions f(n) and g(n), we have f(n) f(n) = Ө(g(n)) if and only if f(n) = O (g(n)) and f(n) = Ω (g(n)). 3. Discuss in detail all the asymptotic notations with examples. 4. Give the definition and graphical representation of O‒notation. 5. Explain briefly Big oh Notation, Omega Notation and Theta Notations. Give examples. 6. What are the rules of manipulate Big ‒ oh expressions and about the typical growth rates of algorithm? 7. Define Big O notation, Big Omega and Big Theta notation. Depict the same graphically and explain.

Asymptotic Notations

• To choose the best algorithm, we need to check efficiency of each algorithm. The efficiency can be measured by computing time complexity of each algorithm. Asymptotic notation is a shorthand way to represent the time complexity.

• Definition : Asymptotic notations are mathematical tools used in algorithm analysis to describe the running time or space requirement of an algorithm in terms of input size (n).

• Using asymptotic notations we can give time complexity as "fastest possible", "slowest possible" or "average time".

Various notations such as Ω Ө, and O used are called asymptotic notions.

 

1. Big oh Notation

• The Big oh notation is denoted by 'O'. It is a method of representing the upper bound of algorithm's running time. Using big oh notation we can give longest amount of time taken by the algorithm to complete.

Definition

• Let f(n) and g(n) be two non‒negative functions.

• Let n0 and constant c are two integers such that n0 denotes some value of input and n > n0. Similarly c is some constant such that c > 0. We can write

f(n) ≤ c*g(n)

then f(n) is big oh of g(n). It is also denoted as f(n) ϵ O (g(n)). In other words f(n) is less than g(n) if g(n) is multiple of some constant c.


Example : Consider function f(n) = 2n + 2 and g(n) = n2. Then we have to find some constant c, so that f(n) ≤ c* g(n). As f(n) = 2n + 2 and g(n) = n2 then we find c for n = 1 then

f(n) = 2n+ 2

       = 2(1) + 2

f(n) = 4

and

g(n) = n2

        = (1)2

g(n)  = 1

i.e.

f(n) > g(n)

If n = 2 then,

f(n) = 2(2) + 2

       = 6

g(n)  = (2)2

g(n) = 4

i.e.

f(n) > g(n)

If n = 3 then,

f(n) = 2(3)+2

       = 8

g(n)  =  (3)2

g(n) = 9

i.e.

f(n) < g(n) is true.

Hence we can conclude that for n > 2, we obtain

f(n) < g(n)

Thus always upper bound of existing time is obtained by big oh notation.


2. Omega Notation

• Omega notation is denoted by 'Ω'. This notation is used to represent the lower bound of algorithm's running time. Using omega notation we can denote shortest amount of time taken by algorithm.

Definition

A function f(n) is said to be in Ω (g(n)) if f(n) is bounded below by some positive constant multiple of g(n) such that

  f(n) ≥ c* g(n)

For all n ≥ n0

It is denoted as f(n) ϵ Ω (g(n)). Following graph illustrates the curve for  Ω notation.


Example :

Consider f(n) = 2n2 + 5 and g(n) = 7n

Then if        n = 0

               f(n) = 2(0)2 +5

                      = 5

               g(n) = 7 (0)

                      = 0              i.e. f(n) > g(n)

But if          n = 1

               f(n) = 2(1)2 +5

                     = 7

              g(n) = 7(1)

                     = 7               i.e. f(n) = g(n)

If n = 3 then,

              f(n) = 2(3)2 +5

                    = 18+ 5

                    = 23

             g(n) = 7(3)

                    = 21

i.e.          f(n) > g(n)

Thus for n > 3 we get f(n) > c* g(n).

It can be represented as

 2n2+5 ϵ Ω (n)

Similarly any

 n3 ϵ Ω (n)2

 

3. Ө Notation

• The theta notation is denoted by Ө. By this method the running time is between upper bound and lower bound.

Definition

• Let f(n) and g(n) be two non negative functions. There are two positive constants namely c1 and c2 such that

c1 * g(n) ≤ f(n) ≤ c2 * g(n)

Then we can say that

f(n) ϵ Ө (g(n))


Example :

If f(n) = 2n + 8 and g(n) = 7n.

where           n ≥  2

Similarly f(n) = 2n + 8

               g(n) = 7n

i.e.        5n < 2n + 8 < 7n        For n ≥ 2

Here     c1 = 5    and               c2 = 7 with n0 = 2.

The theta notation is more precise with both big oh and omega notation.

 

Example: 1

Show the following equalities are correct.

i) 5 n2‒6 n = Ө (n2)

ii) n! = O(nn)

iii) n3 + 106n2 = Ө (n3)

iv) 2 n2 2n+n log n = Ө (n2 2n)

Solution :

i) 5 n2‒ 6 n = Ө (n2)

The given function f(n) ϵ Ө (g (n)) only when f(n) can be represented as

c1 * g(n) ≤ f(n) ≤ c2 * g(n)

f(n) = 5 n2‒ 6 n. This can be denoted as n2 ≤ 5 n2‒ 6 n ≤ 5 n2 for all n ≥ 2.

Where g(n) = n2, c1 = 1 and c2 = 5. Hence given equality holds true.

ii) n! = O(nn)

The given function f(n) ϵ  O (g(n)) only

when               f(n) can be represented by

                        f(n) ≤ c * g(n).

Here                f(n) = n!   and      g(n) = nn

The equation can be represented as,

 n.(n − 1).(n − 2) ....1 ≤ n . n . n .    ...n                    for all n≥1

This shows that f(n) ≤ c*g(n)                  where c = 1.

Hence given equality holds true.

iii) n3+106n2 = Ө(n3)

The given function is f(n) ϵ Ө (g (n)), only

when            f(n) can be represented as

c1 * g (n) ≤ f(n) ≤ c2 * g (n).

f(n) = n3 + 106 n2, f(n) = n3.

This can be denoted as ‒

n3 ≤ n3 + 106 n2 ≤ 107n3

and        c1 = 1, c2 = 107                                    for all n ≥ 1

Hence given equality is true.

iv) 2 n2 2n + n log n = Ө (n22n)

The given function f(n) ϵ Ө (g(n)), only

when         f(n) can be represented as

c1* g (n) f(n) ≤ c2 * g(n).

f(n) = 2 n2 2n + n log n. This can also be denoted as ‒

c1n2 2n ≤ 2 n2 2n + n log n ≤ c2 n2 2n

 c1 ≤ 2 +  ( n log n / n22n)    ≤ c2


where            for all n ≥ 1, 0 ≤ ( n log n / n22n) ≤ 1

with              c1 = 2, c2 = 3 and n0 = 1

Hence given equality is true.

Example: 2

State whether following equality is correct or wrong?

 5n3 + 4 n = Ω(n2)

Solution :

To prove that given function f(n) ϵ Ω(g(n)), only when f(n) can be represented as f(n) ≥ c * g(n) where n ≥ n0.

Here, assume, f(n) = 5n3 + 4 n and g(n) = n2.

 5n3 + 4n  ≥ n2         when c = 1, n0 = 0

 for all n ≥ n0. This shows that given equality is true.

Example: 3

State whether following equality is correct or wrong?

10n3 + n log n + 9 = O(n2).

Solution :

We assume that, we have obtained the values of c n0 in such a way that n >= n0 and we have 10n3+ n log n + 9 < c n2. Now Let, m = max(n0, ceiling (c/10)), then

We have,

10 m3 + m log m + 9 < cm2

         = 10 m3+ m log m + 9 − cm2 < 0

         = m2(10 m ‒ c) + m log m + 9 < 0

But 10 m ‒ c > 0, hence

m2(10 m ‒ c) + m log m + 9 < 0 is not possible.

That means there are no such values of c and n0 such that n ≥ n0 and

10 n3 + n log n + 9 < c n2. Hence given equality is wrong.

Example: 4

Derive a loose bound on the following equation:

f(x) = 35x8 ‒ 22x7 +14x5 ‒2x4 ‒4x2+x‒15

Solution :

Let, f(n) and g(n) are two non‒negative functions.

Let c be some constant.

The equation

                  f(n) ≤ c*g(n)

then          f(n) = ϵ O(g(n) with tight bound.

But if       f(n) < c*g(n)

then         f(n)  ϵ O(g(n) with loose bound.

Consider the function

          f(x) = 35x8‒22x7 +14x5 ‒2x4 ‒4x2 +x‒15

and          g(x) =  x8

If                  x = 1, then

                 f(x) = 35(1)8 − 22(1)7 +14(1)5 −2(1)4 −4(1)2 +1‒15

                  f(x) = 7

                 g(x) = x8 = (1)8

If we assume c = 35 then

We will always get

f(x) < g(x) for x ≥1

 

4. Basic Efficiency Classes

• We can have different efficiency classes and each class possessing certain characteristic. Let us see the classification of different order of growth 



Review Questions

1. Elaborate on asymptotic notations with examples.

2. Prove that for any two functions f(n) and g(n), we have f(n) f(n) = Ө(g(n)) if and only if f(n) = O (g(n)) and f(n) = Ω (g(n)).

3. Discuss in detail all the asymptotic notations with examples.

4. Give the definition and graphical representation of O‒notation.

5. Explain briefly Big oh Notation, Omega Notation and Theta Notations. Give examples.

6. What are the rules of manipulate Big ‒ oh expressions and about the typical growth rates of algorithm?

7. Define Big O notation, Big Omega and Big Theta notation. Depict the same graphically and explain.

 

Data Structures using C PlusPlus: Chapter 3: Analysis of Algorithms and Arrays : Tag: Data Structure, C++ Programing : - Asymptotic Notations


Data Structures using C PlusPlus: Chapter 3: Analysis of Algorithms and Arrays



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation