TExcelTAD
Home › Foundations of Algorithm Analysis › Little o, Little omega and Limits

Little o, Little omega and Limits

OO and Ω\Omega behave like ≤\le and ≥\ge: n2=O(n2)n^2 = O(n^2) is true, just as 5≤55 \le 5 is. Often you want the strict version, "grows strictly slower than", for example to say that nlg⁡nn \lg n is genuinely better than n2n^2 and not merely no worse. That is what little o and little omega express. They are also where limits come in, which turn most asymptotic comparisons into a calculus exercise. The lesson ends with something ≤\le never does: two functions that cannot be compared at all.

The previous lesson, O, Omega and Theta, gave the three big-letter definitions.

Little o: strictly slower

o(g(n))={ f(n):for every constant c>0 there is an n0 such that 0≤f(n)<c g(n) for all n≥n0 }o(g(n)) = \{\, f(n) : \text{for every constant } c > 0 \text{ there is an } n_0 \text{ such that } 0 \le f(n) < c\,g(n) \text{ for all } n \ge n_0 \,\}

Compare the quantifiers with big O. For f=O(g)f = O(g) some constant cc must work. For f=o(g)f = o(g) every constant must work, however small, with an n0n_0 that may depend on cc. In words: ff eventually becomes a negligible fraction of gg.

  • 2n=o(n2)2n = o(n^2). Given cc, the inequality 2n<c n22n < c\,n^2 is n>2/cn > 2/c, so n0=⌊2/c⌋+1n_0 = \lfloor 2/c \rfloor + 1 works. For c=0.001c = 0.001 that is n0=2001n_0 = 2001.
  • 2n2≠o(n2)2n^2 \ne o(n^2). With c=1c = 1 the inequality 2n2<n22n^2 < n^2 never holds.

Little omega: strictly faster

ω(g(n))={ f(n):for every constant c>0 there is an n0 such that 0≤c g(n)<f(n) for all n≥n0 }\omega(g(n)) = \{\, f(n) : \text{for every constant } c > 0 \text{ there is an } n_0 \text{ such that } 0 \le c\,g(n) < f(n) \text{ for all } n \ge n_0 \,\}

n2/2=ω(n)n^2 / 2 = \omega(n), but n2/2≠ω(n2)n^2/2 \ne \omega(n^2). As with the big letters there is a transpose rule: f=ω(g)f = \omega(g) if and only if g=o(f)g = o(f).

The limit test

When g(n)>0g(n) > 0 for large nn, the strict bounds are exactly statements about the ratio:

f(n)=o(g(n))  ⟺  lim⁡n→∞f(n)g(n)=0,f(n)=ω(g(n))  ⟺  lim⁡n→∞f(n)g(n)=∞f(n) = o(g(n)) \iff \lim_{n \to \infty} \frac{f(n)}{g(n)} = 0, \qquad f(n) = \omega(g(n)) \iff \lim_{n \to \infty} \frac{f(n)}{g(n)} = \infty

The first equivalence is the definition of a limit in disguise: "for every c>0c > 0, eventually f/g<cf/g < c" is what lim⁡f/g=0\lim f/g = 0 means. A ratio with a finite positive limit LL gives Θ\Theta: eventually L/2≤f/g≤2LL/2 \le f/g \le 2L, so c1=L/2c_1 = L/2 and c2=2Lc_2 = 2L are witnesses.

The word "eventually" deserves respect. Compare (lg⁡n)3(\lg n)^3 with n\sqrt{n}. Writing n=2kn = 2^k, so that lg⁡n=k\lg n = k, the ratio is k3/2k/2k^3 / 2^{k/2}:

python · try it yourself
for k in [10, 20, 30, 40, 60, 80]:
    ratio = k**3 / 2 ** (k / 2)
    print(f"n = 2^{k:<3} (lg n)^3 / sqrt(n) = {ratio:.3g}")
n = 2^10  (lg n)^3 / sqrt(n) = 31.2
n = 2^20  (lg n)^3 / sqrt(n) = 7.81
n = 2^30  (lg n)^3 / sqrt(n) = 0.824
n = 2^40  (lg n)^3 / sqrt(n) = 0.061
n = 2^60  (lg n)^3 / sqrt(n) = 0.000201
n = 2^80  (lg n)^3 / sqrt(n) = 4.66e-07

At n=210=1024n = 2^{10} = 1024 the polylogarithm is 31 times larger. The limit is still 0, so (lg⁡n)3=o(n)(\lg n)^3 = o(\sqrt{n}); the polylogarithm only falls behind for good near n=229.3n = 2^{29.3}, about 660 million. A table of small nn can point the wrong way.

Computing limits with L'Hopital's rule

To find a limit of a ratio of functions of an integer nn, treat nn as a real variable xx. If both f(x)f(x) and g(x)g(x) go to infinity and are differentiable, L'Hopital's rule says lim⁡f/g=lim⁡f′/g′\lim f/g = \lim f'/g', whenever the second limit exists.

Logarithms lose to every power. For any ε>0\varepsilon > 0:

lim⁡x→∞ln⁡xxε=lim⁡x→∞1/xεxε−1=lim⁡x→∞1εxε=0\lim_{x \to \infty} \frac{\ln x}{x^{\varepsilon}} = \lim_{x \to \infty} \frac{1/x}{\varepsilon x^{\varepsilon - 1}} = \lim_{x \to \infty} \frac{1}{\varepsilon x^{\varepsilon}} = 0

So ln⁡n=o(nε)\ln n = o(n^{\varepsilon}), even for ε=0.01\varepsilon = 0.01. Raising both sides to a power kk gives (ln⁡n)k=o(nkε)(\ln n)^k = o(n^{k\varepsilon}), and since ε\varepsilon was arbitrary, every power of a logarithm is o(nδ)o(n^{\delta}) for every δ>0\delta > 0.

Powers lose to every exponential. For an integer k≥1k \ge 1 and a>1a > 1, apply the rule kk times; each time the power drops by one and the exponential stays:

lim⁡x→∞xkax=lim⁡x→∞k xk−1(ln⁡a) ax=⋯=lim⁡x→∞k!(ln⁡a)k ax=0\lim_{x \to \infty} \frac{x^k}{a^x} = \lim_{x \to \infty} \frac{k\,x^{k-1}}{(\ln a)\,a^x} = \cdots = \lim_{x \to \infty} \frac{k!}{(\ln a)^k\,a^x} = 0

So n100=o(1.01n)n^{100} = o(1.01^n). The Polynomials, Exponentials and Logarithms lesson uses both facts constantly.

Limits are sufficient, not necessary

The limit test only applies when the limit exists. A function can be Θ(n)\Theta(n) with a ratio that never settles. Take

f(n)={3nn evennn oddf(n) = \begin{cases} 3n & n \text{ even} \\ n & n \text{ odd} \end{cases}

Then n≤f(n)≤3nn \le f(n) \le 3n for all nn, so f=Θ(n)f = \Theta(n) with c1=1c_1 = 1, c2=3c_2 = 3, n0=1n_0 = 1, yet f(n)/nf(n)/n alternates between 3 and 1 forever. Running times of this shape are common: an algorithm that does extra work when nn is even, or when nn is not a power of two. When the limit fails to exist, go back to the definitions and find witnesses.

The same example shows that O(g)O(g) minus Θ(g)\Theta(g) is bigger than o(g)o(g). Define h(n)=nh(n) = n for even nn and h(n)=1h(n) = 1 for odd nn. Then h=O(n)h = O(n), but h≠Ω(n)h \ne \Omega(n) (the odd values stay at 1) and h≠o(n)h \ne o(n) (the even values equal nn).

Two functions that cannot be compared

For real numbers, exactly one of a<ba < b, a=ba = b, a>ba > b holds. This trichotomy fails for functions. The classic counterexample is nn against n1+sin⁡nn^{1 + \sin n}. Their ratio is nsin⁡nn^{\sin n}. Because the integers are spread evenly around the circle modulo 2π2\pi, sin⁡n\sin n comes arbitrarily close to +1+1 and to −1-1 infinitely often. Near +1+1 the ratio is close to nn, near −1-1 close to 1/n1/n. So the ratio is neither bounded above (no OO) nor bounded away from 0 (no Ω\Omega).

The program looks at each range of nn between two powers of ten and reports the largest and smallest value of nsin⁡nn^{\sin n} in it.

python · try it yourself
import math

for e in range(1, 6):
    lo, hi = 10**e, 10 ** (e + 1)
    vals = [(n ** math.sin(n), n) for n in range(lo, hi)]
    big, n_big = max(vals)
    small, n_small = min(vals)
    print(f"{lo:>6} <= n < {hi:<7} max {big:10.1f} at n = {n_big:<6} min {small:.2e} at n = {n_small}")
    10 <= n < 100     max       89.1 at n = 96     min 1.01e-02 at n = 99
   100 <= n < 1000    max      984.8 at n = 988    min 1.04e-03 at n = 966
  1000 <= n < 10000   max     9963.8 at n = 9973   min 1.00e-04 at n = 9995
 10000 <= n < 100000  max    99708.7 at n = 99722  min 1.00e-05 at n = 99744
100000 <= n < 1000000 max   999821.3 at n = 999826 min 1.00e-06 at n = 999471

The maximum keeps growing roughly like nn and the minimum keeps shrinking roughly like 1/n1/n. In the language of the analogy, nn is neither "at most" nor "at least" n1+sin⁡nn^{1 + \sin n}. Such pairs rarely come up as running times, but they are why a statement like "every function is OO, Θ\Theta or Ω\Omega of every other" is false.

The analogy, summed up

Notation Read as Like Ratio f/gf/g when the limit exists
f=O(g)f = O(g) at most a≤ba \le b finite
f=Ω(g)f = \Omega(g) at least a≥ba \ge b positive or ∞\infty
f=Θ(g)f = \Theta(g) same order a=ba = b finite and positive
f=o(g)f = o(g) strictly slower a<ba < b 0
f=ω(g)f = \omega(g) strictly faster a>ba > b ∞\infty

All five are transitive, OO, Ω\Omega and Θ\Theta are reflexive while oo and ω\omega never are (f≠o(f)f \ne o(f)), and f=o(g)f = o(g) implies f=O(g)f = O(g) but f≠Ω(g)f \ne \Omega(g).

Test yourself

What is the essential difference between f=O(g)f = O(g) and f=o(g)f = o(g)?

  1. OO needs some constant cc to work; oo needs every constant c>0c > 0 to work
  2. oo allows ff to be negative
  3. oo is only defined for polynomials
  4. There is none; they are synonyms

At n=1024n = 1024, (lg⁡n)3=1000(\lg n)^3 = 1000 and n=32\sqrt{n} = 32. What follows?

  1. (lg⁡n)3=ω(n)(\lg n)^3 = \omega(\sqrt{n})
  2. (lg⁡n)3=Θ(n)(\lg n)^3 = \Theta(\sqrt{n})
  3. Nothing about growth: the limit of (lg⁡n)3/n(\lg n)^3/\sqrt{n} is 0, so (lg⁡n)3=o(n)(\lg n)^3 = o(\sqrt{n})
  4. The two functions are not comparable

f(n)=3nf(n) = 3n for even nn and f(n)=nf(n) = n for odd nn. Which is true?

  1. f=Θ(n)f = \Theta(n), although lim⁡f(n)/n\lim f(n)/n does not exist
  2. ff is not Θ(n)\Theta(n) because the limit does not exist
  3. f=o(n)f = o(n)
  4. f=ω(n)f = \omega(n)

Why are nn and n1+sin⁡nn^{1 + \sin n} not comparable?

  1. Because sin⁡n\sin n is negative
  2. Because their ratio nsin⁡nn^{\sin n} gets arbitrarily large and arbitrarily close to 0 infinitely often
  3. Because n1+sin⁡nn^{1 + \sin n} is not defined for integers
  4. They are comparable: n=O(n1+sin⁡n)n = O(n^{1 + \sin n})