What O(f) actually is

After thiswhat you will be able to doTest whether a cost belongs to O(f) by producing a valid multiplier and threshold, and explain why O(f) is a set rather than one value.

Questionwhat this lesson answersWe write T(n) = O(n^2) with an equals sign, yet you cannot swap the two sides and the right side is not a single number. What kind of object is O(n^2), such that a cost can be said to equal it?

Not coveredwhat this lesson leaves outWe define what it means to belong to O(f) and we find the two witnesses by hand for one function. We do not prove the rules for combining O(...) expressions, and we leave the lower bound alone.

There is a strange convention in algorithm writing. We write T(n)=O(n2){T(n) = O(n^2)} with an equals sign, even though the line is not an ordinary equality. You would never turn it around and write O(n2)=T(n){O(n^2) = T(n)}. The left side is a rule that gives a step count for each input size. The right side is not one number at all, and it does not even stand for one particular cost. So the equals sign is standing in for something else, and the rest of this lesson is about what.

First notice what the left side is. A rule that takes an input and returns a number is called a function. The cost rule T{T} takes an input size and returns a number of steps. A collection can hold more than one such rule, just as a shelf can hold more than one book. In mathematics, a collection treated as one object is called a set. To say that one thing is among the things in a set is called membership.

That is the kind of object on the right. O(n2){O(n^2)} is a set of functions, not a number and not one function. It contains every cost rule whose growth can be kept under some fixed multiple of n2{n^2} once the input is large enough. The honest statement is TO(n2){T \in O(n^2)}: T{T} belongs to that set. The equals sign is standard shorthand, but it hides the membership claim and encourages the wrong picture.

What it takes to belong

We need a test for that claim. Write f{f} for the comparison rule, the shape we are using as a ceiling, and write g{g} for the cost rule under examination. The test asks for two ordinary numbers. First, find a positive fixed multiplier that lifts the comparison rule high enough. Call that multiplier c{c}. Then find a starting input after which the lifted comparison rule never falls below the cost. Call that starting point n0{n_0}, pronounced “n zero”. A starting point of this kind is a threshold.

Those numbers are not decoration. Producing them is the evidence for the claim, so they are called witnesses. One witness says how high the ceiling is. The other says where we are allowed to begin asking it to cover the cost. Before that point the cost may do anything. After that point, every input must obey the comparison. A cost is never below zero, which accounts for the first comparison in the definition:

O(f)={g:c>0 and n0 such that 0g(n)cf(n) for all nn0}.O(f) = \left\{ g : \exists c > 0 \text{ and } n_0 \text{ such that } 0 \leq g(n) \leq c f(n) \text{ for all } n \geq n_0 \right\}.

The symbol {\exists} reads “there exists”. It is a demand to hand over an actual c{c} and an actual n0{n_0}, not a promise that they feel plausible. The phrase “for all” is just as strict: one lucky large input is not enough. The ceiling has to stay above the cost from the threshold onward.

Find the witnesses

Return to the running cost. Here the cost rule and comparison rule are

g(n)=3n2+500n+20000,f(n)=n2.g(n) = 3n^2 + 500n + 20000, \qquad f(n) = n^2.

Try a ceiling multiplier of c=4{c = 4}. We need to know when the full cost is at most four copies of the comparison shape. Moving everything to one side turns that question into this:

3n2+500n+200004n20n2500n20000.3n^2 + 500n + 20000 \leq 4n^2 \quad\Longleftrightarrow\quad 0 \leq n^2 - 500n - 20000.

The right side first becomes at least zero, or nonnegative, at n=538{n = 538}. The two neighbouring inputs make the cutoff visible:

5372500(537)20000=131,5382500(538)20000=444.537^2 - 500(537) - 20000 = -131, \qquad 538^2 - 500(538) - 20000 = 444.

So c=4{c = 4} and n0=538{n_0 = 538} are witnesses. From input 538{538} onward, the ceiling 4n2{4n^2} covers the full cost. The result is not that the cost has become exactly four copies of n2{n^2}. It is that g{g} belongs to the set O(n2){O(n^2)}.

The number four was not arbitrary, though it was not unique. Look at the piece with the highest power of n{n}: its coefficient is 3{3}. That coefficient is called the leading coefficient because it belongs to the leading, highest-power piece. The ceiling multiplier must be strictly greater than 3{3}. If we try the boundary value, the extra linear and fixed pieces have nowhere to go:

3n2+500n+200003n2500n+200000.3n^2 + 500n + 20000 \leq 3n^2 \quad\Longleftrightarrow\quad 500n + 20000 \leq 0.

That can never hold for a positive input. This is the same 3{3} that the ratio in the earlier lesson approached from above. A multiplier above the leading coefficient clears the extra pieces eventually, and a multiplier at or below it does not.

Step 1 of 4

A ceiling that can never catch up

Set the ceiling multiplier to two. No starting input can rescue it because the ceiling stays below the cost in the long run.

Witness picker

Move the ceiling, then choose where it starts

The accent curve is the ceiling selected by the multiplier. The shaded region begins at the chosen starting input, where the membership claim is allowed to begin.

first miss 1input n through 700bound fails
  • Cost
  • Moving ceiling

The visible horizontal range expands when a later threshold matters. The shaded region is the part of the input line the current claim is asking to cover.

2.0
1
Current c
2.0
Current n zero
1
Bound after start
No
Smallest working n zero
No threshold works

No, the first failed input is 1 in the range checked through 10,000.

The controls make the price of the notation visible. A larger multiplier can begin sooner. A multiplier too low has no threshold at all. A valid claim needs both witnesses, not merely a curve that looks convincing over a short range.

Now “eventually” has an exact meaning: for every input at least n0{n_0}. A statement such as gO(n2){g \in O(n^2)} is therefore a ceiling with a receipt attached, the multiplier and threshold that certify it. This lesson has built only that ceiling. It has not said how slowly the cost can grow, which is the question behind the Upper, lower, and tight door.

Doorswhat to read next, and why

Symbolswhat each one means, and whether we defined it, measured it, or just started there

O(f)Status: defined
read "big O of f", the collection of all functions that, once the input is large enough, grow no faster than some fixed multiple of f
cStatus: defined
the fixed multiple, the first witness. For our polynomial it has to be larger than the leading coefficient 3
n_0Status: defined
the threshold, the second witness. The bound only has to hold once the input is at least this large
g(n)Status: defined
the cost being classified, here the running example 3n^2 + 500n + 20000
What these classifications mean
defined
circular by construction, true because we chose it
empirical
a measured claim about the world that could have come out otherwise
bottoms out
a primitive of the model, with nothing under it here
door
used here, explained elsewhere