Sub topic 2
Asymptotic Notation: O, Omega, and Theta
This chapter assumes you can read basic loops and recursion, work with logarithms and exponents, and sum simple series such as 1 + 2 + ... + n. It builds on the previous chapter, where cost was measured as a function of input size n, with best, average, and worst cases.
Why we need asymptotic notation
The exact step count of an algorithm depends on the machine, the compiler, and details such as how many instructions one comparison takes. Suppose one algorithm runs in 3n² + 5n + 2 steps. Another runs in 100n + 400 steps. For small n the first may win. For large n the second always wins.
Asymptotic notation keeps only what matters as n grows without bound:
- Constant factors are dropped, because they change with the machine.
- Lower-order terms are dropped, because the highest-order term dominates for large n.
The result is a statement about the growth rate of a function. It is not a statement about the running time of any particular run.
The three main notations
Let f(n) and g(n) be functions from the natural numbers to non-negative reals. For running times, f(n) is usually the exact cost and g(n) is a simple comparison function such as n, n log n, or n².
Big-O: an upper bound
f(n) = O(g(n)) means there exist constants c > 0 and n₀ such that
f(n) ≤ c · g(n) for all n ≥ n₀.
Read it as "f grows no faster than g, up to a constant factor."
Big-Omega: a lower bound
f(n) = Ω(g(n)) means there exist constants c > 0 and n₀ such that
f(n) ≥ c · g(n) for all n ≥ n₀.
Read it as "f grows at least as fast as g."
Big-Theta: a tight bound
f(n) = Θ(g(n)) means f(n) is both O(g(n)) and Ω(g(n)). Equivalently, there exist constants c₁, c₂ > 0 and n₀ such that
c₁ · g(n) ≤ f(n) ≤ c₂ · g(n) for all n ≥ n₀.
Little-o and little-omega
These are the strict versions.
- f(n) = o(g(n)) means f grows strictly slower than g. For every c > 0 there is an n₀ with f(n) < c · g(n) for n ≥ n₀. Equivalently, f(n)/g(n) → 0.
- f(n) = ω(g(n)) means f grows strictly faster than g. Equivalently, f(n)/g(n) → ∞.
| Notation | Informal meaning | Analogy | Limit test on f(n)/g(n) |
|---|---|---|---|
| f = o(g) | strictly slower | f < g | limit is 0 |
| f = O(g) | no faster | f ≤ g | limit is finite (less than ∞) |
| f = Θ(g) | same rate | f = g | limit is a positive constant |
| f = Ω(g) | no slower | f ≥ g | limit is greater than 0 (may be ∞) |
| f = ω(g) | strictly faster | f > g | limit is ∞ |
The limit test is a fast way to settle many questions, but it only works when the limit exists. The definitions with c and n₀ always apply.
A worked proof from the definition
Show that 3n² + 5n + 2 = Θ(n²).
Upper bound. For n ≥ 1, 5n ≤ 5n² and 2 ≤ 2n². So
3n² + 5n + 2 ≤ 3n² + 5n² + 2n² = 10n².
Take c = 10 and n₀ = 1. Then f(n) = O(n²).
Lower bound. For all n ≥ 1, 3n² + 5n + 2 ≥ 3n². Take c = 3 and n₀ = 1. Then f(n) = Ω(n²).
Both bounds hold, so f(n) = Θ(n²), with c₁ = 3, c₂ = 10, n₀ = 1.
Now show that n² is not O(n). Suppose n² ≤ c · n for all n ≥ n₀. Dividing by n gives n ≤ c for all n ≥ n₀. That is false for n > c. So no constants exist.
Useful rules
These rules let you simplify expressions without going back to the definition.
- Drop constants. c · f(n) = Θ(f(n)) for any constant c > 0.
- Keep the dominant term. A polynomial of degree d with a positive leading coefficient is Θ(n^d).
- Sum rule. f₁ + f₂ = O(max(f₁, f₂)). This is why sequential code blocks cost the larger of the two.
- Product rule. If f₁ = O(g₁) and f₂ = O(g₂), then f₁ · f₂ = O(g₁ · g₂). This is why nested loops multiply.
- Transitivity. If f = O(g) and g = O(h), then f = O(h). The same holds for Ω, Θ, o, and ω.
- Reflexivity. f = Θ(f).
- Symmetry. f = Θ(g) if and only if g = Θ(f).
- Transpose symmetry. f = O(g) if and only if g = Ω(f). Also, f = o(g) if and only if g = ω(f).
- Logarithm bases do not matter. loga n = Θ(logb n) for any constants a, b > 1, because loga n = logb n / log_b a.
Exponents are different. 2^n and 3^n are not Θ of each other, and 2^(2n) is not O(2^n). The base of an exponential changes the growth rate, and a constant multiple in the exponent changes it too.
Checking bounds with the limit test in code
The following program estimates f(n)/g(n) for growing n. The ratio settles near a positive constant when f = Θ(g).
import math
def f(n):
return 3 * n * n + 5 * n + 2
def ratio(f, g, sizes):
for n in sizes:
print(f"n={n:>10} f/g={f(n) / g(n):.4f}")
sizes = [10, 100, 1_000, 10_000, 1_000_000]
print("f vs n^2 (expect constant near 3):")
ratio(f, lambda n: n * n, sizes)
print("f vs n (expect growth to infinity):")
ratio(f, lambda n: n, sizes)
print("f vs n^3 (expect shrinking toward 0):")
ratio(f, lambda n: n ** 3, sizes)The first ratio tends to 3, so f = Θ(n²). The second grows without bound, so f = ω(n). The third shrinks toward 0, so f = o(n³). A numeric check is evidence, not a proof, but it catches wrong guesses quickly.
Common errors
Confusing the bound with the case
O, Ω, and Θ describe the growth of a function. Best, worst, and average case are different functions of n. Each case can have its own O, Ω, and Θ.
For linear search, the worst case is Θ(n) and the best case is Θ(1). It is wrong to say "O is for the worst case and Ω is for the best case." You can say that the running time of linear search is O(n) over all inputs, and also Ω(1) over all inputs, without fixing a case.
Treating O as a tight bound
Saying "binary search is O(n²)" is technically true, because n² is an upper bound. It is also useless. When a question asks for the tightest bound, answer with Θ if it exists. In GATE questions, "the time complexity is O(...)" usually means the tightest upper bound you can prove.
Believing every pair of functions is comparable
Some function pairs are not comparable under O or Ω. A classic example is f(n) = n and g(n) = n^(1 + sin n). The exponent oscillates between 0 and 2, so neither f = O(g) nor g = O(f) holds. Most functions in algorithm analysis are well behaved, but this shows the notation is not a total order.
Forgetting that the bound is for large n
An algorithm that is Θ(n) can be slower than a Θ(n²) algorithm for small n. The notation says nothing about n below n₀, and it hides constants that can be large.
Mishandling logs and exponents
- log(n²) = 2 log n = Θ(log n).
- (log n)² is not Θ(log n). It grows strictly faster.
- 2^(log₂ n) = n.
- n^(log₂ 3) is Θ of itself, and it is not Θ(n²). It is o(n²), because log₂ 3 ≈ 1.585.
- 2^(n+1) = 2 · 2^n = Θ(2^n), but 2^(2n) = 4^n is not O(2^n).
- log(n!) = Θ(n log n). This follows from Stirling's approximation. It appears later in the comparison-sorting lower bound.
Applying the sum rule to a growing number of terms
The sum rule f₁ + f₂ = O(max(f₁, f₂)) holds for a fixed number of terms. Consider 1 + 2 + ... + n. Each term is O(n), and there are n terms, so the product rule gives O(n²). A sloppy reading of the sum rule would give O(n), which is wrong. The sum is exactly n(n+1)/2 = Θ(n²).
Quick comparison tests
When two functions look similar, these steps decide the question.
- Take the limit of f(n)/g(n). A finite positive limit means Θ.
- If the limit has the form ∞/∞, apply L'Hôpital's rule to the derivatives.
- Take logarithms of both sides. Comparing log f and log g often turns a power tower into a simple comparison.
- Substitute large concrete values such as n = 2^20 to sanity-check the direction.
Example: compare n^(log n) and 2^(√n). Take log₂ of both. The first gives (log n)². The second gives √n. Since (log n)² = o(√n), the first function is o of the second.
Summary of the working model
- O gives an upper bound, Ω a lower bound, Θ both at once.
- The definitions use constants c and n₀, so constants and small inputs do not matter.
- Cases (best, average, worst) and bounds (O, Ω, Θ) are separate choices.
- Prefer Θ when you can prove it, and treat O alone as "at most."
Exercise
Is 2^(n+1) = O(2^n)? Is 2^(2n) = O(2^n)? Justify each answer.
Show answer
Yes for the first. 2^(n+1) = 2 · 2^n, so it is at most c · 2^n with c = 2 for all n ≥ 0 (it is in fact Θ(2^n)). No for the second. 2^(2n) = 4^n, and 4^n / 2^n = 2^n grows without bound, so no constant c satisfies 4^n ≤ c · 2^n for all large n.
Exercise
Order these functions from slowest to fastest growing: n log n, n², 2^n, log n, n^1.5, n.
Show answer
log n, n, n log n, n^1.5, n², 2^n. Each step is strictly faster: n log n is o(n^1.5) because log n is o(n^0.5), and n^1.5 is o(n²). The exponential 2^n eventually beats every polynomial.
Exercise
A student says "linear search is O(n) in the worst case and Ω(1) in the best case, so its running time is Θ(n)." What is wrong?
Show answer
Θ(n) would need both bounds to hold for the same function. The worst-case function is Θ(n), and the best-case function is Θ(1). Over all inputs together, the running time is O(n) and Ω(1), and no single Θ bound describes it. A Θ bound is valid only once you fix a case.