This note is part of my learning notes on Data Structures & Algorithms.


Asymptotic Notation

Asymptotic notations are mathematical notations used to describe the limiting behavior of a function as its argument approaches infinity or some other limiting value. They are commonly used to analyze the time complexity (time required) or space complexity (memory required) of algorithms. They are also convenient for describing the worst-case running-time function T(n)T(n).

Big O Notation

Big O notation describes an upper bound on the time complexity of an algorithm. It provides an asymptotic upper limit for the growth rate of a function as the input size approaches infinity.(worst case)

A function f(n)f(n) is O(g(n))O(g(n)) if there exist positive constants cc and n0n_0 such that for all nn0n \geq n_0 ,

f(n)cg(n)f(n) \leq c \cdot g(n)

Big Omega Notation (Ω)(\Omega)

Big Omega notation describes a lower bound on the time complexity of an algorithm. It provides an asymptotic lower limit for the growth rate of a function as the input size approaches infinity. (best case)

A function f(n)f(n) is Ω(g(n))\Omega(g(n)) if there exist positive constants cc and n0n_0 such that for all nn0n \geq n_0 ,

f(n)cg(n)f(n) \geq c \cdot g(n)

Big Theta Notation (θ)(\theta)

Big Theta notation describes a tight bound on the time complexity of an algorithm. It provides an asymptotic tight limit for the growth rate of a function as the input size approaches infinity. (average case)

A function f(n)f(n) is θ(g(n))\theta(g(n)) if and only if f(n)f(n) is both O(g(n))O(g(n)) and Ω(g(n))\Omega(g(n)) . That is, there exist positive constants c1c_1, c2c_2 and n0n_0 such that for all nn0n \geq n_0 ,

c1g(n)f(n)c2g(n)c_1 \cdot g(n) \leq f(n) \leq c_2 \cdot g(n)

Visual Representations


Example 1.

Given a function f(n)=3n2+5n+2f(n) = 3n^2 + 5n+2 , prove that f(n)f(n) is O(n2)O(n^2).

Find constants cc and n0n_0 such that for all nn0n \geq n_0 ,

f(n)cn2f(n) \leq c \cdot n^2

We can choose c=10c=10 and n0=1n_0=1 after inspecting the function.
For n1n \geq 1

3n2+5n+23n2+5n2+2n2=10n23n^2 + 5n +2 \leq 3n^2 + 5n^2 + 2n^2 = 10n^2

Hence, f(n)f(n) is O(n2)O(n^2) with c=10c=10 and n0=1n_0 = 1.

Example 2.

Given the function h(n)=7n2+3n5h(n) = 7n^2 + 3n-5, prove that h(n)h(n) is θ(n2)\theta(n^2).

Find constants c1,c2c_1, c_2 and n0n_0 such that for all nn0n \geq n_0 ,

c1n27n2+3n5c2n2c_1 \cdot n^2 \leq 7n^2 + 3n -5 \leq c_2 \cdot n^2

We can choose c1=6,c2=8c_1 = 6, c_2=8 and n0=1n_0=1 after inspecting the function.
For n1n \geq 1

6n27n2+3n58n26n^2 \leq 7n^2 + 3n-5 \leq 8n^2

Hence, h(n)h(n) is θ(n2)\theta(n^2) with c1=6,c2=8c_1 = 6, c_2=8 and n0=1n_0=1 .


References