TExcelTAD
Home › Discrete Mathematics › Asymptotic Notation

Asymptotic Notation

How does a function behave when nn gets large? Asymptotic notation ignores constant factors and small inputs and keeps only the growth rate. It is the language of algorithm analysis: "merge sort is Θ(nlog⁡n)\Theta(n \log n)" says how its running time scales, independent of the computer. For an informal first look see Growth Rates and Big-O and Time Complexity; this lesson gives the precise definitions.

Definitions

For functions f,gf, g from N\mathbb{N} to the positive reals:

Notation Meaning Definition
f=O(g)f = O(g) ff grows at most as fast as gg ∃c>0,n0. ∀n≥n0. f(n)≤c⋅g(n)\exists c > 0, n_0.\ \forall n \ge n_0.\ f(n) \le c \cdot g(n)
f=Ω(g)f = \Omega(g) at least as fast g=O(f)g = O(f)
f=Θ(g)f = \Theta(g) same rate f=O(g)f = O(g) and f=Ω(g)f = \Omega(g)
f=o(g)f = o(g) strictly slower lim⁡n→∞f(n)/g(n)=0\lim_{n \to \infty} f(n)/g(n) = 0
f∼gf \sim g asymptotically equal lim⁡n→∞f(n)/g(n)=1\lim_{n \to \infty} f(n)/g(n) = 1

The "==" is a convention: f=O(g)f = O(g) means ff belongs to the set O(g)O(g). It is not symmetric: n=O(n2)n = O(n^2) but n2≠O(n)n^2 \ne O(n).

Example. 3n2+10n+50=Θ(n2)3n^2 + 10n + 50 = \Theta(n^2): for n≥10n \ge 10, 10n≤n210n \le n^2 and 50≤n250 \le n^2, so the expression is at most 5n25n^2 (take c=5c = 5, n0=10n_0 = 10); and it is at least 3n23n^2.

Limits make it easy

If lim⁡n→∞f(n)g(n)=L\lim_{n \to \infty} \frac{f(n)}{g(n)} = L exists, then L=0L = 0 means f=o(g)f = o(g), 0<L<∞0 < L < \infty means f=Θ(g)f = \Theta(g), and L=∞L = \infty means g=o(f)g = o(f). Using L'Hôpital's rule or known limits:

  • every polynomial beats every logarithm: (log⁡n)k=o(nε)(\log n)^k = o(n^\varepsilon) for any ε>0\varepsilon > 0;
  • every exponential beats every polynomial: nk=o(cn)n^k = o(c^n) for c>1c > 1;
  • n!=o(nn)n! = o(n^n) and cn=o(n!)c^n = o(n!).
python · try it yourself
import math

funcs = {
    "log2 n": lambda n: math.log2(n),
    "sqrt n": lambda n: math.sqrt(n),
    "n": lambda n: n,
    "n log2 n": lambda n: n * math.log2(n),
    "n^2": lambda n: n ** 2,
    "2^n": lambda n: 2.0 ** n,
}
print(f"{'n':>6} " + " ".join(f"{k:>10}" for k in funcs))
for n in [10, 20, 40, 80]:
    print(f"{n:>6} " + " ".join(f"{f(n):10.3g}" for f in funcs.values()))
     n     log2 n     sqrt n          n   n log2 n        n^2        2^n
    10       3.32       3.16         10       33.2        100   1.02e+03
    20       4.32       4.47         20       86.4        400   1.05e+06
    40       5.32       6.32         40        213    1.6e+03    1.1e+12
    80       6.32       8.94         80        506    6.4e+03   1.21e+24

Doubling nn adds 1 to log⁡2n\log_2 n, multiplies n2n^2 by 4, and squares 2n2^n.

Rules

  • Constants don't matter: c⋅f=Θ(f)c \cdot f = \Theta(f) for c>0c > 0; so log⁡2n=Θ(log⁡10n)\log_2 n = \Theta(\log_{10} n) and the base of a log is omitted.
  • Sums keep the larger term: f+g=Θ(max⁡(f,g))f + g = \Theta(\max(f, g)).
  • Products multiply: if f1=O(g1)f_1 = O(g_1) and f2=O(g2)f_2 = O(g_2) then f1f2=O(g1g2)f_1 f_2 = O(g_1 g_2).
  • Transitivity: f=O(g)f = O(g) and g=O(h)g = O(h) give f=O(h)f = O(h).

Pitfalls

  • Big-O is an upper bound only. "Insertion sort is O(n3)O(n^3)" is true but useless; say Θ(n2)\Theta(n^2) (worst case) when you can.
  • Constants matter in practice. An O(n)O(n) algorithm with constant 10610^6 loses to an O(n2)O(n^2) one with constant 1 for every n<106n < 10^6. Library sorts switch to insertion sort for small arrays for exactly this reason.
  • Don't apply Big-O inside an induction. "n=O(1)n = O(1) for all nn by induction" is nonsense: the constant cc must not change with nn.
  • Exponents are not constants: 4n=22n4^n = 2^{2n} is not O(2n)O(2^n), since 4n2n=2n→∞\frac{4^n}{2^n} = 2^n \to \infty.
python · try it yourself
import math

# limits of ratios decide the relation
pairs = [("n log n", "n^1.1", lambda n: n * math.log(n) / n ** 1.1),
         ("n^3", "2^n", lambda n: n ** 3 / 2 ** n),
         ("4^n", "2^n", lambda n: 4 ** n / 2 ** n),
         ("3n^2+10n+50", "n^2", lambda n: (3 * n * n + 10 * n + 50) / (n * n))]
for f, g, ratio in pairs:
    print(f"{f:>12} / {g:<6}:", " ".join(f"{ratio(10 ** k):.3g}" for k in [1, 2, 3]))
     n log n / n^1.1 : 1.83 2.91 3.46
         n^3 / 2^n   : 0.977 7.89e-25 9.33e-293
         4^n / 2^n   : 1.02e+03 1.27e+30 1.07e+301
 3n^2+10n+50 / n^2   : 4.5 3.1 3.01

The first ratio does go to 0, but only for very large nn (it starts decreasing after n=e10≈22026n = e^{10} \approx 22026): asymptotic statements can hide large thresholds n0n_0.

In software

Complexity classes decide what is feasible: 10910^9 simple operations take about a second. So at n=106n = 10^6, nlog⁡nn \log n is instant, n2n^2 takes minutes, and 2n2^n is out of the question at n=60n = 60. Profilers measure constants; asymptotics predict how a program behaves when the data grows by 10× or 1000×.

Exercises

Exercise 1: Prove a Big-O

Show 5n3+2n+7=O(n3)5n^3 + 2n + 7 = O(n^3) with explicit cc and n0n_0.

Answer

c=14c = 14, n0=1n_0 = 1 works.

Solution

For n≥1n \ge 1: 2n≤2n32n \le 2n^3 and 7≤7n37 \le 7n^3, so 5n3+2n+7≤14n35n^3 + 2n + 7 \le 14n^3.

Exercise 2: Order these

Sort by growth rate: n2n^2, 2log⁡2n2^{\log_2 n}, nlog⁡nn \log n, n\sqrt{n}, 1.01n1.01^n, log⁡(n!)\log(n!), n100n^{100}.

Answer

n\sqrt{n} < 2log⁡2n2^{\log_2 n} (which is nn) < nlog⁡nn \log n and log⁡(n!)\log(n!) (both Θ(nlog⁡n)\Theta(n \log n)) < n2n^2 < n100n^{100} < 1.01n1.01^n.

Solution

2log⁡2n=n2^{\log_2 n} = n. By Stirling, log⁡(n!)=Θ(nlog⁡n)\log(n!) = \Theta(n \log n). Any exponential with base greater than 1, even 1.01n1.01^n, eventually beats any polynomial such as n100n^{100} (although only for astronomically large nn).

Exercise 3: True or false

(a) 2n+1=O(2n)2^{n+1} = O(2^n); (b) 22n=O(2n)2^{2n} = O(2^n); (c) log⁡(n5)=Θ(log⁡n)\log(n^5) = \Theta(\log n).

Answer

(a) true; (b) false; (c) true.

Solution

(a) 2n+1=2⋅2n2^{n+1} = 2 \cdot 2^n, a constant factor. (b) 22n/2n=2n→∞2^{2n} / 2^n = 2^n \to \infty. (c) log⁡(n5)=5log⁡n\log(n^5) = 5 \log n.

Going further: the Algorithm Theory track applies asymptotic notation to running times, with recursion trees and the master theorem.

Test yourself

Which is Θ(n2)\Theta(n^2)?

  1. n2log⁡nn^2 \log n
  2. 100n+n2/3100n + n^2 / 3
  3. n3/1000n^3 / 1000
  4. 2n2^n

If lim⁡f(n)/g(n)=0\lim f(n)/g(n) = 0, then…

  1. f=Θ(g)f = \Theta(g)
  2. f=o(g)f = o(g)
  3. g=O(f)g = O(f)
  4. f=Ω(g)f = \Omega(g)

Is 4n=O(2n)4^n = O(2^n)?

  1. Yes, constants don't matter
  2. No, 4n/2n=2n4^n / 2^n = 2^n is unbounded
  3. Only for small nn
  4. Yes, by L'Hôpital