Discrete Mathematics and Combinatorics

Big O Notation From a Mathematical Point of View

Read the definition of big O precisely, prove bounds from it, compare growth rates and avoid the most common misreadings.

Key takeaways

  • f(n) = O(g(n)) is a statement that two constants exist: a multiplier C and a threshold n₀.
  • Big O is an upper bound only, Big Omega is a lower bound, and Big Theta means both at once.
  • Logarithms grow more slowly than any positive power, and powers grow more slowly than any exponential with base above 1.
  • The notation hides constant factors, so it describes large inputs and does not say which method is faster on a particular small one.

Big O notation says that one function grows no faster than a constant multiple of another once the input is large enough. Writing f(n) = O(g(n)) means there are constants C > 0 and n₀ such that |f(n)| ≤ C·g(n) for every n ≥ n₀, where g is positive for those n.

The notation deliberately forgets two things: constant factors, and what happens for small inputs. What remains is the shape of the growth.

Proving a bound from the definition

Take f(n) = 3n² + 5n + 7. We claim f(n) = O(n²). For n ≥ 1 we have 5n ≤ 5n² and 7 ≤ 7n², so

3n² + 5n + 7 ≤ 3n² + 5n² + 7n² = 15n²      for all n ≥ 1

So C = 15 and n₀ = 1 do the job. Other choices work too. With C = 4 you need n² ≥ 5n + 7, which fails at n = 6 (36 against 37) and holds from n = 7 onward. You only have to produce one valid pair.

To show that something is not O(g), you must show that every choice of C and n₀ fails. Is n² = O(n)? That would need n² ≤ Cn, or n ≤ C, for all large n. No constant is larger than every n, so the answer is no.

What the equals sign means

The notation f(n) = O(g(n)) uses "=" in an unusual way. O(g(n)) stands for a whole collection of functions, and the statement means that f belongs to it. You cannot reverse it: writing O(n²) = n would be meaningless, and from f = O(n²) and h = O(n²) you may not conclude that f = h.

Some authors write f ∈ O(g), using the symbol for membership in a set. Both styles are standard. The input variable and the direction of the limit should be clear from context.

Omega, Theta and little o

Big O has relatives that say other things.

Notation Meaning for large n Rough analogy
f = O(g) f is at most a constant times g ≤
f = Ω(g) f is at least a positive constant times g ≥
f = Θ(g) both of the above =
f = o(g) f/g tends to 0 <

So 3n² + 5n + 7 is Θ(n²). It is also O(n³), which is true but less informative, and it is o(n³). In everyday programming talk people often say "O" when they mean "Θ". In mathematics, keep them apart.

The table assumes nonnegative functions. The analogy is loose: two functions need not be comparable at all, as when one oscillates above and below the other forever.

Common growth rates, fastest growing first
Exponential: 2ⁿ Each extra item doubles the work Quadratic: n² Doubling n multiplies by four n log n Typical of good sorting methods Linear: n Doubling n doubles the work Logarithmic: log n Doubling n adds a fixed amount Constant: 1 Does not grow with n Exponential: 2ⁿ Each extra item doubles the work Quadratic: n² Doubling n multiplies by four n log n Typical of good sorting methods Linear: n Doubling n doubles the work Logarithmic: log n Doubling n adds a fixed amount Constant: 1 Does not grow with n

How far apart the rates are

Here log₂ means the logarithm to base 2. Values are rounded.

n log₂ n n log₂ n n² 2ⁿ
10 3.3 33 100 1,024
100 6.6 664 10,000 about 1.3 × 10³⁰
1,000 10 9,966 1,000,000 about 1.1 × 10³⁰¹

Inside big O the base of a logarithm does not matter, provided it is a constant above 1, since changing base only multiplies by a constant: log₂ n = ln n / ln 2. The base of an exponential matters a great deal. 4ⁿ is not O(2ⁿ), because their ratio is 2ⁿ, which grows without bound.

Rules for calculating

You rarely return to the definition once you have these facts.

  • Constant factors vanish: O(7n) is the same as O(n).
  • In a sum of a fixed number of terms, the fastest-growing term wins: n³ + 100n² is O(n³).
  • Products multiply: if f = O(a) and h = O(b), then f·h = O(a·b).
  • Bounds chain: if f = O(g) and g = O(h), then f = O(h).
  • For constants k > 0 and b > 1, (ln n)ᵏ is o(n^c) for every c > 0, and nᵏ is o(bⁿ).

A limit often settles the matter. For positive functions, if f(n)/g(n) tends to a finite positive number, then f = Θ(g). If it tends to 0, then f = o(g). The limit need not exist for a big O statement to be true: (2 + sin n)·n is Θ(n) although its ratio to n never settles.

Be careful with sums whose number of terms grows. The sum 1 + 2 + ... + n has n terms each at most n, and it equals n(n + 1)/2, which is Θ(n²), not O(n).

Counting steps in loops

Bounds usually come from a count, so practice turning a procedure into a sum.

Suppose a procedure compares every item in a list of n with every later item. The first item is compared n − 1 times, the second n − 2 times, and so on.

(n − 1) + (n − 2) + ... + 1 + 0 = n(n − 1)/2 = Θ(n²)

The same number is C(n, 2), the number of pairs. Checking every triple takes C(n, 3) = n(n − 1)(n − 2)/6 steps, which is Θ(n³). In general, for a fixed k, the number of ways to choose k items from n is Θ(nᵏ), a fact that follows from the combination formula.

Now suppose a counter starts at 1 and doubles until it exceeds n. After t doublings it equals 2ᵗ, so the loop stops after about log₂ n steps: Θ(log n).

A third loop does n units of work, then n/2, then n/4, down to 1. Although there are about log₂ n rounds, the total is less than 2n by the geometric series, so it is Θ(n). Multiplying the number of rounds by the largest round would give O(n log n), which is true but not sharp.

The doubling test

Growth classes predict what happens when the input doubles. If the step count is close to c·n², doubling n multiplies it by about 4. For c·n³ the factor is about 8. For n log n it is a little over 2. For c·2ⁿ, doubling n squares the factor 2ⁿ, a catastrophe by comparison.

You can run this in reverse. Say a hypothetical program takes 2 seconds on 10,000 records and 8 seconds on 20,000. The factor of 4 suggests quadratic growth, and about 32 seconds for 40,000. Treat that as evidence and not proof, since memory effects and lower-order terms can distort small measurements.

A bound for the factorial

How fast does log(n!) grow? Here n! is the product of the whole numbers from 1 to n, and ln is the natural logarithm. Write ln(n!) = ln 1 + ln 2 + ... + ln n.

upper:  each of n terms is at most ln n,   so ln(n!) ≤ n ln n
lower:  at least n/2 terms are at least ln(n/2),
        so ln(n!) ≥ (n/2) ln(n/2)           for n ≥ 2

For large n, ln(n/2) is more than half of ln n, so the lower bound is at least a quarter of n ln n. Therefore ln(n!) = Θ(n ln n). This fact shows that any sorting method that works only by comparing pairs needs on the order of n log n comparisons in its worst case: it must distinguish n! possible orders, and each comparison has two outcomes.

A far sharper estimate is given by Stirling's approximation.

Error terms in analysis

The notation began in number theory and analysis. Paul Bachmann used it in the 1890s, and Edmund Landau helped make it standard. There it often measures the error in an approximation.

For example, as x approaches 0,

eˣ = 1 + x + x²/2 + O(x³)

This says the error is at most a constant times |x|³ for all x close enough to 0. Note the direction: the limit is x → 0, so higher powers are the smaller ones. The idea comes from Taylor series.

Another example is the harmonic sum: 1 + 1/2 + ... + 1/n = ln n + γ + O(1/n), where γ is Euler's constant, about 0.5772.

Algorithms and common misreadings

In computing, f(n) is usually a count of basic steps for inputs of size n. Binary search on a sorted list takes O(log n) comparisons, scanning a list takes O(n), and checking every pair takes O(n²). Many divide-and-conquer methods lead to recurrences handled by the master theorem.

  • Big O does not mean "worst case". Worst, best and average cases are different functions, and each can be bounded above or below.
  • A method with a better exponent can be slower in practice. If one takes 1,000n steps and another n², the second wins for every n below 1,000.
  • Size must be defined. A loop that runs up to a number N is linear in N but exponential in the number of digits of N.
  • "O(1)" means bounded by a constant, which could be a very large one.

The division between polynomial and exponential growth is the starting point of computational complexity. For counting arguments that produce the functions being bounded, return to recurrence relations.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic