Working with the notation
The equals sign is not equals, a Theta inside a formula means something specific, and two functions need not be comparable at all.
The definitions from the previous lesson are rarely used directly. In practice you combine known results with a handful of rules, and the rules are worth deriving once so that they are not magic. Start with what happens when two costs are added together, which is what happens whenever one phase of an algorithm is followed by another.
Suppose a procedure scans a list once, costing about , and then does something costing about . The total is . Compare that with the larger of the two on its own.
Change small to n * n so both terms match, and run it again. What does the ratio settle on now?
The ratio starts at 2 and falls towards 1. The smaller term never disappears, but its share of the total does. That is the sum rule: a sum of two functions grows exactly like whichever of them is larger.
What the equals sign means here
Writing is an abuse of notation that everybody uses. is really a set of functions, namely all the functions bounded above by some constant multiple of eventually, and the honest statement is . The equals sign is read left to right and means membership.
So is a correct sentence and is not, in the same way that saying a robin is a bird is correct and saying a bird is a robin is not. Two claims of the form and do not let you conclude , however tempting the symbols make it look. If and then both are and they are not remotely equal.
Notation standing inside a formula
You will see expressions like . This means: there is some function with such that the quantity equals . The notation is standing in for a term whose exact form does not matter, only its growth.
When such an expression appears on the right of an equals sign, the meaning is that for every choice of the anonymous function on the left there is some choice on the right making the equation true. That is what makes a chain like legitimate to read in one direction only, from left to right.
The rules worth memorising
| Rule | Statement |
|---|---|
| Constants vanish | For any constant a > 0, a f(n) = Theta(f(n)) |
| Sum rule | O(f) + O(g) = O(max of f and g), and likewise for Theta |
| Product rule | If f1 = O(g1) and f2 = O(g2) then f1 f2 = O(g1 g2) |
| Transitivity | If f = O(g) and g = O(h) then f = O(h). The same holds for Omega, Theta, o and omega |
| Reflexivity | f = O(f), f = Omega(f), f = Theta(f). But f is never o(f) or omega(f) |
| Symmetry of Theta | f = Theta(g) exactly when g = Theta(f) |
| Transpose symmetry | f = O(g) exactly when g = Omega(f), and f = o(g) exactly when g = omega(f) |
| Theta is both | f = Theta(g) exactly when f = O(g) and f = Omega(g) both hold |
Here is the sum rule proved, since it is the one used most. Suppose with constants , and with constants . For every both inequalities apply at once, so . Taking and shows . Two constants went in and one came out, which is why adding a constant number of phases never changes the growth.
And here is why constants vanish. If then choosing and with gives , so . The constant is absorbed into the that the definition allowed you to pick, which is exactly the point of allowing it.
Transitivity, and why the constants multiply
Transitivity is the rule that lets you take two bounds you already know and join them into a third. It says that if and then . Like every rule here it is proved by producing the pair of constants.
Unfold what the two assumptions give you. From there is a and an with for every . From there is a and an with for every . Take , so that beyond both statements are available at the same time.
Now chain them. Multiplying through by , which is positive and so leaves the direction alone, gives . Put that after the first inequality and you get for every . The pair and satisfies the definition, so .
With real numbers in it: let , and . For , , so and . For , , so and . The rule hands back and , and at that reads , which holds and keeps holding.
The product rule, and the nested loop it licenses
The product rule is the one every nested-loop analysis leans on. It says that if and , then the product . The proof is built the same way, and it uses the fact that the definition of requires the functions to be non-negative.
Suppose for every and for every , and again set so both hold together. Multiply the first inequality through by , which is at least 0, to get . Multiplying by a non-negative quantity cannot flip the direction.
Then multiply the second inequality through by , which is also at least 0, to get . Joining the two lines gives for every , so and that prove the rule.
Here is the case it exists for. Procedure B in the first lesson had a loop over every reading, and inside it a second loop over every reading. Call the number of times the outer loop runs, which is , and the cost of one full pass of the outer body, which is one complete inner loop and is at most some constant times . Then and , and the product rule gives a total of . On the six readings that was 36 comparisons, which is .
Comparing two functions in practice
A question like is $n \log_2 n$ bounded above by $n^{1.5}$ is answered by finding the crossing point and then arguing it never uncrosses. The comparison reduces to against , since both sides carry a factor of .
Add n = 10 and n = 12 to the list and run it again. Find every n where the answer is no.
At both sides equal 4. Before that the logarithm is briefly ahead, and after it the square root pulls away and never gives the lead back. So for all , and multiplying both sides by gives for all . With and the definition is satisfied and .
Not everything is comparable
For ordinary numbers, exactly one of , , holds. Growth rates have no such guarantee. Consider and . The exponent of swings between 0 and 2 forever, so is sometimes far below and sometimes far above it, and no constant multiple ever settles the matter in either direction.
Neither nor is true. This almost never happens to a real running time, but it is why the notation is defined with quantifiers rather than by saying one function is bigger than another.
An algorithm reads its input once (cost about n), then sorts a fixed 100 items (cost some constant), then scans again (cost about n). What is the total, as a Theta?
Theta(n). Adding three phases means adding three functions: n, a constant, and n. By the sum rule the total grows like the largest of them, which is n. The constant middle phase does not depend on n at all, so however slow that sort is in seconds, it is absorbed into the constant factor. Two scans instead of one is also absorbed, since 2n = Theta(n).