Growth of functions and algorithm complexity
Definitions of Big-O, Big-Omega and Big-Theta, finding witnesses, the 'is not O' proof, growth ordering and loop counting. The last topic of the midterm.
This week is the last topic of the midterm. The question types are fixed and mechanical: “Show that f is O(g), give witnesses”, “show that f is not O(g)”, “arrange in order of growth” and “how many times is this line executed?”. All of them have a template. Golden rules:
- Big-O is an upper bound. It does not measure seconds; it tells you how the number of steps grows as the input grows.
- Write both witnesses: and . If one is missing, marks are missing.
- Constant factors and small terms do not matter. and are in the same class.
- A “not O” proof is always done by contradiction. Assume the definition, divide, and reach the conclusion that something unbounded is smaller than a constant.
- When counting loops, first think about a single round of the outer loop, then add up the rounds.
Definition of Big-O
Let and be functions. f(x) is O(g(x)) means:
The constants and are called witnesses. Intuition: after some point (), the graph of never goes above times . What it does at the start does not matter.
The witness pair is not unique. If one pair works, a larger or a larger also works. On the exam you are not asked for the one correct pair, you are asked for a pair that works.
| Notation | Read as | Meaning | Definition |
|---|---|---|---|
| Big-O | grows at most as fast as | , | |
| Big-Omega | grows at least as fast as | , | |
| Big-Theta | and grow at the same rate |
= both and . That is why in a question you write two separate inequalities.
Template for finding witnesses
For polynomials there is a three-line recipe. Example: is .
When x > 1 we have x < x^2 and 1 < x^2.
Hence 4x^2 + 7x + 3 <= 4x^2 + 7x^2 + 3x^2 = 14x^2 for x > 1.
So f(x) is O(x^2) with witnesses C = 14 and k = 1.
The trick: when it is allowed to enlarge the small powers to the highest power, because you are making the expression bigger and the direction of the inequality is preserved. Then add the coefficients: .
PITFALL · The sentence “for x > 1”. If you don’t write for which the inequality holds, you haven’t stated . In the interval we have , so the enlarging trick does not work there; that is exactly why .
Theta: the lower bound is free
is :
- Upper bound: for , , so .
- Lower bound: dropping the positive terms makes the expression smaller: , so .
Result: for , . Write all three constants (, , ) together.
If there is a negative coefficient (like ), the lower bound is not free. Then you use an inequality like and enlarge . On the midterm a polynomial with positive coefficients usually appears.
The “not O” proof (contradiction)
To show that a function is not , an example is not enough; you must show that the definition cannot be satisfied with any . The template is contradiction. Example: is not .
Proof. Suppose, for contradiction, that n^3 is O(7n^2).
Then there are constants C and k with n^3 <= C * 7n^2 for all n > k.
Dividing both sides by n^2 > 0 gives n <= 7C for all n > k.
But this fails for any integer n > max(k, 7C).
This is a contradiction, so n^3 is not O(7n^2). ∎
In plain words: after the division an absurd result appears, like “the unboundedly growing is always smaller than the constant ”. Whatever you choose, and beyond break it. For example, for and : .
TIP · The same template works for exponentials. is not : assume , divide both sides by , get . is unbounded. Contradiction.
Growth ordering
From small to large, the chain to memorize:
Rules:
- In a sum the fastest-growing term wins: is .
- In a product the bounds are multiplied: is .
- The base of a logarithm does not matter: , only a constant factor.
- Adding a constant to the exponent does not matter, multiplying the exponent does: is ; but is not.
- Every exponential with base greater than 1 eventually overtakes every polynomial: even overtakes .
Complexity of algorithms
Time complexity: the number of basic operations (comparisons, additions) the algorithm performs, expressed in terms of the input size . Usually the worst case is asked: at most how many operations, however bad the input is.
| Algorithm | Worst case | Why |
|---|---|---|
| Linear search | comparisons, | If the target is absent, you look at every element |
| Binary search | about rounds, | The interval halves in each round |
| Bubble sort | comparisons, | |
| Two full nested loops | operations | inner rounds for each outer round |
Examples: linear search for a number that is not in a list of 50 elements takes 50 comparisons. Binary search on a sorted list of 1024 elements (until the interval shrinks to 1 element) goes , that is 10 rounds. Bubble sort on a reverse-sorted list of 10 elements makes comparisons.
Counting loops: step by step
for i := 1 to n
for j := i to n
x := x + 1
First think about a single outer round: with fixed, goes from to , that is rounds.
i = 1 -> n rounds
i = 2 -> n - 1 rounds
...
i = n -> 1 round
Total: n + (n-1) + ... + 1 = n(n+1)/2
For , . Big-O estimate: (), that is . Even if it is half of , the class is .
If the loop variable grows by multiplication (), the number of rounds is logarithmic. If it shrinks by division (), it is again logarithmic.
Common mistakes
| Wrong idea | The truth |
|---|---|
| Writing only | Both and : “witnesses C = 14, k = 1” |
| Not writing “for x > 1” | The range where the inequality holds determines , write it |
| “I tried a few , and fails” | “Not O” needs a contradiction proof that defeats all |
| Only an upper bound for | = + : two inequalities, and |
| is | is unbounded, so no |
| and are different classes | They differ by a constant factor, same class |
| is because it is “half” | A constant factor does not change the class, it is |
| If the inner loop is it runs times | It runs times |
| Binary search is | It halves in each round, |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Show that is . Give witnesses and .
Show answer
For , and . Enlarge every term to : . So , . There are other valid pairs (for example , large ), but this template is the fastest.
Arrange the functions in increasing order of growth: , , , , , .
Show answer
A logarithm is slower than any power, and is slower than . is between and . Every polynomial is slower than every exponential (), and beats even . Order: .
Give a big-O estimate for .
Show answer
In a product the big terms are multiplied: for , for , the product is . In a sum the fastest-growing term wins: is small next to . The result is .
(a) Is ? (b) Is ?
Show answer
(a) , so it is with , . Adding a constant to the exponent means a constant factor.
(b) . If , dividing both sides by would give ; since grows without bound, this breaks somewhere. It is not O.
How many times is x := x + 1 executed? Give the count for , the general formula, and a Big-Theta estimate.
for i := 1 to n
for j := 1 to 2n
x := x + 1Show answer
The outer loop runs times, and in each round the inner loop runs times. Total . For , . The constant factor 2 does not matter: .
Show that is . Give , and .
Show answer
Upper bound: for , . Lower bound: dropping the positive terms makes the expression smaller, . So for , , that is .
Prove that is not .
Show answer
The contradiction template. Assume: there exist such that for every . Divide both sides by : . But if you choose this breaks (for example for , : ). Contradiction, so is not .
Bubble sort is run on a list of 12 numbers in decreasing order. How many comparisons does it make?
Show answer
The number of comparisons in bubble sort is independent of the input (the version without early exit): 11 in the 1st pass, 10 in the 2nd, ..., 1 in the last. Total .
The binary search below runs on a sorted list . How many times does the while loop execute, whatever is?
i := 1; j := 64
while i < j
m := floor((i + j) / 2)
if x > a_m then i := m + 1
else j := mShow answer
In each round the interval is exactly halved: 64 → 32 → 16 → 8 → 4 → 2 → 1. Six halvings, so 6 rounds. In general, for there are rounds, .
How many times does the loop body execute for ? Give a big-O estimate.
i := 1
while i <= n
i := 2 * iShow answer
The values of : 1, 2, 4, 8, 16, 32, 64 (all ). The next one, 128 > 100, ends the loop. 7 rounds. The general formula is , that is .
Is ? Justify.
Show answer
Change of base: . There is a constant factor between them, so in the Big-O world the base of a logarithm does not matter: .
How many times is s := s + 1 executed when ? Give a big-O estimate.
for i := 1 to n
for j := 1 to i
for k := 1 to j
s := s + 1Show answer
For fixed and the innermost loop runs times. For fixed the total is . Add these for : . The general formula is , that is .
Verification: Witness inequalities were checked by dense numeric scanning for x > k, loop counts and search/sort comparisons by running the code and counting, and the growth ordering by logarithmic comparison, in node (verify/MAT2033/06.mjs).