Sub topic 3
Comparing Growth Rates
This chapter assumes you know what O, Omega, and Theta mean (the previous chapter), and that you are comfortable with logarithms and exponents. Here you will learn how to rank the common functions that appear in complexity analysis. You will also learn the tests that settle close cases, which GATE questions often use.
The standard ladder
Most running times you meet fall into a short list. From slowest-growing to fastest-growing, for large n:
- 1 (constant)
- log log n
- log n
- (log n)^k for any constant k
- n^a for 0 < a < 1, such as √n
- n
- n log n
- n^2, n^3, and in general n^k for larger k
- 2^n
- n!
- n^n
Each function on this list is little-o of every function below it. That means the ratio of an earlier function to a later one goes to 0 as n grows. The following table shows how fast the gaps open up.
| n | log n | n log n | n^2 | 2^n |
|---|---|---|---|---|
| 8 | 3 | 24 | 64 | 256 |
| 16 | 4 | 64 | 256 | 65,536 |
| 32 | 5 | 160 | 1,024 | 4,294,967,296 |
Here logs are base 2. Between n = 16 and n = 32, n^2 grows by a factor of 4, while 2^n is squared.
Rules that settle most comparisons
You rarely need to compute anything heavy. A few rules decide most cases.
- Polynomial versus logarithm. Any positive power of n beats any power of log n. So n^0.1 grows faster than (log n)^100.
- Polynomial versus exponential. Any exponential c^n with c > 1 beats any polynomial n^k.
- Base of a logarithm does not matter. loga n = logb n / logb a, so the two differ by a constant factor. In asymptotic notation, log2 n, ln n, and log_10 n are all Theta of each other.
- Base of an exponential does matter. 2^n and 3^n are not Theta of each other, because 3^n / 2^n = (1.5)^n goes to infinity. Likewise, 2^(2n) = 4^n is not O(2^n).
- Lower-order terms vanish. n^2 + 100n + 5 is Theta(n^2). The dominant term decides the class.
- Constants vanish. 1000n is Theta(n), and 0.001 n^2 is not O(n).
- Exponent inside a log. log(n^k) = k log n, so log(n^5) is Theta(log n). But (log n)^5 is a different, larger class.
The limit test
When the rules do not decide it, compare f(n) and g(n) with the limit of f(n)/g(n) as n goes to infinity:
| Limit value | Conclusion |
|---|---|
| 0 | f is o(g), so f is O(g) but not Theta(g) |
| a finite constant c > 0 | f is Theta(g) |
| infinity | f is omega(g), so f is Omega(g) but not O(g) |
If the limit gives 0/0 or infinity/infinity, apply L'Hôpital's rule by differentiating the top and bottom. For example, compare n and log n:
lim n / ln n = lim 1 / (1/n) = lim n = infinity
So n is omega(log n). The limit test is sufficient but not necessary. Some pairs have no limit at all, such as n and n^(1 + sin n). Those are rare in exam questions.
Taking logs to compare
For expressions with exponents, take the log of both sides and compare the results. This works because log is increasing, but be careful: it only preserves order, not Theta. If log f is much larger than log g (by more than an additive constant), then f is much larger than g.
Example: compare n^(log n) and 2^(√n).
- log of n^(log n) is (log n)^2.
- log of 2^(√n) is √n.
Since √n beats (log n)^2, we get 2^(√n) larger than n^(log n) for large n. So n^(log n) is o(2^(√n)).
A second example: compare 2^n and n^(log n). The logs are n and (log n)^2. Linear beats polylog, so 2^n is larger.
Another classic: n! versus 2^n. Stirling's approximation gives log(n!) = Theta(n log n), while log(2^n) = n. Since n log n beats n, n! beats 2^n. Also, n! is o(n^n), because n! is roughly (n/e)^n times a smaller factor.
Checking claims with code
You can test an ordering numerically. Note that this shows behavior for the sizes you try, so treat it as evidence and not proof. Small n can mislead, as the crossover points below show.
import math
functions = {
"log n": lambda n: math.log2(n),
"sqrt n": lambda n: math.sqrt(n),
"n": lambda n: n,
"n log n": lambda n: n * math.log2(n),
"n^2": lambda n: n ** 2,
"2^n": lambda n: 2 ** n,
}
for n in (10, 20, 40, 80):
row = {name: f(n) for name, f in functions.items()}
ranked = sorted(row, key=row.get)
print(n, ranked)For these sizes the ranking comes out as log n, sqrt n, n, n log n, n^2, 2^n. Be aware that a function such as 100n beats n^2 only when n is above 100. Asymptotic order says nothing about small inputs.
Common mistakes
- Treating O as an exact rate. Saying n is O(n^2) is true, but it is not tight. If a question asks for Theta, give the tight class.
- Ignoring the base of an exponential. Students drop constants correctly from n and then wrongly treat 2^n and 3^n as the same.
- Mixing up log(n^2) and (log n)^2. The first is 2 log n, which is Theta(log n). The second grows faster.
- Assuming 2^(2n) is O(2^n). It equals 4^n, which is strictly larger.
- Comparing by small n. Evaluating at n = 2 or n = 10 can reverse the true order. Always reason about large n.
- Forgetting that sums are dominated by the largest term. The sum 1 + 2 + ... + n is Theta(n^2), while n terms each of cost log n give Theta(n log n).
Worked ordering
Order these from slowest to fastest growing: n^2, 2^n, n log n, log(n!), n^(1.5), 4^(log n).
- Simplify the odd ones. log(n!) is Theta(n log n). 4^(log n) = 2^(2 log n) = n^2.
- Group them: n log n and log(n!) are the same class. n^2 and 4^(log n) are the same class.
- Rank the classes: n log n, then n^1.5, then n^2, then 2^n.
Result: n log n (equal to log(n!)), n^1.5, n^2 (equal to 4^(log n)), 2^n.
Exercise
Is 2^(n+1) in O(2^n)? Is 2^(2n) in O(2^n)? Give a reason for each.
Show answer
Yes for 2^(n+1), because 2^(n+1) = 2 · 2^n, which is a constant multiple of 2^n, so it is Theta(2^n). No for 2^(2n), because 2^(2n) = 4^n, and 4^n / 2^n = 2^n goes to infinity, so no constant c bounds the ratio.
Exercise
Rank n^(log n), (log n)^n, and 2^n from slowest to fastest growing.
Show answer
Take logs. The log of n^(log n) is (log n)^2. The log of 2^n is n. The log of (log n)^n is n log log n. Since (log n)^2 is smaller than n, which is smaller than n log log n (for large n), the order is n^(log n), then 2^n, then (log n)^n.
Exercise
Use the limit test to decide whether n^2 + n log n is Theta(n^2).
Show answer
Compute (n^2 + n log n) / n^2 = 1 + (log n) / n. Since (log n) / n goes to 0, the limit is 1, a finite positive constant, so the function is Theta(n^2).