Section 14.2

Asymptotic complexity

Definition

Asymptotic complexity describes how the cost of an algorithm §14.1 grows as the size of its input grows without limit. The cost is a count of one chosen resource, such as comparisons, executed instructions or bytes of memory, written as a function T(n)T(n) of the input size nn. Only the growth rate is kept: constant factors and the cost at small sizes are ignored. Big-O notation, O(g(n))O(g(n)), states an upper bound on the growth; Big-Omega, Ω(g(n))\Omega(g(n)), a lower bound; and Big-Theta, Θ(g(n))\Theta(g(n)), both at once.

The idea

Two programs check a list of nn records for duplicates. The first compares every record with every other record. The second sorts the records, which puts equal records next to each other, and then compares only neighbors. For 1,000,000 records the first makes 1,000,000×999,999/21{,}000{,}000 \times 999{,}999 / 2, about 5×10115 \times 10^{11}, comparisons. A good sorting method needs at most about nlog2n≈1,000,000×20=2×107n \log_2 n \approx 1{,}000{,}000 \times 20 = 2 \times 10^{7}, where log2n\log_2 n is the power to which 2 must be raised to give nn. A processor 100 times faster divides both running times by 100, but it cannot close a gap of more than 20,000 times that keeps widening as nn grows.

Asymptotic complexity keeps that widening gap and ignores the rest. Exact counts depend on details that differ between machines and ways of counting, such as whether a comparison is one step or three instructions. Such details multiply the count by a constant, or add a term that grows more slowly. The growth rate, meaning how the count changes when nn doubles, survives them all: doubling nn doubles a count that grows like nn and quadruples one that grows like n2n^2. The picture to keep is the shape of the cost curve, not the height of any single point on it.

How it works

A complexity claim is complete when it states four things.

  1. The input size nn: how the input is measured, such as the number of elements in a list, the number of bits in a number, or two measures at once, such as the numbers of vertices VV and edges EE of a graph (a set of points, the vertices, joined by lines, the edges).

  2. The resource and the cost model: what is counted, such as comparisons, executed instructions or bytes of memory, and which operations count as one step. The usual model counts each memory access, and each arithmetic operation on numbers that fit in one machine word (the fixed-size unit, such as 32 or 64 bits, that the processor handles at once), as one step.

  3. The case: the worst case, the largest cost over all inputs of size nn; the average case, the expected cost when inputs are drawn from a stated probability distribution; or the amortized cost §14.4, the total cost of a sequence of operations divided by their number. The worst-case versus average-case complexity §14.3 entry and the amortized analysis entry treat these in full.

  4. The bound: an upper bound written with OO, a lower bound with Ω\Omega, or a tight bound with Θ\Theta.

To classify a count, keep its fastest-growing term and drop that term’s constant factor: 3n2+5n+23n^2 + 5n + 2 is Θ(n2)\Theta(n^2). The common classes, from slowest-growing to fastest, are:

The base of a logarithm does not matter: logan=logab⋅logbn\log_a n = \log_a b \cdot \log_b n, so logarithms to different bases differ only by a constant factor.

Worked example

Take n=1024=210n = 1024 = 2^{10}. Then log2n=10\log_2 n = 10; nlog2n=1024×10=10,240n \log_2 n = 1024 \times 10 = 10{,}240; n2=1,048,576n^2 = 1{,}048{,}576; and 2n=210242^n = 2^{1024}, a number with 309 decimal digits. Doubling nn to 2048 gives log2n=11\log_2 n = 11, nlog2n=2048×11=22,528n \log_2 n = 2048 \times 11 = 22{,}528, n2=4,194,304n^2 = 4{,}194{,}304 and 2n=220482^n = 2^{2048}. The logarithmic count grew by 1, the linear count doubled, the quadratic count quadrupled, and the exponential count was squared.

The pair count of the duplicate check above comes from choosing a first record in nn ways and a second in n−1n - 1 ways; that counts each pair twice, once from each end, so there are n(n−1)/2n(n - 1)/2 pairs. For records numbered 1 to 4 the pairs are {1,2},{1,3},{1,4},{2,3},{2,4},{3,4}\{1, 2\}, \{1, 3\}, \{1, 4\}, \{2, 3\}, \{2, 4\}, \{3, 4\}, and 4×3/2=64 \times 3 / 2 = 6. For 8 records, 8×7/2=288 \times 7 / 2 = 28: doubling the records multiplied the work by 28/6≈4.728/6 \approx 4.7, near the factor 4 of a quadratic count.

Now prove a bound from the definition of OO given below. Suppose a procedure’s exact count is T(n)=3n+7T(n) = 3n + 7, so T(1)=3+7=10T(1) = 3 + 7 = 10 and T(2)=6+7=13T(2) = 6 + 7 = 13. To show T(n)∈O(n)T(n) \in O(n), choose the constant c=10c = 10 and the threshold n0=1n_0 = 1. For n≥1n \ge 1 the constant 7 is at most 7n7n, so T(n)=3n+7≤3n+7n=10nT(n) = 3n + 7 \le 3n + 7n = 10n. Check: T(1)=10≤10T(1) = 10 \le 10 and T(2)=13≤20T(2) = 13 \le 20. Also T(n)≥3nT(n) \ge 3n for every n≥0n \ge 0, so T(n)∈Ω(n)T(n) \in \Omega(n) with c=3c = 3, and together T(n)∈Θ(n)T(n) \in \Theta(n). The pair count is Θ(n2)\Theta(n^2): n(n−1)/2≤n2/2n(n - 1)/2 \le n^2/2 for every n≥0n \ge 0, and for n≥2n \ge 2, n−1≥n/2n - 1 \ge n/2, so n(n−1)/2≥n2/4n(n - 1)/2 \ge n^2/4.

Constant factors still decide real cases. Algorithm X needs 100n100n steps and algorithm Y needs n2n^2 steps. Y belongs to the faster-growing class, yet Y is cheaper for every n<100n < 100: at n=50n = 50, X needs 100×50=5000100 \times 50 = 5000 steps and Y needs 502=250050^2 = 2500. Both need 10,000 steps at n=100n = 100, and X is cheaper for every larger nn.

Stated precisely

Let T(n)T(n) and g(n)g(n) be functions from input sizes to non-negative numbers.

The definitions speak only about n≥n0n \ge n_0 and say nothing about smaller inputs. Many texts write T(n)=O(g(n))T(n) = O(g(n)); that equals sign means “belongs to” and cannot be read backwards.

Every complexity statement is relative to its cost model. Counting one step per arithmetic operation assumes that every number fits in one machine word. Numbers longer than a machine word, such as the integers of thousands of bits used in public-key cryptography §5.15, break that assumption, and their arithmetic must itself be counted in word operations.

Limits and common mistakes

Big-O gives only an upper bound. A linear algorithm is also in O(n2)O(n^2) and in O(2n)O(2^n); those statements are true but loose. A claim that an algorithm needs at least some amount of work uses Ω\Omega, and a claim about its exact growth uses Θ\Theta.

Asymptotic complexity is not a running time. It has no units, hides constant factors and says nothing below the threshold n0n_0. Effects that the step count ignores, such as processor caches (small fast memories that make access to recently used data cheaper), can change the real time several times over for the same count.

When the input size has a fixed upper limit, every algorithm is formally O(1)O(1), because a bounded cost is bounded by a constant. For a table that never holds more than 64 entries, the useful statement is the maximum count at 64 entries, not the class.

O(1)O(1) does not mean constant time in the sense used in cryptography. O(1)O(1) allows the cost to vary with the input below a fixed bound. A constant-time implementation §5.36 must take the same time whatever the secret values are, because any variation is a side channel §1.29: a way for information to leak through a physical effect such as timing.

The input size must be the right one. Testing whether a number NN is prime by trying every divisor up to N\sqrt{N} takes about N\sqrt{N} divisions, which looks modest. But the input is the bb-bit binary form of NN, and NN can be close to 2b2^b, so the count is about 2b/22^{b/2}: exponential in the input size. Likewise, the cost of an interpreter §2.13 (a program that executes another program one instruction at a time) grows with the number of instructions executed, which a loop can make far larger than the program.

Each case is a separate claim. A lookup in a hash table §14.7 (a table that places each key in a bucket chosen by computing a number from the key) takes O(1)O(1) expected steps when keys spread evenly and the load factor §14.9, the number of keys per bucket, stays bounded, but Θ(n)\Theta(n) steps in the worst case, when every key lands in one bucket.

Questions answered

What is n, and does the bound concern worst, average, or amortized cost?

nn is whatever measure of input size the claim defines, and a sound claim says which measure it uses and which case it bounds; without both, a bound such as O(n)O(n) (at most a constant times nn for all large nn) has no definite meaning. Typical choices of nn are the number of elements in a collection, the number of bits in a number, or two measures at once, such as the numbers of vertices VV and edges EE of a graph (points joined by lines) in a bound like O(V+E)O(V + E). The claim must also name the resource counted, such as comparisons or bytes of memory, and the case. The worst case is the largest cost over every input of size nn, and it holds even for inputs an attacker chooses. The average case is the expected cost when inputs follow a stated probability distribution, and it says nothing about other inputs. The amortized cost is the total cost of any sequence of operations divided by their number, and it allows a single operation to cost far more. One structure can have a different bound in each case: appending to a dynamic array, which copies itself into a larger block of memory when full, costs Θ(n)\Theta(n) for one append in the worst case but O(1)O(1) amortized.

Related entries: Worst-case versus average-case complexity (§14.3), Amortized analysis (§14.4), Constant-time cryptographic implementation (§5.36), Algorithm (§14.1).