3.1.1Complexity Analysis

Big-O notation — formal definition, mathematical

1,762 words8 min readdifficulty · medium6 backlinks

WHY does Big-O exist?

So Big-O answers: "As input size explodes, roughly how does cost blow up?"


WHAT is the formal definition?

Read it aloud: "There is some multiplier cc and some threshold n0n_0 so that beyond n0n_0, ff never pokes above cgc\,g."

Three moving parts you MUST be able to recall:

  • cc — the constant multiplier (absorbs hidden coefficients).
  • n0n_0 — the threshold past which the bound holds (lets us ignore small-nn weirdness).
  • The inequality is one-sided (\le): Big-O is an upper bound only.
Figure — Big-O notation — formal definition, mathematical

HOW do you prove f(n)=O(g(n))f(n) = O(g(n))?

The whole game is: pick a cc, pick an n0n_0, show the inequality holds forever after.


Forecast-then-Verify

Recall Forecast before reading the answer

Q: Is 1000n=O(n)1000n = O(n)? Forecast yes/no, then justify.

Verify: YES. Take c=1000,n0=0c = 1000, n_0 = 0: 1000n1000n1000n \le 1000\cdot n trivially. Big constants vanish — that's the point of Big-O.

Recall Forecast

Q: Is 2n=O(n100)2^{n} = O(n^{100})?

Verify: NO. Any exponential eventually overtakes any polynomial: for large nn, 2n/n1002^n / n^{100} \to \infty, so no constant cc caps the ratio. Exponentials dominate polynomials.


Common Mistakes (Steel-manned)


Useful properties (each provable from the definition)


Feynman

Recall Explain to a 12-year-old

Imagine you're timing how long it takes to hand out candy to a class. If there are nn kids, and you take "about nn seconds plus a little setup," Big-O is you shrugging and saying "it grows like the number of kids." You don't care that one teacher is twice as fast (that's a constant), and you don't care about the tiny setup time. You only care: if the class doubles, the work roughly doubles. Big-O is the lazy-but-honest way to say how the work scales when things get huge.


Flashcards

Big-O formal definition
f(n)=O(g(n))f(n)=O(g(n)) iff c>0,n00\exists\, c>0, n_0\ge 0 such that 0f(n)cg(n)0\le f(n)\le c\,g(n) for all nn0n\ge n_0.
What does the constant cc absorb?
Hidden coefficients / constant factors, so machine- and implementation-dependent multipliers are ignored.
What is the role of n0n_0?
A threshold; the bound only needs to hold for sufficiently large nn, ignoring small-input behaviour.
Is Big-O an upper or lower bound?
Upper bound only (the inequality is one-sided \le). For lower use Ω\Omega, for tight use Θ\Theta.
Prove 3n+7=O(n)3n+7=O(n)
For n7n\ge 7, 3n+73n+n=4n3n+7\le 3n+n=4n, so c=4,n0=7c=4, n_0=7.
Why is n2O(n)n^2 \ne O(n)?
Would require ncn\le c for all large nn, contradicting nn\to\infty with fixed cc.
Sum rule for Big-O
O(f)+O(g)=O(max(f,g))O(f)+O(g)=O(\max(f,g)); the dominant term wins.
Is 2n=O(n100)2^n = O(n^{100})?
No; exponentials eventually dominate any polynomial.
Does f=O(g)f=O(g) imply g=O(f)g=O(f)?
No, Big-O is directional (like \le), not symmetric.
True or false: a degree-dd polynomial is O(nd)O(n^d)
True; bound every lower power by ndn^d.

Connections

Concept Map

motivated by

ignore constants

cares about

ignore small n

formalized as

uses

uses

requires

means only

pick c and n0

generalizes to

can give

allows

Big-O upper bound

Growth shape not machine

Behaviour as n to infinity

Formal definition

Constant c > 0

Threshold n0

One-sided inequality

Proof recipe

Polynomial is O of top power

Loose vs tight bounds

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Big-O ka matlab simple hai: ye batata hai ki tumhara algorithm input bada hone par kitni tezi se slow hota hai — exact time nahi, balki growth ki shape. Formal definition yaad rakho: f(n)=O(g(n))f(n)=O(g(n)) tabhi jab koi constant c>0c>0 aur ek threshold n0n_0 mil jaaye, taaki har nn0n\ge n_0 ke liye f(n)cg(n)f(n)\le c\cdot g(n) ho. Yahan cc saare constants aur coefficients ko "kha jata" hai, aur n0n_0 ka kaam hai chhote inputs ki bakwaas ko ignore karna.

Proof karne ka tarika ekdam mechanical hai: ek cc chuno, ek n0n_0 chuno, aur dikha do ki inequality hamesha ke liye sach reh jaati hai. Jaise 3n+73n+7 ke liye, jab n7n\ge 7 to 7n7\le n, isliye 3n+74n3n+7\le 4n — matlab c=4,n0=7c=4, n_0=7. Polynomial ke case mein simple trick: har chhoti power ko sabse badi power se dabaa do, jaise 2n2+3n+16n22n^2+3n+1\le 6n^2.

Sabse important baat: Big-O sirf upper bound hai, exact value nahi. Isliye n=O(n2)n=O(n^2) bhi sach hai (bas loose hai). Aur ye one-directional hai — n=O(n2)n=O(n^2) sach, par n2=O(n)n^2=O(n) galat, kyunki nn kabhi bhi constant cc se aage nikal jaayega. Exam mein log galti karte hain constants aur chhote terms rakh ke; mat karo — asymptotic world mein wo cc ke andar gayab ho jaate hain. Yaad rakho: "O for Over the top, after a while."

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections