MAT2033 · Week 6 · growth of functions and complexity of algorithms

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.

Big-O notationBig-OmegaBig-Thetawitnessesgrowth of functionstime complexityworst-case complexitylinear searchbinary searchbubble sort

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:

  1. Big-O is an upper bound. It does not measure seconds; it tells you how the number of steps grows as the input grows.
  2. Write both witnesses: CC and kk. If one is missing, marks are missing.
  3. Constant factors and small terms do not matter. 5n2+100n5n^2 + 100n and n2n^2 are in the same class.
  4. 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.
  5. When counting loops, first think about a single round of the outer loop, then add up the rounds.

Definition of Big-O

Let ff and gg be functions. f(x) is O(g(x)) means:

∃ C,k    such that    ∣f(x)∣≤C ∣g(x)∣    whenever    x>k.\exists\, C, k \;\; \text{such that} \;\; |f(x)| \le C\,|g(x)| \;\; \text{whenever} \;\; x > k.

The constants CC and kk are called witnesses. Intuition: after some point (x>kx > k), the graph of ff never goes above CC times gg. What it does at the start does not matter.

The witness pair is not unique. If one pair works, a larger CC or a larger kk also works. On the exam you are not asked for the one correct pair, you are asked for a pair that works.

NotationRead asMeaningDefinition
f=O(g)f = O(g)Big-Off grows at most as fast as gg∣f∣≤C∣g∣\lvert f\rvert \le C\lvert g\rvert, x>kx > k
f=Ω(g)f = \Omega(g)Big-Omegaff grows at least as fast as gg∣f∣≥C∣g∣\lvert f\rvert \ge C\lvert g\rvert, x>kx > k
f=Θ(g)f = \Theta(g)Big-Thetaff and gg grow at the same rateC1∣g∣≤∣f∣≤C2∣g∣C_1\lvert g\rvert \le \lvert f\rvert \le C_2\lvert g\rvert

Θ\Theta = both OO and Ω\Omega. That is why in a Θ\Theta question you write two separate inequalities.

Template for finding witnesses

For polynomials there is a three-line recipe. Example: f(x)=4x2+7x+3f(x) = 4x^2 + 7x + 3 is O(x2)O(x^2).

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 x>1x > 1 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: 4+7+3=144 + 7 + 3 = 14.

PITFALL · The sentence “for x > 1”. If you don’t write for which xx the inequality holds, you haven’t stated kk. In the interval 0<x<10 < x < 1 we have x>x2x > x^2, so the enlarging trick does not work there; that is exactly why k=1k = 1.

Theta: the lower bound is free

f(n)=3n2+5n+2f(n) = 3n^2 + 5n + 2 is Θ(n2)\Theta(n^2):

  • Upper bound: for n>1n > 1, 3n2+5n+2≤3n2+5n2+2n2=10n23n^2 + 5n + 2 \le 3n^2 + 5n^2 + 2n^2 = 10n^2, so C2=10C_2 = 10.
  • Lower bound: dropping the positive terms makes the expression smaller: 3n2+5n+2≥3n23n^2 + 5n + 2 \ge 3n^2, so C1=3C_1 = 3.

Result: for n>1n > 1, 3n2≤f(n)≤10n23n^2 \le f(n) \le 10n^2. Write all three constants (C1=3C_1 = 3, C2=10C_2 = 10, k=1k = 1) together.

If there is a negative coefficient (like 5n2−3n5n^2 - 3n), the lower bound is not free. Then you use an inequality like 3n≤n23n \le n^2 and enlarge kk. On the midterm a polynomial with positive coefficients usually appears.

The “not O” proof (contradiction)

To show that a function is not O(g)O(g), an example is not enough; you must show that the definition cannot be satisfied with any C,kC, k. The template is contradiction. Example: n3n^3 is not O(7n2)O(7n^2).

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 nn is always smaller than the constant 7C7C”. Whatever CC you choose, n=7C+1n = 7C + 1 and beyond break it. For example, for C=50C = 50 and n=351n = 351: 3513>350⋅3512351^3 > 350 \cdot 351^2.

TIP · The same template works for exponentials. 4n4^n is not O(2n)O(2^n): assume 4n≤C⋅2n4^n \le C\cdot 2^n, divide both sides by 2n2^n, get 2n≤C2^n \le C. 2n2^n is unbounded. Contradiction.

Growth ordering

From small to large, the chain to memorize:

1  <  log⁡n  <  n  <  n  <  nlog⁡n  <  n2  <  n3  <  2n  <  3n  <  n!1 \;<\; \log n \;<\; \sqrt{n} \;<\; n \;<\; n\log n \;<\; n^2 \;<\; n^3 \;<\; 2^n \;<\; 3^n \;<\; n!

Rules:

  • In a sum the fastest-growing term wins: n2+1000n+log⁡nn^2 + 1000n + \log n is O(n2)O(n^2).
  • In a product the bounds are multiplied: (n2+3)log⁡n(n^2 + 3)\log n is O(n2log⁡n)O(n^2 \log n).
  • The base of a logarithm does not matter: log⁡2n=(log⁡210)log⁡10n\log_2 n = (\log_2 10)\log_{10} n, only a constant factor.
  • Adding a constant to the exponent does not matter, multiplying the exponent does: 2n+3=8⋅2n2^{n+3} = 8\cdot 2^n is O(2n)O(2^n); but 22n=4n2^{2n} = 4^n is not.
  • Every exponential with base greater than 1 eventually overtakes every polynomial: even (1.1)n(1.1)^n overtakes n100n^{100}.

Complexity of algorithms

Time complexity: the number of basic operations (comparisons, additions) the algorithm performs, expressed in terms of the input size nn. Usually the worst case is asked: at most how many operations, however bad the input is.

AlgorithmWorst caseWhy
Linear searchnn comparisons, O(n)O(n)If the target is absent, you look at every element
Binary searchabout log⁡2n\log_2 n rounds, O(log⁡n)O(\log n)The interval halves in each round
Bubble sort(n−1)n2\frac{(n-1)n}{2} comparisons, Θ(n2)\Theta(n^2)(n−1)+(n−2)+⋯+1(n-1) + (n-2) + \dots + 1
Two full nested loopsn2n^2 operationsnn 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 1024→512→⋯→11024 \to 512 \to \dots \to 1, that is 10 rounds. Bubble sort on a reverse-sorted list of 10 elements makes 9+8+⋯+1=459 + 8 + \dots + 1 = 45 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 ii fixed, jj goes from ii to nn, that is n−i+1n - i + 1 rounds.

i = 1  ->  n rounds
i = 2  ->  n - 1 rounds
...
i = n  ->  1 round
Total: n + (n-1) + ... + 1 = n(n+1)/2

For n=8n = 8, 8⋅92=36\frac{8 \cdot 9}{2} = 36. Big-O estimate: n(n+1)2=12n2+12n≤n2\frac{n(n+1)}{2} = \frac{1}{2}n^2 + \frac{1}{2}n \le n^2 (n≥1n \ge 1), that is O(n2)O(n^2). Even if it is half of n2n^2, the class is n2n^2.

If the loop variable grows by multiplication (i:=2ii := 2i), the number of rounds is logarithmic. If it shrinks by division (i:=⌊i/2⌋i := \lfloor i/2 \rfloor), it is again logarithmic.

Common mistakes

Wrong ideaThe truth
Writing only CCBoth CC and kk: “witnesses C = 14, k = 1”
Not writing “for x > 1”The range where the inequality holds determines kk, write it
“I tried a few nn, and f≤Cgf \le Cg fails”“Not O” needs a contradiction proof that defeats all C,kC, k
Only an upper bound for Θ\ThetaΘ\Theta = OO + Ω\Omega: two inequalities, C1C_1 and C2C_2
4n4^n is O(2n)O(2^n)4n/2n=2n4^n / 2^n = 2^n is unbounded, so no
log⁡2n\log_2 n and log⁡10n\log_{10} n are different classesThey differ by a constant factor, same class
n22\frac{n^2}{2} is O(n)O(n) because it is “half”A constant factor does not change the class, it is O(n2)O(n^2)
If the inner loop is 1..i1..i it runs nn timesIt runs 1+2+⋯+n=n(n+1)21 + 2 + \dots + n = \frac{n(n+1)}{2} times
Binary search is O(n/2)O(n/2)It halves in each round, O(log⁡n)O(\log n)

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Show that f(x)=2x2+9x+5f(x) = 2x^2 + 9x + 5 is O(x2)O(x^2). Give witnesses CC and kk.

Show answer
C = 16, k = 1

For x>1x > 1, x<x2x < x^2 and 1<x21 < x^2. Enlarge every term to x2x^2: 2x2+9x+5≤2x2+9x2+5x2=16x22x^2 + 9x + 5 \le 2x^2 + 9x^2 + 5x^2 = 16x^2. So C=16C = 16, k=1k = 1. There are other valid pairs (for example C=3C = 3, large kk), but this template is the fastest.

Question 2easy

Arrange the functions in increasing order of growth: nlog⁡nn\log n, n2n^2, 2n2^n, log⁡n\log n, n!n!, n\sqrt{n}.

Show answer
log n, sqrt n, n log n, n^2, 2^n, n!

A logarithm is slower than any power, and n=n1/2\sqrt{n} = n^{1/2} is slower than nn. nlog⁡nn\log n is between nn and n2n^2. Every polynomial is slower than every exponential (2n2^n), and n!n! beats even 2n2^n. Order: log⁡n<n<nlog⁡n<n2<2n<n!\log n < \sqrt{n} < n\log n < n^2 < 2^n < n!.

Question 3medium

Give a big-O estimate for f(n)=(n3+2n)(log⁡n+4)+5n2f(n) = (n^3 + 2n)(\log n + 4) + 5n^2.

Show answer
O(n^3 log n)

In a product the big terms are multiplied: O(n3)O(n^3) for n3+2nn^3 + 2n, O(log⁡n)O(\log n) for log⁡n+4\log n + 4, the product is O(n3log⁡n)O(n^3 \log n). In a sum the fastest-growing term wins: 5n25n^2 is small next to n3log⁡nn^3\log n. The result is O(n3log⁡n)O(n^3 \log n).

Question 4medium

(a) Is 3n+23^{n+2} O(3n)O(3^n)? (b) Is 9n9^n O(3n)O(3^n)?

Show answer
(a) yes, C = 9; (b) no

(a) 3n+2=9⋅3n3^{n+2} = 9 \cdot 3^n, so it is O(3n)O(3^n) with C=9C = 9, k=0k = 0. Adding a constant to the exponent means a constant factor.
(b) 9n=32n=3n⋅3n9^n = 3^{2n} = 3^n \cdot 3^n. If 9n≤C⋅3n9^n \le C\cdot 3^n, dividing both sides by 3n3^n would give 3n≤C3^n \le C; since 3n3^n grows without bound, this breaks somewhere. It is not O.

Question 5medium

How many times is x := x + 1 executed? Give the count for n=6n = 6, the general formula, and a Big-Theta estimate.

for i := 1 to n
    for j := 1 to 2n
        x := x + 1
Show answer
72; 2n^2; Theta(n^2)

The outer loop runs nn times, and in each round the inner loop runs 2n2n times. Total n⋅2n=2n2n \cdot 2n = 2n^2. For n=6n = 6, 2⋅36=722\cdot 36 = 72. The constant factor 2 does not matter: Θ(n2)\Theta(n^2).

Question 6medium

Show that f(n)=4n2+6n+1f(n) = 4n^2 + 6n + 1 is Θ(n2)\Theta(n^2). Give C1C_1, C2C_2 and kk.

Show answer
C1 = 4, C2 = 11, k = 1

Upper bound: for n>1n > 1, 4n2+6n+1≤4n2+6n2+n2=11n24n^2 + 6n + 1 \le 4n^2 + 6n^2 + n^2 = 11n^2. Lower bound: dropping the positive terms makes the expression smaller, 4n2+6n+1≥4n24n^2 + 6n + 1 \ge 4n^2. So for n>1n > 1, 4n2≤f(n)≤11n24n^2 \le f(n) \le 11n^2, that is Θ(n2)\Theta(n^2).

Question 7hard

Prove that n2n^2 is not O(10n)O(10n).

Show answer
Assume n^2 <= C(10n) for n > k; then n <= 10C, false for n > max(k, 10C).

The contradiction template. Assume: there exist C,kC, k such that n2≤C⋅10nn^2 \le C \cdot 10n for every n>kn > k. Divide both sides by n>0n > 0: n≤10Cn \le 10C. But if you choose n>max⁡(k,10C)n > \max(k, 10C) this breaks (for example for C=3C = 3, n=31n = 31: 961>930961 > 930). Contradiction, so n2n^2 is not O(10n)O(10n).

Question 8easy

Bubble sort is run on a list of 12 numbers in decreasing order. How many comparisons does it make?

Show answer
66

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 (n−1)n2=11⋅122=66\frac{(n-1)n}{2} = \frac{11 \cdot 12}{2} = 66.

Question 9medium

The binary search below runs on a sorted list a1,…,a64a_1, \dots, a_{64}. How many times does the while loop execute, whatever xx is?

i := 1; j := 64
while i < j
    m := floor((i + j) / 2)
    if x > a_m then i := m + 1
    else j := m
Show answer
6

In each round the interval is exactly halved: 64 → 32 → 16 → 8 → 4 → 2 → 1. Six halvings, so 6 rounds. In general, for n=2kn = 2^k there are k=log⁡2nk = \log_2 n rounds, O(log⁡n)O(\log n).

Question 10hard

How many times does the loop body execute for n=100n = 100? Give a big-O estimate.

i := 1
while i <= n
    i := 2 * i
Show answer
7; O(log n)

The values of ii: 1, 2, 4, 8, 16, 32, 64 (all ≤100\le 100). The next one, 128 > 100, ends the loop. 7 rounds. The general formula is ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1, that is O(log⁡n)O(\log n).

Question 11medium

Is log⁡2n\log_2 n Θ(log⁡10n)\Theta(\log_{10} n)? Justify.

Show answer
Yes

Change of base: log⁡2n=log⁡10nlog⁡102=(log⁡210)⋅log⁡10n≈3.32log⁡10n\log_2 n = \frac{\log_{10} n}{\log_{10} 2} = (\log_2 10)\cdot \log_{10} n \approx 3.32 \log_{10} n. There is a constant factor between them, so in the Big-O world the base of a logarithm does not matter: Θ\Theta.

Question 12hard

How many times is s := s + 1 executed when n=5n = 5? Give a big-O estimate.

for i := 1 to n
    for j := 1 to i
        for k := 1 to j
            s := s + 1
Show answer
35; O(n^3)

For fixed ii and jj the innermost loop runs jj times. For fixed ii the total is 1+2+⋯+i=i(i+1)21 + 2 + \dots + i = \frac{i(i+1)}{2}. Add these for i=1..5i = 1..5: 1+3+6+10+15=351 + 3 + 6 + 10 + 15 = 35. The general formula is n(n+1)(n+2)6\frac{n(n+1)(n+2)}{6}, that is O(n3)O(n^3).

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).

Review card

f is O(g): |f(x)| <= C|g(x)| for all x > k (C, k = witnesses, WRITE BOTH) Omega: >= C|g| Theta: C1|g| <= |f| <= C2|g| (O and Omega together) Polynomial template: for x > 1 enlarge every term to the highest power, add the coefficients = C, k = 1 Lower bound: drop the positive terms -> leading coefficient = C1 "Not O": assume n^a <= C n^b, divide, get an absurdity like n <= C, contradiction with n > max(k, C) Order: 1 < log n < sqrt n < n < n log n < n^2 < n^3 < 2^n < 3^n < n! Sum -> biggest term; product -> multiply: (n^2+1)log n = O(n^2 log n) 2^(n+1) = 2*2^n -> O(2^n) but 4^n = 2^(2n) is NOT O(2^n) log base does not matter: log2 n = Theta(log10 n) Nested 1..n, 1..n -> n^2 inner loop 1..i -> n(n+1)/2 -> Theta(n^2) i := 2i loop -> floor(log2 n)+1 rounds -> O(log n) Linear search worst case n comparisons O(n); binary search O(log n) Bubble sort (n-1)n/2 comparisons -> Theta(n^2)