Loading lessons...
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
| Notation | Meaning | Definition |
|---|---|---|
| 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 smaller | f(n)/g(n) → 0 |
The constants c and n₀ are called witnesses.
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). ✓
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
| n | log₂ n | n log₂ n | n² | 2ⁿ |
|---|---|---|---|---|
| 8 | 3 | 24 | 64 | 256 |
| 16 | 4 | 64 | 256 | 65,536 |
| 64 | 6 | 384 | 4,096 | 1.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 pattern | Steps | Order |
|---|---|---|
| single loop 1..n | n | Θ(n) |
| two nested loops 1..n | n² | Θ(n²) |
| i = 1; while i < n: i = 2i | log₂ n | Θ(log n) |
| for i in 1..n, for j in 1..i | n(n + 1)/2 | Θ(n²) |
| binary search | T(n) = T(n/2) + 1 | Θ(log n) |
| merge sort | T(n) = 2T(n/2) + n | Θ(n log n) |
The last two are recurrences, solved in Lessons 20-22 (Master theorem).
📝 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)