MCS-211 · Block 1 Unit 2

Some Pre-requisites and Asymptotic Bounds

Section 6 of 8

Block 1 · Unit 2Some Pre-requisites and Asymptotic Bounds12 min · Section 6 of 8

2.6Big-O Notation: Upper Bounds

Covers IGNOU MCS-211 §2.7, 2.7.1

Big-O is the most widely used asymptotic notation. This page gives its precise definition, a reliable method for proving f(n)=O(g(n))f(n) = O(g(n)) by finding constants, the method for proving that a function is not O(g(n))O(g(n)), and fully worked answers to the Big-O parts of Check Your Progress 2.

Definition

O(g(n))O(g(n)) is really a set of functions — all functions that grow at most as fast as gg, up to a constant factor — and "f(n)=O(g(n))f(n) = O(g(n))" means ”f(n)f(n) belongs to that set”. The "==" here is one-way: n=O(n2)n = O(n^2) is true but "O(n2)=nO(n^2) = n" is not a sensible statement.

Graph of a Big-O upper bound

Proving f(n)=O(g(n))f(n) = O(g(n))

A method that always works for polynomials:

  1. Write the inequality f(n)≤c⋅g(n)f(n) \le c \cdot g(n) you need.
  2. Drop negative terms of f(n)f(n) (this only makes the left side bigger).
  3. Raise each remaining lower power to the power of gg, using nj≤nmn^j \le n^m for n≥1n \ge 1 and j≤mj \le m.
  4. Add the coefficients to get cc; take n0=1n_0 = 1.

Proving f(n)≠O(g(n))f(n) \ne O(g(n))

Assume the opposite — that some cc and n0n_0 exist — and show the inequality fails for some large nn. That is a contradiction.

Check Your Progress 2: the Big-O parts

(a) 4n2+7n+12=O(n2)4n^2 + 7n + 12 = O(n^2) — true. For n≥1n \ge 1: 4n2+7n+12≤4n2+7n2+12n2=23n24n^2 + 7n + 12 \le 4n^2 + 7n^2 + 12n^2 = 23n^2. So c=23c = 23, n0=1n_0 = 1. With the smaller constant c=5c = 5 we need n2−7n−12≥0n^2 - 7n - 12 \ge 0, i.e. n≥7+972≈8.42n \ge \frac{7 + \sqrt{97}}{2} \approx 8.42, so n0=9n_0 = 9.

Python · find_n0.py
def smallest_n0(f, g, c, limit=100000):
    """Smallest n0 such that f(n) <= c*g(n) for every n from n0 to limit."""
    n0 = None
    for n in range(limit, 0, -1):
        if f(n) <= c * g(n):
            n0 = n
        else:
            break
    return n0

sq = lambda n: n * n
print("4n^2+7n+12 <= 5n^2 from n0 =", smallest_n0(lambda n: 4*n*n + 7*n + 12, sq, 5))
print("4n^2+7n+12 <= 23n^2 from n0 =", smallest_n0(lambda n: 4*n*n + 7*n + 12, sq, 23))
print("3n^2+4n-2  <= 5n^2 from n0 =", smallest_n0(lambda n: 3*n*n + 4*n - 2, sq, 5))
print("3n^2+4n-2  <= 7n^2 from n0 =", smallest_n0(lambda n: 3*n*n + 4*n - 2, sq, 7))
Output
4n^2+7n+12 <= 5n^2 from n0 = 9
4n^2+7n+12 <= 23n^2 from n0 = 1
3n^2+4n-2  <= 5n^2 from n0 = 1
3n^2+4n-2  <= 7n^2 from n0 = 1

(b) log⁡n+log⁡(log⁡n)=O(log⁡n)\log n + \log(\log n) = O(\log n) — true. Use base 2. For n≥2n \ge 2, log⁡2n≥1\log_2 n \ge 1, and since log⁡2x≤x\log_2 x \le x for all x≥1x \ge 1, we get log⁡2(log⁡2n)≤log⁡2n\log_2(\log_2 n) \le \log_2 n. Hence log⁡2n+log⁡2log⁡2n≤2log⁡2n\log_2 n + \log_2 \log_2 n \le 2\log_2 n for all n≥2n \ge 2: c=2c = 2, n0=2n_0 = 2.

(d) 2n+1=O(2n)2^{n+1} = O(2^n) — true. 2n+1=2⋅2n2^{n+1} = 2 \cdot 2^n, so c=2c = 2, n0=1n_0 = 1 (equality holds).

(e) 22n=O(2n)2^{2n} = O(2^n) — false. 22n=4n2^{2n} = 4^n. If 4n≤c⋅2n4^n \le c \cdot 2^n for all n≥n0n \ge n_0, dividing by 2n2^n gives 2n≤c2^n \le c for all large nn, which is impossible because 2n2^n grows without bound.

(f) f(n)=O(g(n))f(n) = O(g(n)) implies g(n)=O(f(n))g(n) = O(f(n)) — false. Counterexample: n=O(n2)n = O(n^2), but n2≠O(n)n^2 \ne O(n) (same argument as n3≠O(n2)n^3 \ne O(n^2)).

(h) f(n)=O(g(n))f(n) = O(g(n)) implies 2f(n)=O(2g(n))2^{f(n)} = O(2^{g(n)}) — false. Counterexample: f(n)=2nf(n) = 2n, g(n)=ng(n) = n. Then 2n=O(n)2n = O(n), but 22n=4n≠O(2n)2^{2n} = 4^n \ne O(2^n) by (e).

Key takeaways

  • f(n)=O(g(n))f(n) = O(g(n)) iff there are c>0c > 0, n0n_0 with 0≤f(n)≤c g(n)0 \le f(n) \le c\,g(n) for all n≥n0n \ge n_0.
  • For polynomials: drop negative terms, raise lower powers, add coefficients.
  • To disprove, show that f(n)/g(n)f(n)/g(n) cannot stay below any constant.
  • Big-O is an upper bound on a function, not a synonym for “worst case”.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which constants prove 5n+20=O(n)5n + 20 = O(n)?

Show answer

Answer: B. c=25c = 25, n0=1n_0 = 1

For n ≥ 1, 5n + 20 ≤ 5n + 20n = 25n. With c = 5 the inequality 5n + 20 ≤ 5n is never true.

Question 2 · multiple choice

Which statement is false?

Show answer

Answer: C. 22n=O(2n)2^{2n} = O(2^n)

2^(2n) = 4ⁿ and 4ⁿ/2ⁿ = 2ⁿ grows without limit. 2^(n+1) is only a constant factor 2 above 2ⁿ.

Question 3 · multiple choice

With c=5c = 5, from which n0n_0 does 4n2+7n+12≤5n24n^2 + 7n + 12 \le 5n^2 hold?

Show answer

Answer: C. 9

It needs n² − 7n − 12 ≥ 0, i.e. n ≥ 8.42, so n₀ = 9. At n = 8 the left side is 324 and the right side 320.

Question 4 · multiple choice

If f(n)=O(g(n))f(n) = O(g(n)), which of the following is always true?

Show answer

Answer: D. f(n)≤c g(n)f(n) \le c\,g(n) for some constant c>0c > 0 and all large nn

That is just the definition. The first two are false (counterexamples n and n², and f = 2n, g = n).

Question 5 · multiple choice

Which step is valid when proving 6n2−5n+4=O(n2)6n^2 - 5n + 4 = O(n^2) for n≥1n \ge 1?

Show answer

Answer: A. 6n2−5n+4≤6n2+4≤6n2+4n2=10n26n^2 - 5n + 4 \le 6n^2 + 4 \le 6n^2 + 4n^2 = 10n^2

Dropping −5n and raising 4 to 4n² are both valid for n ≥ 1. Replacing −5n by −5n² is invalid: it gives 5n², and at n = 10 the left side is 554 > 500.

Question 6 · short answer

Prove that n2≠O(n)n^2 \ne O(n).

Show worked answer

Suppose there are constants c > 0 and n₀ with n² ≤ c·n for all n ≥ n₀. Dividing by n > 0 gives n ≤ c for all n ≥ n₀. Take n = max(n₀, ⌈c⌉ + 1): then n ≥ n₀ and n > c, a contradiction. Hence no such constants exist and n² ≠ O(n).

↑ ↓ move↵ openTab switch type