To compare algorithms we ignore small details and look at how the count grows when n becomes very large. Asymptotic notations are the language for this.

Why Ignore Constants

Suppose one algorithm takes 3n² + 5n + 20 steps and another takes 100n steps. For n = 10 the first is 370 steps and the second 1000; but for n = 1000 the first takes about 3 million and the second only 100,000. As n grows the highest-order term dominates and the constant factor hardly matters, because a faster machine changes constants but not growth. We therefore describe a function by its rate of growth, writing 3n² + 5n + 20 simply as of order n².

Big-O Notation (Upper Bound)

We say f(n) = O(g(n)) if there are positive constants c and n₀ such that f(n) ≤ c·g(n) for all n ≥ n₀. It says f grows no faster than g, so g is an upper bound. For example 3n + 2 = O(n) because 3n + 2 ≤ 4n for all n ≥ 2 (take c = 4 and n₀ = 2). Big-O is used to state the worst-case guarantee of an algorithm.

Omega Notation (Lower Bound)

f(n) = Ω(g(n)) if there are positive constants c and n₀ such that f(n) ≥ c·g(n) for all n ≥ n₀. It says f grows at least as fast as g: a lower bound. For 3n + 2, we have 3n + 2 ≥ 3n for all n ≥ 1, so 3n + 2 = Ω(n). Omega describes the best case or the minimum work any algorithm must do.

Theta Notation (Tight Bound)

f(n) = Θ(g(n)) if there are positive constants c₁, c₂ and n₀ such that c₁·g(n) ≤ f(n) ≤ c₂·g(n) for all n ≥ n₀. Theta says f grows exactly as fast as g, both upper and lower bound together. Therefore f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). For 3n + 2 we have already shown both, so 3n + 2 = Θ(n).

Definition: Asymptotic notation. A way of describing the growth rate of a function for large n: O gives an upper bound, Ω a lower bound and Θ a tight bound, ignoring constant factors and lower-order terms.

Common Growth Rates

In increasing order of growth: constant O(1), logarithmic O(log n), linear O(n), linearithmic O(n log n), quadratic O(n²), cubic O(n³), exponential O(2ⁿ) and factorial O(n!). Algorithms up to polynomial are called tractable; exponential ones become unusable even for n around 50. Useful rules: drop constants (5n is O(n)); keep only the largest term (n² + n is O(n²)); for a sequence of steps add the complexities and keep the larger; for nested loops multiply them.

Table 1.2: Growth of common functions

n n log₂ n n²
8 24 64
64 384 4,096
1,024 10,240 1,048,576

Example 1.2: Proving a Big-O bound

Problem: Show that 3n² + 5n + 20 = O(n²) and find suitable constants.

Solution:

  1. For n ≥ 1 we have 5n ≤ 5n² and 20 ≤ 20n².
  2. So 3n² + 5n + 20 ≤ 3n² + 5n² + 20n² = 28n² for all n ≥ 1.
  3. Thus c = 28 and n₀ = 1 satisfy the definition.
  4. Check n = 10: 3(100) + 50 + 20 = 370 ≤ 28(100) = 2800.

Result: 3n² + 5n + 20 = O(n²) with c = 28, n₀ = 1; it is also Ω(n²) with c = 3, hence Θ(n²).

import math
f = lambda n: 3*n*n + 5*n + 20
print("n      f(n)       28*n^2     f<=28n^2")
for n in (1, 10, 100, 1000):
    print(f"{n:<6} {f(n):<10} {28*n*n:<10} {f(n) <= 28*n*n}")
print("n=1024 n*log2(n) =", 1024 * int(math.log2(1024)))

Output

n      f(n)       28*n^2     f<=28n^2
1      28         28         True
10     370        2800       True
100    30520      280000     True
1000   3005020    28000000   True
n=1024 n*log2(n) = 10240

Common mistakes

  • Writing f(n) = O(g(n)) and treating it as an equation of equality; it only says g is an upper bound.
  • Keeping constants and lower-order terms such as O(3n² + n).
  • Using Big-O when a tight Θ bound is known and more informative.
  • Confusing best case with Ω: notations describe functions, cases describe inputs.

Key takeaways

  • O is an upper bound, Ω a lower bound, Θ a tight bound.
  • f = Θ(g) exactly when f = O(g) and f = Ω(g).
  • Order: 1 < log n < n < n log n < n² < n³ < 2ⁿ < n!.
  • Drop constants and lower-order terms; add for sequence, multiply for nesting.

Practice questions

  1. State the definition of f(n) = O(g(n)).
  2. What does Θ notation express?
  3. Which grows faster, n log n or n²?

Answers to the practice questions

  1. There exist c > 0 and n₀ such that f(n) ≤ c·g(n) for all n ≥ n₀.
  2. A tight bound: f grows at the same rate as g, bounded above and below by constant multiples.
  3. n² grows faster for large n.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit I