Lesson 13 +30 XP

Growth of Functions: Big-O, Big-Ω & Big-Θ

Growth of Functions: Big-O, Big-Ω & Big-Θ

To compare algorithms we compare how their running times grow for large inputs, ignoring constant factors and small n.

1. Asymptotic Notations

NotationMeaningDefinition
f(n) = O(g(n))upper bound ("at most")∃ c > 0, n₀ such that f(n) ≤ c·g(n) for all n ≥ n₀
f(n) = Ω(g(n))lower bound ("at least")∃ c > 0, n₀ such that f(n) ≥ c·g(n) for all n ≥ n₀
f(n) = Θ(g(n))tight bound ("exactly")c₁·g(n) ≤ f(n) ≤ c₂·g(n) for n ≥ n₀
f(n) = o(g(n))strictly smallerf(n)/g(n) → 0

The constants c and n₀ are called witnesses.

Big-O, Big-Ω and Big-Θ as bounding curves beyond n₀Big-O, Big-Ω and Big-Θ as bounding curves beyond n₀

2. Proving Big-O with Witnesses

Solved 1. Show 3n² + 5n + 2 = O(n²). For n ≥ 1: 5n ≤ 5n² and 2 ≤ 2n², so 3n² + 5n + 2 ≤ 3n² + 5n² + 2n² = 10n². Witnesses c = 10, n₀ = 1. ✓

Solved 2. Show n² ≠ O(n). Suppose n² ≤ cn for all n ≥ n₀. Then n ≤ c for all large n-impossible. ✓

Solved 3. Show 7n³ + 2n = Θ(n³). Lower: 7n³ + 2n ≥ 7n³ (c₁ = 7). Upper: ≤ 9n³ for n ≥ 1 (c₂ = 9). ✓

💡 Tip

Polynomial rule: if f(n) = aₖnᵏ + … + a₁n + a₀ with aₖ > 0, then f(n) = Θ(nᵏ). Keep the leading term, drop its coefficient.

3. The Growth Hierarchy

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

nlog₂ nn log₂ nn²2ⁿ
832464256
1646425665,536
6463844,0961.8 × 10¹⁹

4. Useful Rules

  • Sum: if f₁ = O(g₁) and f₂ = O(g₂) then f₁ + f₂ = O(max(g₁, g₂)).
  • Product: f₁·f₂ = O(g₁·g₂).
  • Transitivity: f = O(g) and g = O(h) ⇒ f = O(h).
  • log bases differ by a constant factor (loga n = logb n / logb a), so we write O(log n) without a base.
  • log(n!) = Θ(n log n) since n! ≤ nⁿ and n! ≥ (n/2)n/2.

Limit test. If lim f(n)/g(n) = L:

  • L = 0 ⇒ f = O(g) but not Θ
  • 0 < L < ∞ ⇒ f = Θ(g)
  • L = ∞ ⇒ f = Ω(g) but not Θ

5. Analysing Simple Algorithms

Code patternStepsOrder
single loop 1..nnΘ(n)
two nested loops 1..nn²Θ(n²)
i = 1; while i < n: i = 2ilog₂ nΘ(log n)
for i in 1..n, for j in 1..in(n + 1)/2Θ(n²)
binary searchT(n) = T(n/2) + 1Θ(log n)
merge sortT(n) = 2T(n/2) + nΘ(n log n)

The last two are recurrences, solved in Lessons 20-22 (Master theorem).

🎯 END-TERM EXAM PREP

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

  • Q1. Define O, Ω and Θ notation. Show that 3n² + 5n + 2 = O(n²) with witnesses. (5 Marks)
  • Q2. Arrange in increasing order of growth: n!, 2ⁿ, n log n, √n, n², log n. (2.5 Marks)
  • Q3. Prove that log(n!) = Θ(n log n). (5 Marks)