Lesson 3 +30 XP

Asymptotic Notations (Big-O, Omega, Theta)

Asymptotic Notations (Big-O, Omega, Theta)

Step counts such as 2n + 3 or 2n2 + 2n + 1 are too detailed to compare easily. Asymptotic notation keeps only the part that matters for large inputs - the rate of growth - and gives us a short, machine-independent way to describe the cost of an algorithm.

1. The Idea of Asymptotic Analysis

Asymptotic" means "as n becomes very large". For large n:

  • Constant factors depend on the machine and compiler, so we ignore them (5n and n grow the same way).
  • Lower-order terms become negligible: in n2 + 100n, at n = 106 the n2 term is 10 000 times bigger than 100n.

So T(n) = 3n2 + 5n + 7 is simply described as "grows like n2".

nn23n2 + 5n + 7Share of 3n2
1010035784%
10010 00030 50798.3%
10001 000 0003 005 00799.8%

The three main notations say whether a function g(n) is an upper bound, a lower bound, or both for the running time f(n).

2. Big-O Notation - Upper Bound

Definition. f(n) = O(g(n)) if there exist positive constants c and n0 such that

0 ≤ f(n) ≤ c · g(n) for all n ≥ n0

Meaning: beyond some point n0, f(n) never grows faster than a constant multiple of g(n). Big-O describes the worst-case growth: "the algorithm takes at most this long".

DIAGRAM
 time
  ^                    c·g(n)
  |                  /
  |                /    ___ f(n)
  |              /  __--
  |            / _-
  |          /_-
  |       _-/
  |    _-  /
  |  -    /
  +---------|------------------> n
           n0
  for n ≥ n0, f(n) stays below c·g(n)

Worked proof 1: show 4n + 6 = O(n).

  • For n ≥ 1: 6 ≤ 6n, so 4n + 6 ≤ 4n + 6n = 10n.
  • Choose c = 10, n0 = 1. Then 4n + 6 ≤ 10 · n for all n ≥ 1. ∎

Worked proof 2: show 5n2 + 3n + 20 = O(n2).

  • For n ≥ 1: 3n ≤ 3n2 and 20 ≤ 20n2.
  • So 5n2 + 3n + 20 ≤ 5n2 + 3n2 + 20n2 = 28n2.
  • Choose c = 28, n0 = 1. ∎

This "replace every term by the highest power" trick is called the coefficient-sum method: for any polynomial of degree d with positive leading coefficient, f(n) = O(nd) with c = sum of the absolute values of the coefficients and n0 = 1.

Worked proof 3: show 4x2 - 5x + 3 = O(x2).

  • For x ≥ 1: 4x2 - 5x + 3 ≤ 4x2 + 5x2 + 3x2 = 12x2.
  • Choose c = 12, n0 = 1. ∎

More examples from the notes

f(n)Big-OWitnesses
7n - 3O(n)7n - 3 ≤ 7n for n ≥ 1: c = 7, n₀ = 1
50 n log nO(n log n)c = 50, n₀ = 1
8n2 log n + 5n2 + nO(n2 log n)≤ 14 n2 log n for n ≥ 2: c = 14, n₀ = 2
(n(n+1) / 2)O(n2)(n2 / 2) + (n / 2) ≤ n2 for n ≥ 1: c = 1, n₀ = 1

Big-O is only an upper bound. n2 + n = O(n3) is also true, just not tight. In answers we always give the smallest (tightest) class: say O(n2), not O(n3).

Disproof example: is n2 = O(n)? We would need n2 ≤ c · n, i.e. n ≤ c, for all n ≥ n0. No constant c works for every large n, so n2 ≠ O(n).

3. Big-Omega Notation - Lower Bound

Definition. f(n) = Ω(g(n)) if there exist positive constants c and n0 such that

0 ≤ c · g(n) ≤ f(n) for all n ≥ n0

Meaning: beyond n0, f(n) grows at least as fast as a constant multiple of g(n). Big-Omega describes the best-case growth: "the algorithm needs at least this much time".

Worked proof: show 3n2 + 2n = Ω(n2).

  • For all n ≥ 1: 3n2 + 2n ≥ 3n2 ≥ 1 · n2.
  • Choose c = 1 (or 3), n0 = 1. ∎

Example: any comparison-based sorting algorithm is Ω(n log n) in the worst case - no such algorithm can do better.

4. Theta Notation - Tight Bound

Definition. f(n) = Θ(g(n)) if there exist positive constants c1, c2 and n0 such that

0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0

Meaning: f(n) is sandwiched between two constant multiples of g(n); it grows exactly like g(n).

f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)).

Worked proof: show (n2 / 2) - 3n = Θ(n2).

  • Upper: (n2 / 2) - 3n ≤ (1 / 2) n2 for all n ≥ 1, so c2 = (1 / 2).
  • Lower: we need (n2 / 2) - 3n ≥ c1 n2. Dividing by n2: (1 / 2) - (3 / n) ≥ c1. For n ≥ 7, (1 / 2) - (3 / 7) = (1 / 14), so c1 = (1 / 14) works.
  • Choose c1 = (1 / 14), c2 = (1 / 2), n0 = 7. ∎

5. Little-o and Little-omega (Strict Bounds)

NotationMeaningTest with the ratio f(n)/g(n) as n → ∞
f(n) = o(g(n))f grows strictly slower than g (upper bound that is not tight)ratio → 0
f(n) = ω(g(n))f grows strictly faster than g (lower bound that is not tight)ratio → ∞

Examples: 2n = o(n2) but 2n2 ≠ o(n2) (the ratio tends to 2, not 0). n2 = ω(n) but n2 ≠ ω(n2).

6. Summary of the Five Notations

NotationBoundLike comparing numbersTypical use
OUpper (may be tight)f ≤ gWorst-case running time
ΩLower (may be tight)f ≥ gBest case, lower bound of a problem
ΘTight (both)f = gExact order of growth
oStrict upperf < g"Strictly faster" comparisons
ωStrict lowerf > g"Strictly slower" comparisons

7. Rules for Working with Asymptotic Notation

  1. Drop constant factors: 5n2 = O(n2); O(3n) = O(n).
  2. Keep the dominant term: a polynomial of degree d is Θ(nd).
  3. Sum rule (sequential code): if T1 = O(f) and T2 = O(g), then T1 + T2 = O(max(f, g)). A loop of n2 followed by a loop of n is O(n2).
  4. Product rule (nested code): T1 · T2 = O(f · g). A loop of n inside a loop of log n is O(n log n).
  5. Logarithm base does not matter: log2 n and log10 n differ only by a constant factor, so we just write O(log n).
  6. Transitivity: if f = O(g) and g = O(h), then f = O(h).

8. Common Growth Rates

1 < log n < √(n) < n < n log n < n2 < n3 < 2n < n!

ClassNameExample
O(1)ConstantAccess a[i], push onto a stack
O(log n)LogarithmicBinary search, search in a balanced BST
O(n)LinearLinear search, traversing a list
O(n log n)LinearithmicMerge sort, heap sort, average quick sort
O(n2)QuadraticBubble, selection and insertion sort
O(n3)CubicPlain matrix multiplication, Floyd-Warshall
O(2n)ExponentialAll subsets, naive recursive Fibonacci
O(n!)FactorialAll permutations (brute-force travelling salesman)

How fast they grow (number of steps):

nlog2 nn log2 nn22n
832464256
1646425665 536
3251601024about 4.3 × 10⁹
10241010 240about 10⁶astronomically large

An O(2n) algorithm becomes useless for quite small n, while O(n log n) handles millions of items easily. This is why the growth rate, not the constant factor, decides which algorithm is better.

💡 Tip

Key points to remember - O = upper bound (at most), Ω = lower bound (at least), Θ = tight bound (exactly). - To prove f = O(g), find explicit constants c and n0 and show the inequality. - Θ holds exactly when both O and Ω hold. - Drop constants and lower-order terms; nested loops multiply, sequential code adds. - Order: 1 < log n < √(n) < n < n log n < n2 < n3 < 2n < n!

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Define asymptotic notations O, Ω and Θ with graphs. (7.5 Marks)
  • Write: the formal definition of each with constants c, n0; a sketch of f(n) below c · g(n) (for O), above it (for Ω), between two curves (for Θ); one example each.
  • Q2. Prove that 4n + 6 = O(n) and 4x2 - 5x + 3 = O(x2). (5 Marks)
  • Write: the coefficient-sum steps, ending with explicit c and n0.
  • Q3. Show that (n2 / 2) - 3n = Θ(n2). (5 Marks)
  • Q4. Arrange in increasing order of growth: n!, n log n, 2n, log n, n2, √(n). (2.5 Marks)