Loading lessons...
Asymptotic Notations (Big-O, Omega, Theta)
Asymptotic Notations (Big-O, Omega, Theta)
Step counts such as 2n + 3 or 2n2 + 2n + 1 are too detailed to compare easily. Asymptotic notation keeps only the part that matters for large inputs - the rate of growth - and gives us a short, machine-independent way to describe the cost of an algorithm.
1. The Idea of Asymptotic Analysis
Asymptotic" means "as n becomes very large". For large n:
- Constant factors depend on the machine and compiler, so we ignore them (5n and n grow the same way).
- Lower-order terms become negligible: in n2 + 100n, at n = 106 the n2 term is 10 000 times bigger than 100n.
So T(n) = 3n2 + 5n + 7 is simply described as "grows like n2".
| n | n2 | 3n2 + 5n + 7 | Share of 3n2 |
|---|---|---|---|
| 10 | 100 | 357 | 84% |
| 100 | 10 000 | 30 507 | 98.3% |
| 1000 | 1 000 000 | 3 005 007 | 99.8% |
The three main notations say whether a function g(n) is an upper bound, a lower bound, or both for the running time f(n).
2. Big-O Notation - Upper Bound
Definition. f(n) = O(g(n)) if there exist positive constants c and n0 such that
Meaning: beyond some point n0, f(n) never grows faster than a constant multiple of g(n). Big-O describes the worst-case growth: "the algorithm takes at most this long".
time
^ c·g(n)
| /
| / ___ f(n)
| / __--
| / _-
| /_-
| _-/
| _- /
| - /
+---------|------------------> n
n0
for n ≥ n0, f(n) stays below c·g(n)Worked proof 1: show 4n + 6 = O(n).
- For n ≥ 1: 6 ≤ 6n, so 4n + 6 ≤ 4n + 6n = 10n.
- Choose c = 10, n0 = 1. Then 4n + 6 ≤ 10 · n for all n ≥ 1. ∎
Worked proof 2: show 5n2 + 3n + 20 = O(n2).
- For n ≥ 1: 3n ≤ 3n2 and 20 ≤ 20n2.
- So 5n2 + 3n + 20 ≤ 5n2 + 3n2 + 20n2 = 28n2.
- Choose c = 28, n0 = 1. ∎
This "replace every term by the highest power" trick is called the coefficient-sum method: for any polynomial of degree d with positive leading coefficient, f(n) = O(nd) with c = sum of the absolute values of the coefficients and n0 = 1.
Worked proof 3: show 4x2 - 5x + 3 = O(x2).
- For x ≥ 1: 4x2 - 5x + 3 ≤ 4x2 + 5x2 + 3x2 = 12x2.
- Choose c = 12, n0 = 1. ∎
More examples from the notes
| f(n) | Big-O | Witnesses |
|---|---|---|
| 7n - 3 | O(n) | 7n - 3 ≤ 7n for n ≥ 1: c = 7, n₀ = 1 |
| 50 n log n | O(n log n) | c = 50, n₀ = 1 |
| 8n2 log n + 5n2 + n | O(n2 log n) | ≤ 14 n2 log n for n ≥ 2: c = 14, n₀ = 2 |
| (n(n+1) / 2) | O(n2) | (n2 / 2) + (n / 2) ≤ n2 for n ≥ 1: c = 1, n₀ = 1 |
Big-O is only an upper bound. n2 + n = O(n3) is also true, just not tight. In answers we always give the smallest (tightest) class: say O(n2), not O(n3).
Disproof example: is n2 = O(n)? We would need n2 ≤ c · n, i.e. n ≤ c, for all n ≥ n0. No constant c works for every large n, so n2 ≠ O(n).
3. Big-Omega Notation - Lower Bound
Definition. f(n) = Ω(g(n)) if there exist positive constants c and n0 such that
Meaning: beyond n0, f(n) grows at least as fast as a constant multiple of g(n). Big-Omega describes the best-case growth: "the algorithm needs at least this much time".
Worked proof: show 3n2 + 2n = Ω(n2).
- For all n ≥ 1: 3n2 + 2n ≥ 3n2 ≥ 1 · n2.
- Choose c = 1 (or 3), n0 = 1. ∎
Example: any comparison-based sorting algorithm is Ω(n log n) in the worst case - no such algorithm can do better.
4. Theta Notation - Tight Bound
Definition. f(n) = Θ(g(n)) if there exist positive constants c1, c2 and n0 such that
Meaning: f(n) is sandwiched between two constant multiples of g(n); it grows exactly like g(n).
f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)).
Worked proof: show (n2 / 2) - 3n = Θ(n2).
- Upper: (n2 / 2) - 3n ≤ (1 / 2) n2 for all n ≥ 1, so c2 = (1 / 2).
- Lower: we need (n2 / 2) - 3n ≥ c1 n2. Dividing by n2: (1 / 2) - (3 / n) ≥ c1. For n ≥ 7, (1 / 2) - (3 / 7) = (1 / 14), so c1 = (1 / 14) works.
- Choose c1 = (1 / 14), c2 = (1 / 2), n0 = 7. ∎
5. Little-o and Little-omega (Strict Bounds)
| Notation | Meaning | Test with the ratio f(n)/g(n) as n → ∞ |
|---|---|---|
| f(n) = o(g(n)) | f grows strictly slower than g (upper bound that is not tight) | ratio → 0 |
| f(n) = ω(g(n)) | f grows strictly faster than g (lower bound that is not tight) | ratio → ∞ |
Examples: 2n = o(n2) but 2n2 ≠ o(n2) (the ratio tends to 2, not 0). n2 = ω(n) but n2 ≠ ω(n2).
6. Summary of the Five Notations
| Notation | Bound | Like comparing numbers | Typical use |
|---|---|---|---|
| O | Upper (may be tight) | f ≤ g | Worst-case running time |
| Ω | Lower (may be tight) | f ≥ g | Best case, lower bound of a problem |
| Θ | Tight (both) | f = g | Exact order of growth |
| o | Strict upper | f < g | "Strictly faster" comparisons |
| ω | Strict lower | f > g | "Strictly slower" comparisons |
7. Rules for Working with Asymptotic Notation
- Drop constant factors: 5n2 = O(n2); O(3n) = O(n).
- Keep the dominant term: a polynomial of degree d is Θ(nd).
- Sum rule (sequential code): if T1 = O(f) and T2 = O(g), then T1 + T2 = O(max(f, g)). A loop of n2 followed by a loop of n is O(n2).
- Product rule (nested code): T1 · T2 = O(f · g). A loop of n inside a loop of log n is O(n log n).
- Logarithm base does not matter: log2 n and log10 n differ only by a constant factor, so we just write O(log n).
- Transitivity: if f = O(g) and g = O(h), then f = O(h).
8. Common Growth Rates
| Class | Name | Example |
|---|---|---|
| O(1) | Constant | Access a[i], push onto a stack |
| O(log n) | Logarithmic | Binary search, search in a balanced BST |
| O(n) | Linear | Linear search, traversing a list |
| O(n log n) | Linearithmic | Merge sort, heap sort, average quick sort |
| O(n2) | Quadratic | Bubble, selection and insertion sort |
| O(n3) | Cubic | Plain matrix multiplication, Floyd-Warshall |
| O(2n) | Exponential | All subsets, naive recursive Fibonacci |
| O(n!) | Factorial | All permutations (brute-force travelling salesman) |
How fast they grow (number of steps):
| n | log2 n | n log2 n | n2 | 2n |
|---|---|---|---|---|
| 8 | 3 | 24 | 64 | 256 |
| 16 | 4 | 64 | 256 | 65 536 |
| 32 | 5 | 160 | 1024 | about 4.3 × 10⁹ |
| 1024 | 10 | 10 240 | about 10⁶ | astronomically large |
An O(2n) algorithm becomes useless for quite small n, while O(n log n) handles millions of items easily. This is why the growth rate, not the constant factor, decides which algorithm is better.
Key points to remember - O = upper bound (at most), Ω = lower bound (at least), Θ = tight bound (exactly). - To prove f = O(g), find explicit constants c and n0 and show the inequality. - Θ holds exactly when both O and Ω hold. - Drop constants and lower-order terms; nested loops multiply, sequential code adds. - Order: 1 < log n < √(n) < n < n log n < n2 < n3 < 2n < n!
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Define asymptotic notations O, Ω and Θ with graphs. (7.5 Marks)
- Write: the formal definition of each with constants c, n0; a sketch of f(n) below c · g(n) (for O), above it (for Ω), between two curves (for Θ); one example each.
- Q2. Prove that 4n + 6 = O(n) and 4x2 - 5x + 3 = O(x2). (5 Marks)
- Write: the coefficient-sum steps, ending with explicit c and n0.
- Q3. Show that (n2 / 2) - 3n = Θ(n2). (5 Marks)
- Q4. Arrange in increasing order of growth: n!, n log n, 2n, log n, n2, √(n). (2.5 Marks)