WHAT. We have two functions of n (the input size — how many items your program handles). Call them:
f(n) — the cost of your algorithm (number of steps it takes).
g(n) — a clean reference shape we already understand, like n, n2, or nlogn.
WHY. We never say "this program takes 4000 steps." Machines differ, inputs differ. Instead we ask a shape question: as n gets huge, does f curve up like g? Comparing to a known reference g turns a messy count into a clean category.
PICTURE. Look at the two curves below. The coral curve is f(n)=3n2+5n+7. The lavender curve is the reference g(n)=n2. They look like they bend the same way — but coral is higher. Step 2 asks: is that height difference just a constant stretch?
WHAT. Take the reference g(n)=n2 and multiply it by a constantc. That stretches it vertically. Question: is there a stretch c big enough that c⋅g(n) sits abovef(n) forever?
WHY this tool — a constant multiplier. A 2× faster laptop halves every step count. We refuse to let hardware change an algorithm's category. Multiplying g by a constant c is exactly the freedom "ignore constant factors." If somec makes cg a ceiling, then f never outgrows g's shape.
PICTURE. The mint curve below is 15g(n)=15n2. Notice it stays above the coral f once we pass a threshold. That threshold has a name: n0. Before n0 we do not care (the "baby photos" — small n).
costf(n)≤stretch factorc⋅referenceg(n)for all n≥ignore-before-heren0
WHY each replacement is legal: for n≥1 we have n≤n2 and 1≤n2, so replacing 5n by 5n2 and 7 by 7n2 only makes the right side bigger. A bigger right side still being ≥ our left side proves the ceiling.
WHAT. Now do the mirror image. Multiply g by a (possibly smaller) constant c1 and ask: does f(n) stay abovec1g(n) forever?
WHY. The ceiling forbids f from being slower than g's shape. But a ceiling alone is a loose promise — anything below it also fits. To pin f we also need a floor that forbids f from being secretly faster. Flip the inequality and you have the lower-bound question.
PICTURE. The butter curve below is 3g(n)=3n2. The coral f sits on or above it everywhere (for n≥0). So g acts as a floor with c1=3.
WHY: the dropped terms 5n and 7 are positive, so throwing them away only shrinks the left side. If the shrunken thing is still ≥3n2, so is the full thing.
WHAT. Stack Step 2 and Step 3 on one picture. The floor 3n2 is below, the ceiling 15n2 is above, and f is trapped in the band between them.
WHY. When the same shapeg works as both floor and ceiling (with two constants), f can neither escape upward nor sink downward — its growth is g's growth, exactly. That "trapped between two copies of one shape" is precisely Θ.
PICTURE. The shaded lavender band below is everything between 3n2 and 15n2. The coral f lives inside it forever past n0=1. Trapped = tight.
WHAT. Proving constants by hand is slow. Instead, form the ratiof(n)/g(n) and let n→∞. Call the result L.
WHY this tool — the limit. The band picture says f and g differ by a bounded constant factor. The ratio f/gis that factor at each n. If the ratio settles to a finite nonzero number, the factor is bounded — that is exactly the Θ sandwich, read off in one shot. This is why Limits and L'Hôpital's rule powers the shortcut.
PICTURE. Below, f(n)/g(n)=n23n2+5n+7=3+n5+n27. Watch it glide down toward the dashed line L=3: a finite, nonzero limit.
\begin{cases}
L=0 & f=O(g)\ \text{but not}\ \Omega(g)\ \ (\text{strictly smaller})\\[2pt]
0<L<\infty & f=\Theta(g)\ \ (\text{same shape!})\\[2pt]
L=\infty & f=\Omega(g)\ \text{but not}\ O(g)\ \ (\text{strictly larger})
\end{cases}$$
Our $L=3$ lands in the middle row: $f=\Theta(n^2)$. Same verdict as the hand proof, far less work. (See [[Asymptotic comparison of functions]] for the $o$ and $\omega$ siblings at $L=0$ and $L=\infty$.)
---
## Step 6 — Degenerate case: when NO single $\Theta$ exists.
**WHAT.** Try $f(n)=n^2$ against $g(n)=n$. Can $n$ be a *ceiling* for $n^2$? Suppose $n^2\le c\,n$. Divide by $n>0$:
$$n \le c$$
**WHY this breaks.** This must hold for **all** large $n$. But no fixed number $c$ is bigger than every $n$ — pick $n=c+1$ and it fails. So **no ceiling constant exists**: $n^2\ne O(n)$. Yet $n^2\ge 1\cdot n$ for $n\ge 1$, so the *floor* holds: $n^2=\Omega(n)$. Floor yes, ceiling no ⇒ **not tight**.
**PICTURE.** Below, every stretched line $c\,n$ (three of them) is eventually overtaken by $n^2$. There is no line high enough forever — the ratio $n^2/n=n$ runs off to $\infty$ (that is the $L=\infty$ row of Step 5).
![[deepdives/dd-coding-3.1.06-d2-s06.png]]
> [!mistake] "Every algorithm has a $\Theta$."
> **Why it feels right:** functions usually have a tidy shape. **Truth:** if best and worst case grow in *different orders*, no single $\Theta$ covers the whole runtime. Insertion sort is best $\Theta(n)$, worst $\Theta(n^2)$ — overall only $O(n^2)$ and $\Omega(n)$. **Fix:** state $\Theta$ *per case*, or bound the spread with $O$ and $\Omega$. See [[Best, worst, and average case analysis]].
---
## Step 7 — The other degenerate axis: a *problem's* floor.
**WHAT.** $\Omega$ can bound a whole **problem**, not just one algorithm. Comparison-based sorting needs $\Omega(n\log n)$ comparisons — a floor under **every possible** sorting algorithm.
**WHY it matters.** A [[Decision trees and sorting lower bound|decision tree]] with $n!$ leaf outcomes (all orderings) has height at least $\log_2(n!)=\Theta(n\log n)$, and height = worst-case comparisons. So *no cleverness* can beat $n\log n$. Merge sort *achieves* $O(n\log n)$ — its ceiling meets the problem's floor, so merge sort is **optimal**: $\Theta(n\log n)$. ([[Master Theorem]] confirms merge sort's $\Theta(n\log n)$ from its recurrence.)
**PICTURE.** Two brackets meeting: the problem's floor $\Omega(n\log n)$ pushing up, merge sort's ceiling $O(n\log n)$ pushing down, kissing at the optimal line.
![[deepdives/dd-coding-3.1.06-d2-s07.png]]
---
## The one-picture summary
![[deepdives/dd-coding-3.1.06-d2-s08.png]]
One reference shape $g$, stretched two ways, traps the cost $f$ in a band — that band **is** $\Theta$. Loosen only the top → $O$. Loosen only the bottom → $\Omega$. Split the curves and take a limit → the verdict falls out as $L$.
> [!recall]- Feynman: tell it like a story
> I have a mystery cost curve $f$ and a plain reference $g$. First I stretch $g$ upward by a fudge factor $c_2$ until my copy sits *over* $f$ forever — that copy is a **ceiling**, and "there is such a ceiling" is Big-$O$. Then I shrink $g$ by $c_1$ so its copy slides *under* $f$ forever — a **floor**, called $\Omega$. If the same simple $g$ gives me both a floor and a ceiling, then $f$ is caught in a narrow band and I know its growth *exactly* — that is $\Theta$. Lazy trick: instead of building floors and ceilings by hand, I just divide $f$ by $g$ and see where the ratio settles. Settles at zero? $g$ is way bigger, only a ceiling. Runs off to infinity? $g$ is way smaller, only a floor. Settles at a nice middle number? Same shape — $\Theta$. And two warnings I keep taped to my monitor: (1) these three symbols are about *growth*, not about best/worst case — that's a whole different choice. (2) When best and worst grow differently, there is simply no single $\Theta$ to give.
> [!recall]- Quick self-check
> If $\lim f/g = 7$, which bound? ::: $\Theta(g)$ — finite nonzero limit means same shape.
> Why does $n^2\ne O(n)$? ::: $n^2\le cn$ forces $n\le c$, impossible for all large $n$.
> What does the shaded band in Step 4 represent? ::: every value between $c_1 g$ and $c_2 g$ — the $\Theta$ trap.
> Problem lower bound for comparison sort? ::: $\Omega(n\log n)$ from decision-tree height $\log_2(n!)$.
## Connections
- [[Big-O notation — upper bounds]] — the ceiling half, alone.
- [[Best, worst, and average case analysis]] — the *other axis* the mistakes confuse.
- [[Master Theorem]] — hands you $\Theta$ for divide-and-conquer recurrences.
- [[Decision trees and sorting lower bound]] — the $\Omega(n\log n)$ proof of Step 7.
- [[Limits and L'Hôpital's rule]] — powers the Step 5 ratio test.
- [[Asymptotic comparison of functions]] — $o$ and $\omega$ at $L=0$ and $L=\infty$.