Skip to main content

Asymptotic Notation: Describing How Cost Grows

15 min readRead first: Complexity Analysis

In the previous lesson we obtained exact expressions such as T(n)=3n+4T(n) = 3n + 4 and saw that the constants depend on arbitrary counting decisions and, in reality, on the machine. What does not depend on them is the rate of growth of the function: how fast T(n)T(n) increases when nn becomes large.

Asymptotic notation is the mathematical language for describing that rate of growth. It refers to the speed of growth of the values of a function f(n)f(n) as nn tends to infinity, ignoring constant factors and lower-order terms. It is the notation used in every textbook, paper and technical interview to talk about the efficiency of algorithms.

What you will learn in this lesson
  • The formal definition of OO (big-O) and how to prove that a function belongs to O(f(n))O(f(n)).
  • The related notations Ω\Omega (big-Omega) and Θ\Theta (big-Theta), and what each one really says.
  • The algebraic properties that let us simplify expressions without going back to the definition.
  • The logarithm rules you need for analyzing algorithms, and why the base of a logarithm does not matter.
  • How to determine the order of complex expressions quickly and reliably.

Big-O: An Upper Bound on Growth​

Definition. A function T(n)T(n) is O(f(n))O(f(n)) if there exist two positive constants cc and n0n_0 such that:

T(n)≤c⋅f(n)for all n≥n0T(n) \le c \cdot f(n) \qquad \text{for all } n \ge n_0

In words: from some point n0n_0 onwards, T(n)T(n) never exceeds a constant multiple of f(n)f(n). If T(n)T(n) is O(f(n))O(f(n)), we say that T(n)T(n) has, at most, a growth rate of f(n)f(n).

Big-O: from n0 onwards, T(n) stays below c·f(n)ntimen₀c · f(n)T(n)small n: anything goesfor every n ≥ n₀: T(n) ≤ c · f(n)
The definition of Big-O in a picture. Before n₀ anything can happen; from n₀ onwards, T(n) never rises above the curve c · f(n).

Two details of the definition are important:

  • The constant cc absorbs the constant factors: 3n3n, 100n100n and n/2n/2 are all O(n)O(n). This is exactly the freedom we need, since constants depend on the machine and on the counting conventions.
  • The threshold n0n_0 means that only the behavior for large values of nn matters. What happens for small inputs is irrelevant to the definition.

Proving a bound: an example​

Let us prove that T(n)=5n4+4n3T(n) = 5n^4 + 4n^3 is O(n4)O(n^4). We need constants cc and n0n_0 such that:

5n4+4n3≤c⋅n4for every n≥n05n^4 + 4n^3 \le c \cdot n^4 \qquad \text{for every } n \ge n_0

If we pick c=6c = 6, the inequality becomes 5n4+4n3≤6n45n^4 + 4n^3 \le 6n^4, which simplifies to 4n3≤n44n^3 \le n^4, and (dividing by n3>0n^3 > 0) to n≥4n \ge 4. So it holds mathematically for every n≥4n \ge 4, i.e. with c=6c = 6 and n0=4n_0 = 4. We can see the crossing point directly in a table:

nn5n4+4n35n^4 + 4n^36n46n^45n4+4n3≤6n45n^4 + 4n^3 \le 6n^4?
196✗
211296✗
3513486✗
41,5361,536✓ (n0=4n_0 = 4)
53,6253,750✓
1054,00060,000✓

The pair (c,n0)(c, n_0) is not unique—for example, with c=9c = 9 the inequality already holds from n0=1n_0 = 1—we only need to show that some valid pair exists.

A quicker test with limits

When the limit exists, we can compare growth rates with:

lim⁡n→∞T(n)f(n)=L\lim_{n \to \infty} \frac{T(n)}{f(n)} = L
  • If LL is a finite number (including 0), then T(n)T(n) is O(f(n))O(f(n)).
  • If L=0L = 0, T(n)T(n) grows strictly more slowly than f(n)f(n).
  • If LL is a finite positive number, both functions grow at the same rate.
  • If L=∞L = \infty, T(n)T(n) grows faster, and it is not O(f(n))O(f(n)).

For example, lim⁡n→∞5n4+4n3n4=lim⁡n→∞(5+4n)=5\lim_{n \to \infty} \frac{5n^4 + 4n^3}{n^4} = \lim_{n \to \infty} \left(5 + \frac{4}{n}\right) = 5, so both functions grow at the same rate.

Big-O is a bound, not an exact description​

Since OO is an upper bound, a function belongs to many classes: 3n+43n + 4 is O(n)O(n), but also O(n2)O(n^2) and O(2n)O(2^n), because all those functions grow at least as fast as nn. These statements are true but not very informative. When we analyze an algorithm, we always give the tightest bound we can prove: saying that the sum of a vector is O(n2)O(n^2) is correct, but misleading.

Big-O does not mean "worst case"

A common misconception is that OO describes the worst case. It does not: OO, Ω\Omega and Θ\Theta describe functions, and any of the functions we studied in the previous lesson can be described with them. The worst-case time of linear search is O(n)O(n), its best-case time is O(1)O(1), its average-case time is O(n)O(n). When someone says that "an algorithm is O(n2)O(n^2)" without further detail, they usually mean that its worst-case time is O(n2)O(n^2), but the two ideas are independent.


Big-Omega and Big-Theta​

Big-O gives an upper bound. Two related notations complete the picture:

  • Ω\Omega (big-Omega), a lower bound. T(n)T(n) is Ω(f(n))\Omega(f(n)) if there exist positive constants cc and n0n_0 such that T(n)≥c⋅f(n)T(n) \ge c \cdot f(n) for all n≥n0n \ge n_0. It says that T(n)T(n) grows at least as fast as f(n)f(n).
  • Θ\Theta (big-Theta), an exact order. T(n)T(n) is Θ(f(n))\Theta(f(n)) if it is both O(f(n))O(f(n)) and Ω(f(n))\Omega(f(n)), that is, if there exist positive constants c1c_1, c2c_2 and n0n_0 such that c1⋅f(n)≤T(n)≤c2⋅f(n)c_1 \cdot f(n) \le T(n) \le c_2 \cdot f(n) for all n≥n0n \ge n_0. It says that T(n)T(n) grows exactly at the rate of f(n)f(n), up to constants.
NotationMeaningAnalogyExample
T(n)∈O(f(n))T(n) \in O(f(n))grows at most as fast as f(n)f(n)≤\le3n+4∈O(n2)3n + 4 \in O(n^2) (true, but not tight)
T(n)∈Ω(f(n))T(n) \in \Omega(f(n))grows at least as fast as f(n)f(n)≥\ge3n+4∈Ω(n)3n + 4 \in \Omega(n)
T(n)∈Θ(f(n))T(n) \in \Theta(f(n))grows exactly as fast as f(n)f(n)==3n+4∈Θ(n)3n + 4 \in \Theta(n)

These notations also allow us to talk about problems, not only algorithms. For example, we saw in the case study that any algorithm for the maximum pairwise product must read all the elements, so every algorithm for that problem takes Ω(n)\Omega(n) time in the worst case. Since the linear scan is O(n)O(n), it is optimal: no algorithm can have a better growth rate. Lower bounds are what allow us to measure the inherent difficulty of a problem.

Notation in practice

In everyday use (and in this course), people often write OO when they actually mean Θ\Theta: "the sum of a vector is O(n)O(n)" is usually intended as "it grows exactly linearly". Also, although O(f(n))O(f(n)) is formally a set of functions, you will commonly see T(n)=O(f(n))T(n) = O(f(n)) instead of T(n)∈O(f(n))T(n) \in O(f(n)). Read the "==" as "is", not as a true equality: n=O(n2)n = O(n^2) is correct, but O(n2)=nO(n^2) = n makes no sense.


Why Constants Can Be Ignored (and When They Matter)​

With asymptotic notation, we can evaluate a program by its growth rate, ignoring constants: a program that is O(n)O(n) will be better than one that is O(n2)O(n^2) for large enough inputs.

However, there are values of nn for which an algorithm with time 100n100n behaves worse than one with time n2n^2: as long as n<100n \lt 100, n2n^2 is smaller than 100n100n. Asymptotic notation guarantees that the linear algorithm wins eventually, not that it wins for every input size.

In practice:

  • For large inputs, the growth rate is decisive, and the constants become irrelevant.
  • For small inputs, constants can matter, and a "worse" algorithm may be faster. This is why, for example, efficient sorting libraries often switch to a simple quadratic algorithm (insertion sort) for very small subarrays.

To facilitate the analysis, the values of the constants are usually removed. But a good engineer remembers that they exist.


Properties of the Asymptotic Notation​

The following properties allow us to simplify expressions without returning to the definition every time:

  1. Reflexivity. T(n)T(n) is O(T(n))O(T(n)). Every function is a bound of itself (take c=1c = 1).
  2. Constant factors. O(c⋅f(n))=O(f(n))O(c \cdot f(n)) = O(f(n)) for any constant c>0c \gt 0. Multiplying by a constant does not change the class.
  3. Sum rule. If T(n)=T1(n)+T2(n)+⋯+Tm(n)T(n) = T_1(n) + T_2(n) + \cdots + T_m(n), with mm fixed, then T(n)T(n) is of the order of the largest term: O(max⁡(T1(n),T2(n),…,Tm(n)))O(\max(T_1(n), T_2(n), \dots, T_m(n))). This is the rule for sequential code: the slowest part dominates.
  4. Product rule. If T(n)=T1(n)⋅T2(n)⋯Tm(n)T(n) = T_1(n) \cdot T_2(n) \cdots T_m(n), then T(n)T(n) is O(T1(n)⋅T2(n)⋯Tm(n))O(T_1(n) \cdot T_2(n) \cdots T_m(n)). This is the rule for nested code: a loop of O(n)O(n) iterations whose body costs O(n)O(n) is O(n2)O(n^2).
  5. Base of logarithms. O(log⁡an)=O(log⁡bn)=O(log⁡n)O(\log_a n) = O(\log_b n) = O(\log n) for any bases a,b>1a, b \gt 1.
  6. Powers inside a logarithm. O(log⁡(nk))=O(klog⁡n)=O(log⁡n)O(\log(n^k)) = O(k \log n) = O(\log n) for any constant kk.
  7. Powers of a logarithm. O(log⁡kn)≠O(log⁡n)O(\log^k n) \ne O(\log n) for k>1k \gt 1. Here log⁡kn\log^k n means (log⁡n)k(\log n)^k, which grows faster than log⁡n\log n.

Properties 5, 6 and 7 deserve a closer look, because logarithms appear constantly in algorithm analysis.

Why the base of the logarithm does not matter​

The change-of-base formula says that, for any bases a,b>1a, b \gt 1:

log⁡an=log⁡bnlog⁡ba=1log⁡ba⋅log⁡bn\log_a n = \frac{\log_b n}{\log_b a} = \frac{1}{\log_b a} \cdot \log_b n

Since 1log⁡ba\frac{1}{\log_b a} is a constant, logarithms in different bases differ only by a constant factor, and property 2 tells us that constant factors do not change the class. That is why we simply write O(log⁡n)O(\log n). In computing, when a base is needed, it is usually 2, because many algorithms halve the problem at each step.

Power inside versus power outside​

Compare log⁡(n8)\log(n^8) and (log⁡n)8(\log n)^8:

  • log⁡(n8)=8log⁡n\log(n^8) = 8 \log n, which is just a constant times log⁡n\log n, so it is O(log⁡n)O(\log n) (property 6).
  • (log⁡n)8(\log n)^8 is the logarithm raised to the eighth power. It grows faster than any constant times log⁡n\log n, so it is not O(log⁡n)O(\log n) (property 7). It is still much smaller than any power of nn: even (log⁡n)8(\log n)^8 is eventually smaller than n\sqrt{n}, although only for very large nn.
ProofRules of the big O notation ProofLogarithms grow more slowly than any power

Logarithm Rules to Remember​

These identities, valid for positive values and bases different from 1, are used continuously when analyzing algorithms:

  1. log⁡a(nk)=k⋅log⁡an\log_a(n^k) = k \cdot \log_a n
  2. log⁡a(n⋅m)=log⁡an+log⁡am\log_a(n \cdot m) = \log_a n + \log_a m
  3. log⁡a(n/m)=log⁡an−log⁡am\log_a(n / m) = \log_a n - \log_a m
  4. nlog⁡ab=blog⁡ann^{\log_a b} = b^{\log_a n}
  5. log⁡ab⋅log⁡bn=log⁡an\log_a b \cdot \log_b n = \log_a n

Rule 4 is less familiar but very useful, especially in divide and conquer: it lets you swap the base bb and the argument nn, turning an expression like 3log⁡2n3^{\log_2 n} (which looks exponential at first sight) into the polynomial nlog⁡23≈n1.585n^{\log_2 3} \approx n^{1.585}. To see why it holds, apply log⁡a\log_a to both sides and use rule 1 (log⁡a(xy)=y⋅log⁡ax\log_a(x^y) = y \cdot \log_a x):

  • Left-hand side: log⁡a ⁣(nlog⁡ab)=log⁡ab⋅log⁡an\log_a\!\left(n^{\log_a b}\right) = \log_a b \cdot \log_a n
  • Right-hand side: log⁡a ⁣(blog⁡an)=log⁡an⋅log⁡ab\log_a\!\left(b^{\log_a n}\right) = \log_a n \cdot \log_a b

Both sides produce the exact same product, so the original positive numbers must be equal. With concrete numbers (a=2,b=3,n=8a = 2, b = 3, n = 8): 3log⁡28=33=273^{\log_2 8} = 3^3 = 27 and 8log⁡23=(23)log⁡23=(2log⁡23)3=33=278^{\log_2 3} = (2^3)^{\log_2 3} = (2^{\log_2 3})^3 = 3^3 = 27.

Check your understanding​

🧠 Is nlog⁡3a=alog⁡3nn^{\log_3 a} = a^{\log_3 n}?

Yes. It is rule 4 with base 3. Taking log⁡3\log_3 of both sides gives log⁡3a⋅log⁡3n\log_3 a \cdot \log_3 n in both cases.

🧠 Is O(log⁡3(n8))=O(8log⁡3n)O(\log_3 (n^8)) = O(8 \log_3 n)? And O((log⁡3n)8)=O(8log⁡3n)O((\log_3 n)^8) = O(8 \log_3 n)?

The first one, yes: by rule 1, log⁡3(n8)=8log⁡3n\log_3(n^8) = 8 \log_3 n, and both are O(log⁡n)O(\log n). The second one, no: (log⁡3n)8(\log_3 n)^8 is a power of the logarithm and grows faster than log⁡n\log n (property 7). Always pay attention to whether the exponent applies to nn or to the logarithm.

🧠 Is log⁡3(4n)=log⁡34⋅log⁡3n\log_3(4n) = \log_3 4 \cdot \log_3 n?

No. The logarithm of a product is the sum of the logarithms (rule 2): log⁡3(4n)=log⁡34+log⁡3n\log_3(4n) = \log_3 4 + \log_3 n. Asymptotically, it is still O(log⁡n)O(\log n), because log⁡34\log_3 4 is just a constant added to log⁡3n\log_3 n.

🧠 Is log⁡34⋅log⁡4n=log⁡3n\log_3 4 \cdot \log_4 n = \log_3 n?

Yes. It is rule 5 with a=3a = 3 and b=4b = 4, a form of the change-of-base formula.


Determining the Order of an Expression​

In practice, finding the order of an expression consists of identifying its dominant term and dropping its constant factor. To identify the dominant term, it helps to remember the order of growth of the most common functions, from slowest to fastest:

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

and two general facts:

  • Any power of nn (even a tiny one, such as n0.1n^{0.1}) eventually grows faster than any power of log⁡n\log n.
  • Any exponential cnc^n with c>1c \gt 1 eventually grows faster than any polynomial nkn^k.

The following examples apply these ideas. Cover the "Order" column and try to find the order of each expression yourself; the "Why?" tab explains the ones that hide a trap.

T(n)T(n)Order
45n3+100\frac{4}{5} n^3 + 100O(n3)O(n^3)
10nlog⁡n10 n \log nO(nlog⁡n)O(n \log n)
100n+310000nlog⁡n100n + \frac{3}{10000} n \log nO(nlog⁡n)O(n \log n)
2n+n2+50n2\sqrt{n} + n^2 + 50nO(n2)O(n^2)
nlog⁡n+2n+13n \log n + 2n + 13O(nlog⁡n)O(n \log n)
n2+4(n+log⁡n)2⋅nn^2 + 4(n + \log n)^2 \cdot nO(n3)O(n^3)
4n2+2log⁡nn+1\frac{4n^2 + 2 \log n}{n + 1}O(n)O(n)
16log⁡n+n0.416 \log n + n^{0.4}O(n0.4)O(n^{0.4})
🧠 Checkpoint: Order these functions

Question: Order these functions by increasing growth rate: n3n^3, log⁡2n\log_2 n, n0.5n^{0.5}, 2n2^n, nlog⁡2nn \log_2 n, log⁡10n\log_{10} n, n2/log⁡nn^2 / \log n, n1.01n^{1.01}, n!n!.

Answer: log⁡2n\log_2 n and log⁡10n\log_{10} n (same order; they differ only by a constant factor) <\lt n0.5n^{0.5} <\lt nlog⁡2nn \log_2 n <\lt n1.01n^{1.01} <\lt n2/log⁡nn^2 / \log n <\lt n3n^3 <\lt 2n2^n <\lt n!n!.

The most delicate pair is nlog⁡nn \log n versus n1.01n^{1.01}: dividing both by nn, we compare log⁡n\log n with n0.01n^{0.01}, and any power of nn eventually beats the logarithm. However, the crossover happens for an astronomically large nn, so for any practical input size, nlog⁡nn \log n is the smaller of the two.

Common mistakes
  • Reading OO as an exact order. 3n∈O(n2)3n \in O(n^2) is true but not tight; use Θ\Theta to state the exact order.
  • Identifying OO with the worst case and Ω\Omega with the best case. Bounds and cases are independent ideas: each case has its own OO, Ω\Omega and Θ\Theta.
  • Dropping powers of logarithms. The base of a logarithm does not matter, but log⁡2n∉O(log⁡n)\log^2 n \notin O(\log n).
  • Not simplifying. Write O(n2)O(n^2), not O(3n2+n)O(3n^2 + n) or O(2n2)O(2n^2).

Summary​

  • Asymptotic notation describes the growth rate of a function, ignoring constant factors and the behavior for small nn.
  • T(n)∈O(f(n))T(n) \in O(f(n)) means T(n)≤c⋅f(n)T(n) \le c \cdot f(n) from some n0n_0 onwards: an upper bound. Ω\Omega is a lower bound and Θ\Theta an exact order.
  • Asymptotic bounds and the best, worst and average cases are independent ideas.
  • Lower bounds on problems let us prove that an algorithm is optimal.
  • Key properties: constants are dropped, a sum is dominated by its largest term, products multiply, and the base of logarithms is irrelevant, but powers of logarithms are not.
  • To find the order of an expression, identify the dominant term using the hierarchy of functions.

Practice​

  • ✍️ Prove it. Using the definition, find constants cc and n0n_0 that prove that 3n2+59n3n^2 + 59n is O(n2)O(n^2). Then prove that it is also Ω(n2)\Omega(n^2), and conclude that it is Θ(n2)\Theta(n^2).
  • ❌ Disprove it. Show that n2n^2 is not O(n)O(n). (Hint: assume that constants cc and n0n_0 exist and find an n≥n0n \ge n_0 that breaks the inequality.)
  • 🔁 Logarithms. Simplify 2log⁡2n2^{\log_2 n}, 4log⁡2n4^{\log_2 n} and nlog⁡n2n^{\log_n 2}. Which of them grow polynomially?
  • 📊 Visualize. Plot 100n100n, n2n^2 and nlog⁡2nn \log_2 n for nn between 1 and 200 (with a spreadsheet or any plotting tool). Where does each curve overtake the others?
  • 🎯 Tight bounds. For each of the methods you analyzed in the previous lesson (maximum of a vector, checking if a vector is sorted), give the tightest bound for the best and the worst case.
Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.