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 of the input size . Only the growth rate is kept: constant factors and the cost at small sizes are ignored. Big-O notation, , states an upper bound on the growth; Big-Omega, , a lower bound; and Big-Theta, , both at once.
The idea
Two programs check a list of 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 , about , comparisons. A good sorting method needs at most about , where is the power to which 2 must be raised to give . 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 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 doubles, survives them all: doubling doubles a count that grows like and quadruples one that grows like . 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.
The input size : 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 and edges of a graph (a set of points, the vertices, joined by lines, the edges).
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.
The case: the worst case, the largest cost over all inputs of size ; 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.
The bound: an upper bound written with , a lower bound with , or a tight bound with .
To classify a count, keep its fastest-growing term and drop that term’s constant factor: is . The common classes, from slowest-growing to fastest, are:
, constant, such as reading the array §14.5 element at a given position;
, logarithmic, such as binary search in a sorted array, which halves the candidate range at each step;
, linear, such as one pass over a list;
, such as merge sort, which sorts each half of a list and merges the two sorted halves;
, quadratic, such as comparing every pair of records;
, exponential, such as a loop that runs once for each of the subsets of items.
The base of a logarithm does not matter: , so logarithms to different bases differ only by a constant factor.
Worked example
Take . Then ; ; ; and , a number with 309 decimal digits. Doubling to 2048 gives , , and . 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 ways and a second in ways; that counts each pair twice, once from each end, so there are pairs. For records numbered 1 to 4 the pairs are , and . For 8 records, : doubling the records multiplied the work by , near the factor 4 of a quadratic count.
Now prove a bound from the definition of given below. Suppose a procedure’s exact count is , so and . To show , choose the constant and the threshold . For the constant 7 is at most , so . Check: and . Also for every , so with , and together . The pair count is : for every , and for , , so .
Constant factors still decide real cases. Algorithm X needs steps and algorithm Y needs steps. Y belongs to the faster-growing class, yet Y is cheaper for every : at , X needs steps and Y needs . Both need 10,000 steps at , and X is cheaper for every larger .
Stated precisely
Let and be functions from input sizes to non-negative numbers.
means: there are a constant and a threshold such that for every .
means: there are a constant and a threshold such that for every .
means both and .
, read “little-o”, means that the ratio tends to 0 as grows: grows strictly more slowly than .
The definitions speak only about and say nothing about smaller inputs. Many texts write ; 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 and in ; those statements are true but loose. A claim that an algorithm needs at least some amount of work uses , and a claim about its exact growth uses .
Asymptotic complexity is not a running time. It has no units, hides constant factors and says nothing below the threshold . 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 , 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.
does not mean constant time in the sense used in cryptography. 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 is prime by trying every divisor up to takes about divisions, which looks modest. But the input is the -bit binary form of , and can be close to , so the count is about : 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 expected steps when keys spread evenly and the load factor §14.9, the number of keys per bucket, stays bounded, but 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?
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 (at most a constant times for all large ) has no definite meaning. Typical choices of 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 and edges of a graph (points joined by lines) in a bound like . 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 , 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 for one append in the worst case but amortized.
Related entries: Worst-case versus average-case complexity (§14.3), Amortized analysis (§14.4), Constant-time cryptographic implementation (§5.36), Algorithm (§14.1).