TExcelTAD
Home › Foundations of Algorithm Analysis › O, Omega and Theta

O, Omega and Theta

Counting steps exactly gives a worst case as a polynomial with awkward coefficients, something like 32n2+72n−4\frac{3}{2}n^2 + \frac{7}{2}n - 4. Exact counts are useful, but they carry detail nobody needs when comparing algorithms: the constant in front depends on what you call a step, and the lower-order terms vanish next to the leading one once nn is large. Asymptotic notation throws that detail away on purpose and keeps the growth rate. This lesson treats the three workhorses, OO, Ω\Omega and Θ\Theta, as tools for proofs: how to pick the constants, how to show a bound is false, which algebraic rules hold, and what a formula like 2n2+Θ(n)2n^2 + \Theta(n) actually means.

The definitions and a first set of rules are in Asymptotic Notation in the Discrete Mathematics course. Here they are applied to running times.

Functions we compare

A running time T(n)T(n) is defined for whole numbers n≥0n \ge 0 and is never negative. The notation below is stated for functions that are asymptotically nonnegative: f(n)≥0f(n) \ge 0 for all large enough nn. Every running time qualifies, and so does something like n−100n - 100, which is negative only for small nn.

O: an upper bound

O(g(n))={ f(n):there are constants c>0 and n0 such that 0≤f(n)≤c g(n) for all n≥n0 }O(g(n)) = \{\, f(n) : \text{there are constants } c > 0 \text{ and } n_0 \text{ such that } 0 \le f(n) \le c\,g(n) \text{ for all } n \ge n_0 \,\}

O(g(n))O(g(n)) is a set of functions, those that gg eventually bounds from above, up to a constant factor. To prove f(n)=O(g(n))f(n) = O(g(n)) you exhibit two numbers, cc and n0n_0, called the witnesses, and show the inequality for every n≥n0n \ge n_0.

Example. Take f(n)=4n2+25n+6f(n) = 4n^2 + 25n + 6 and g(n)=n2g(n) = n^2. There are many valid pairs of witnesses:

  • c=35c = 35, n0=1n_0 = 1: for n≥1n \ge 1, 25n≤25n225n \le 25n^2 and 6≤6n26 \le 6n^2, so f(n)≤35n2f(n) \le 35n^2. This is the lazy choice: bound every term by the leading power.
  • c=5c = 5, n0=26n_0 = 26: f(n)≤5n2f(n) \le 5n^2 is the same as 25n+6≤n225n + 6 \le n^2, which holds from n=26n = 26 on (650+6=656≤676650 + 6 = 656 \le 676) and fails at n=25n = 25 (631>625631 > 625).

A smaller cc forces a larger n0n_0, and c=4c = 4 never works, because 4n2+25n+6>4n24n^2 + 25n + 6 > 4n^2 for every nn. The program finds the smallest n0n_0 for several choices of cc by checking every nn up to 10,000. A check over a finite range is not a proof, but here c n2−f(n)c\,n^2 - f(n) is a quadratic with positive leading coefficient once c>4c > 4, so once it turns non-negative it stays so.

python · try it yourself
def f(n):
    return 4 * n * n + 25 * n + 6

def smallest_n0(c, limit=10_000):
    last_bad = None
    for n in range(1, limit + 1):
        if f(n) > c * n * n:
            last_bad = n
    if last_bad == limit:
        return None
    return 1 if last_bad is None else last_bad + 1

for c in [4, 4.5, 5, 6, 10, 35]:
    n0 = smallest_n0(c)
    print(f"c = {c:>4}: " + ("no n0 up to 10000" if n0 is None else f"smallest n0 = {n0}"))
c =    4: no n0 up to 10000
c =  4.5: smallest n0 = 51
c =    5: smallest n0 = 26
c =    6: smallest n0 = 13
c =   10: smallest n0 = 5
c =   35: smallest n0 = 1

Omega: a lower bound

Ω(g(n))={ f(n):there are constants c>0 and n0 such that 0≤c g(n)≤f(n) for all n≥n0 }\Omega(g(n)) = \{\, f(n) : \text{there are constants } c > 0 \text{ and } n_0 \text{ such that } 0 \le c\,g(n) \le f(n) \text{ for all } n \ge n_0 \,\}

The same ff is in Ω(n2)\Omega(n^2) with c=4c = 4 and n0=1n_0 = 1, since the extra terms 25n+625n + 6 are positive. Lower bounds are usually the easy direction for a sum of positive terms (drop all but one) and the hard direction for a difference: n2−50n=Ω(n2)n^2 - 50n = \Omega(n^2) needs n2−50n≥12n2n^2 - 50n \ge \frac{1}{2}n^2, which holds once n≥100n \ge 100.

Theta: both at once

Θ(g(n))={ f(n):there are constants c1,c2>0 and n0 such that 0≤c1g(n)≤f(n)≤c2g(n) for all n≥n0 }\Theta(g(n)) = \{\, f(n) : \text{there are constants } c_1, c_2 > 0 \text{ and } n_0 \text{ such that } 0 \le c_1 g(n) \le f(n) \le c_2 g(n) \text{ for all } n \ge n_0 \,\}

f(n)=Θ(g(n))f(n) = \Theta(g(n)) says that gg is an asymptotically tight bound: ff is sandwiched between two constant multiples of gg. The connection with the other two is exact.

Theorem. f(n)=Θ(g(n))f(n) = \Theta(g(n)) if and only if f(n)=O(g(n))f(n) = O(g(n)) and f(n)=Ω(g(n))f(n) = \Omega(g(n)).

One direction is immediate: the sandwich contains both inequalities. For the other, if the OO bound holds from n1n_1 with constant c2c_2 and the Ω\Omega bound from n2n_2 with constant c1c_1, both hold from n0=max⁡(n1,n2)n_0 = \max(n_1, n_2). So 4n2+25n+6=Θ(n2)4n^2 + 25n + 6 = \Theta(n^2), with c1=4c_1 = 4, c2=5c_2 = 5, n0=26n_0 = 26.

Showing that a bound is false

To show f(n)≠O(g(n))f(n) \ne O(g(n)) you must defeat every pair of witnesses. The standard argument is by contradiction.

Claim. n2≠O(n)n^2 \ne O(n). Suppose n2≤c nn^2 \le c\,n for all n≥n0n \ge n_0. Dividing by nn gives n≤cn \le c for all n≥n0n \ge n_0, which fails at n=max⁡(n0,⌈c⌉+1)n = \max(n_0, \lceil c \rceil + 1). No constant cc can cap a quantity that grows without bound.

The same pattern shows 2n+1=O(2n)2^{n+1} = O(2^n) (take c=2c = 2) but 22n≠O(2n)2^{2n} \ne O(2^n): the ratio 22n/2n=2n2^{2n} / 2^n = 2^n is unbounded. A constant in the exponent is not a constant factor.

Rules you can use without re-proving them

Write f∈X(g)f \in X(g) for each kind of bound. The following hold for all asymptotically positive ff, gg, hh:

Property Statement
Transitivity f=O(g)f = O(g) and g=O(h)g = O(h) imply f=O(h)f = O(h); the same for Ω\Omega and Θ\Theta
Reflexivity f=O(f)f = O(f), f=Ω(f)f = \Omega(f), f=Θ(f)f = \Theta(f)
Symmetry f=Θ(g)f = \Theta(g) if and only if g=Θ(f)g = \Theta(f)
Transpose symmetry f=O(g)f = O(g) if and only if g=Ω(f)g = \Omega(f)
Constant factors a f=Θ(f)a\,f = \Theta(f) for every constant a>0a > 0
Sums f+g=Θ(max⁡(f,g))f + g = \Theta(\max(f, g))

Transitivity multiplies the constants: from f≤c gf \le c\,g (for n≥n1n \ge n_1) and g≤d hg \le d\,h (for n≥n2n \ge n_2) you get f≤cd hf \le cd\,h for n≥max⁡(n1,n2)n \ge \max(n_1, n_2). Transpose symmetry divides by the constant: f≤c gf \le c\,g is the same as 1cf≤g\frac{1}{c} f \le g. These rules make Θ\Theta behave like equality and OO, Ω\Omega like ≤\le and ≥\ge, with one big difference: not every pair of functions is comparable. The next lesson, Little o, Little omega and Limits, shows an example.

Notation inside formulas

The equals sign in f(n)=O(g(n))f(n) = O(g(n)) means "is a member of". It is a one-way equals: n=O(n2)n = O(n^2) is true, and writing it backwards, O(n2)=nO(n^2) = n, says nothing.

When asymptotic notation appears inside a larger formula, it stands for an anonymous function from that set. For example

T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n)

means T(n)=2T(n/2)+h(n)T(n) = 2T(n/2) + h(n) for some function h(n)h(n) in Θ(n)\Theta(n) that we don't care to name, such as the cost of merging. A chain like 2n2+3n+1=2n2+Θ(n)=Θ(n2)2n^2 + 3n + 1 = 2n^2 + \Theta(n) = \Theta(n^2) is read left to right: whatever anonymous functions are chosen on the left, the right side can be made true. Each step may lose information and never gains any.

A classic trap. Here is a wrong "proof" that S(n)=1+2+⋯+nS(n) = 1 + 2 + \cdots + n is O(n)O(n), by induction. Base: S(1)=1=O(1)S(1) = 1 = O(1). Step: if S(n−1)=O(n)S(n-1) = O(n), then S(n)=S(n−1)+n=O(n)+n=O(n)S(n) = S(n-1) + n = O(n) + n = O(n). The error is that O(n)O(n) is not a statement about a single nn. A proof of S(n)≤c nS(n) \le c\,n must use the same constant cc for every nn, and the step quietly raises the constant by one each time. Written with an explicit constant, the induction step would need c(n−1)+n≤c nc(n-1) + n \le c\,n, that is n≤cn \le c, which fails. The ratio S(n)/n=(n+1)/2S(n)/n = (n+1)/2 grows without bound:

python · try it yourself
for n in [10, 100, 1000, 10000]:
    s = n * (n + 1) // 2
    print(f"n = {n:>5}: S(n) = {s:>9}, S(n)/n = {s / n:8.1f}, S(n)/n^2 = {s / n**2:.4f}")
n =    10: S(n) =        55, S(n)/n =      5.5, S(n)/n^2 = 0.5500
n =   100: S(n) =      5050, S(n)/n =     50.5, S(n)/n^2 = 0.5050
n =  1000: S(n) =    500500, S(n)/n =    500.5, S(n)/n^2 = 0.5005
n = 10000: S(n) =  50005000, S(n)/n =   5000.5, S(n)/n^2 = 0.5000

The last column settles near 12\frac{1}{2}, so S(n)=Θ(n2)S(n) = \Theta(n^2).

O versus Theta for running times

The difference between OO and Θ\Theta matters when you describe an algorithm with a single phrase. Insertion sort's worst case is Θ(n2)\Theta(n^2) (reversed input) and its best case is Θ(n)\Theta(n) (sorted input, see Counting Steps Exactly). Therefore:

  • "The running time of insertion sort is O(n2)O(n^2)" is true: every input of size nn costs at most c n2c\,n^2.
  • "The running time of insertion sort is Ω(n)\Omega(n)" is true: every input costs at least a pass over the array.
  • "The running time of insertion sort is Θ(n2)\Theta(n^2)" is false as a statement about every input: a sorted array runs in linear time. It is true of the worst-case running time.

The program counts the shifts (executions of the inner loop body) on three kinds of input and divides by n2n^2.

python · try it yourself
import random

def shifts(a):
    a = list(a)
    count = 0
    for i in range(1, len(a)):
        key, j = a[i], i - 1
        while j >= 0 and a[j] > key:
            a[j + 1] = a[j]
            j -= 1
            count += 1
        a[j + 1] = key
    return count

rng = random.Random(7)
print(f"{'n':>5} {'sorted':>8} {'random':>8} {'reversed':>9}   random/n^2  reversed/n^2")
for n in [100, 200, 400, 800]:
    up = list(range(n))
    mixed = up[:]
    rng.shuffle(mixed)
    s, r, w = shifts(up), shifts(mixed), shifts(up[::-1])
    print(f"{n:>5} {s:>8} {r:>8} {w:>9}   {r / n**2:10.3f}  {w / n**2:12.3f}")
    n   sorted   random  reversed   random/n^2  reversed/n^2
  100        0     2629      4950        0.263         0.495
  200        0    10604     19900        0.265         0.497
  400        0    37260     79800        0.233         0.499
  800        0   158269    319600        0.247         0.499

Sorted input needs no shifts, reversed input needs exactly n(n−1)/2n(n-1)/2, and a random order lands near n2/4n^2/4. One O(n2)O(n^2) covers all three columns; no single Θ\Theta does.

Test yourself

Which pair of witnesses proves 4n2+25n+6=O(n2)4n^2 + 25n + 6 = O(n^2)?

  1. c=4c = 4, n0=1000n_0 = 1000
  2. c=5c = 5, n0=26n_0 = 26
  3. c=5c = 5, n0=1n_0 = 1
  4. c=1c = 1, n0=26n_0 = 26

Why is 22n≠O(2n)2^{2n} \ne O(2^n)?

  1. Because 22n2^{2n} is negative for some nn
  2. Because the ratio 22n/2n=2n2^{2n} / 2^n = 2^n is unbounded, so no constant cc works
  3. Because the base is the same
  4. It is: take c=2c = 2

In T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n), what does Θ(n)\Theta(n) stand for?

  1. The set of all linear functions, added to 2T(n/2)2T(n/2)
  2. Some unnamed function h(n)h(n) that is in Θ(n)\Theta(n)
  3. Exactly nn
  4. An upper bound only

Which statement about insertion sort is false?

  1. Its running time is O(n2)O(n^2)
  2. Its running time is Ω(n)\Omega(n)
  3. Its worst-case running time is Θ(n2)\Theta(n^2)
  4. Its running time on every input is Θ(n2)\Theta(n^2)