# Big O notation

Big O notation is a mathematical notation used to describe the asymptotic behavior of functions. More precisely, it is used to describe an asymptotic upper bound for the magnitude of a function in terms of another, usually simpler, function.

In mathematics, it is usually used to characterize the residual term of a truncated infinite series, especially an asymptotic series. In computer science, it is useful in the analysis of the complexity of algorithms.

It was first introduced by German number theorist Paul Bachmann in his 1892 book Analytische Zahlentheorie. The notation was popularized in the work of another German number theorist Edmund Landau, hence it is sometimes called a Landau symbol. The big-O, standing for "order of", was originally a capital omicron; today the capital letter O is used, but never the digit zero.

## Uses

There are two formally close, but noticeably different usages of this notation: infinite asymptotics and infinitesimal asymptotics. This distinction is only in application and not in principle, however—the formal definition for the "big O" is the same for both cases, only with different limits for the function argument.

### Infinite asymptotics

Big O notation is useful when analyzing algorithms for efficiency. For example, the time (or the number of steps) it takes to complete a problem of size n might be found to be T(n) = 4n2 - 2n + 2.

As n grows large, the n2 term will come to dominate, so that all other terms can be neglected. Further, the coefficients will depend on the precise details of the implementation and the hardware it runs on, so they should also be neglected. Big O notation captures what remains: we write

${\displaystyle T(n)\in O(n^{2})}$

and say that the algorithm has order of n2 time complexity.

### Infinitesimal asymptotics

Big O can also be used to describe the error term in an approximation to a mathematical function. For example,

${\displaystyle e^{x}=1+x+x^{2}/2+{\hbox{O}}(x^{3})\qquad {\hbox{as}}\ x\to 0}$

expresses the fact that the error is smaller in absolute value than some constant times x3 if x is close enough to 0.

## Formal definition

Suppose f(x) and g(x) are two functions defined on some subset of the real numbers. We say

f(x) is O(g(x)) as x ${\displaystyle \to }$

if and only if

there exist numbers x0 and M such that |f(x)| ≤ M |g(x)| for x > x0.

The notation can also be used to describe the behavior of f near some real number a: we say

f(x) is O(g(x)) as x ${\displaystyle \to }$ a

if and only if

there exists numbers δ>0 and M such that |f(x)| ≤ M |g(x)| for |x - a| < δ.

If g(x) is non-zero for values of x sufficiently close to a, both of these definitions can be unified using the limit superior:

f(x) is O(g(x)) as x ${\displaystyle \to }$ a

if and only if

${\displaystyle \limsup _{x\to a}\left|{\frac {f(x)}{g(x)}}\right|<\infty .}$

In mathematics, both asymptotic behaviors near ∞ and near a are considered. In computational complexity theory, only asymptotics near ∞ are used; furthermore, only positive functions are considered, so the absolute value bars may be left out.

## Example

Take the polynomials:

${\displaystyle f(x)=6x^{4}-2x^{3}+5\,}$
${\displaystyle g(x)=x^{4}.\,}$

We say f(x) has order O(g(x)) or O(x4). From the definition of order, |f(x)| ≤ C |g(x)| for all x>1, where C is a constant.

Proof:

${\displaystyle |6x^{4}-2x^{3}+5|\leq 6x^{4}+2x^{3}+5\,}$         where x > 1
${\displaystyle |6x^{4}-2x^{3}+5|\leq 6x^{4}+2x^{4}+5x^{4}\,}$     because x3 < x4, and so on.
${\displaystyle |6x^{4}-2x^{3}+5|\leq 13x^{4}\,}$
${\displaystyle |6x^{4}-2x^{3}+5|\leq 13\,|x^{4}|.\,}$

## Matters of notation

The statement "f(x) is O(g(x))" as defined above is often written as f(x) = O(g(x)). This is a slight abuse of notation: we are not really asserting the equality of two functions. The property of being O(g(x)) is not symmetric:

${\displaystyle {\mbox{O}}(x)\,=\,{\mbox{O}}(x^{2})}$ but ${\displaystyle {\mbox{O}}(x^{2})\,\neq \,{\mbox{O}}(x)}$.

For this reason, some authors prefer a set notation and write f ${\displaystyle \in }$ O(g), thinking of O(g) as the set of all functions dominated by g.

Furthermore, an "equation" of the form

${\displaystyle f(x)=h(x)+{\mbox{O}}(g(x))}$

should be understood as "the difference of f(x) and h(x) is O(g(x))".

## Common orders of functions

Here is a list of classes of functions that are commonly encountered when analyzing algorithms. All of these are as n increases to infinity. The slower-growing functions are listed first. c is an arbitrary constant.

notation name
${\displaystyle O(1)}$ constant
${\displaystyle O(\log *n)}$ log*
${\displaystyle O(\log n)}$ logarithmic
${\displaystyle O([\log n]^{c})}$ polylogarithmic
${\displaystyle o(n)}$ sublinear
${\displaystyle O(n)}$ linear
${\displaystyle O(n\log n)}$ linearithmic, quasilinear or supralinear
${\displaystyle O(n^{2})}$ quadratic
${\displaystyle O(n^{c}),~c>1}$ polynomial, sometimes called "algebraic"
${\displaystyle O(c^{n})}$ exponential, sometimes called "geometric"
${\displaystyle O(n!)}$ factorial, sometimes called "combinatorial"

Not as common, but even larger growth is possible, such as the ackermann(n,n)

## Properties

If a function f(n) can be written as a finite sum of other functions, then the fastest growing one determines the order of f(n). For example

${\displaystyle f(n)=10\log n+5(\log n)^{3}+7n+3n^{2}+6n^{3}={\hbox{O}}(n^{3})\,\!}$.

In particular, if a function may be bounded by a polynomial in n, then as n tends to infinity, one may disregard lower-order terms of the polynomial.

O(nc) and O(cn) are very different. The latter grows much, much faster, no matter how big the constant c is (so long as it is greater than one). A function that grows faster than any power of n is called superpolynomial. One that grows slower than an exponential function of the form cn is called subexponential. An algorithm can require time that is both superpolynomial and subexponential; examples of this include the fastest algorithms known for integer factorization.

O(log n) is exactly the same as O(log(nc)). The logarithms differ only by a constant factor, (since log(nc)=c log n) and thus the big O notation ignores that. Similarly, logs with different constant bases are equivalent.

### Product

${\displaystyle O(f(n))O(g(n))=O(f(n)g(n))\,}$

### Sum

${\displaystyle O(f(n))+O(g(n))=O(\max \lbrace f(n),g(n)\rbrace )\,}$

### Multiplication by a constant

${\displaystyle O(kg(n))=O(g(n))\!\,}$, k≠0

${\displaystyle O(k\ +g(n))=O(g(n))}$ unless g(n) ∈ o(1), in which case it is O(1).

Other useful relations are given in section Big O and little o below.

## Related asymptotic notations: O, o, Ω, ω, Θ, Õ

Big O is the most commonly used asymptotic notation for comparing functions, although it is often actually an informal substitute for Θ (Theta, see below). Here, we define some related notations in terms of "big O":

Notation Definition Mathematical definition
${\displaystyle f(n)\in O(g(n))}$ asymptotic upper bound ${\displaystyle \limsup _{x\to \infty }\left|{\frac {f(x)}{g(x)}}\right|<\infty }$
${\displaystyle f(n)\in o(g(n))}$ asymptotically negligible ${\displaystyle \lim _{x\to \infty }{\frac {f(x)}{g(x)}}=0}$
${\displaystyle f(n)\in \Omega (g(n))}$ asymptotic lower bound ${\displaystyle \liminf _{x\to \infty }\left|{\frac {f(x)}{g(x)}}\right|>0}$
${\displaystyle f(n)\in \omega (g(n))}$ asymptotically dominant ${\displaystyle \lim _{x\to \infty }{\frac {g(x)}{f(x)}}=0}$
${\displaystyle f(n)\in \Theta (g(n))}$ asymptotically tight bound ${\displaystyle f\in O(g)}$ and ${\displaystyle g\in O(f)}$

Here "lim sup" and "lim inf" denote limit superior and limit inferior.

(A mnemonic for these Greek letters is that "omicron" can be read "o-micron", i.e., "o-small", whereas "omega" can be read "o-mega" or "o-big".)

The relation f(n) = o(g(n)) is read as "f(n) is little-oh of g(n)". Intuitively, it means that g(n) grows much faster than f(n). Formally, it states that the limit of f(n)/g(n) is zero.

Aside from big-O, the notations Θ and Ω are the two most often used in computer science; the lower-case o is common in mathematics but rarer in computer science. The lower-case ω is rarely used.

In casual use, O is commonly used where Θ is meant, i.e., when a tight estimate is implied. For example, one might say "heapsort is O(n log n) in the average case" when the intended meaning was "heapsort is Θ(n log n) in the average case". Both statements are true, but the latter is a stronger claim.

Another notation sometimes used in computer science is Õ (read Soft-O). f(n) = Õ(g(n)) is shorthand for f(n) = O(g(n) logkg(n)) for some k. Essentially, it is Big-O, ignoring logarithmic factors. This notation is often used to describe a class of "nitpicking" estimates (since logkn is always o(n) for any constant k).

## Big O and little o

The following properties can be useful:

• o(f) + o(f) ∈ o(f)
• o(f) o(g) ∈ o(fg)
• o(o(f)) ∈ o(f)
• o(f) ∈ O(f) (and thus the above properties apply with most combinations of o and O).

## Multiple variables

Big O (and little o, and Ω...) can also be used with multiple variables. For example, the statement

${\displaystyle f(n,m)=n^{2}+m^{3}+{\hbox{O}}(n+m){\mbox{ as }}n,m\to \infty }$

asserts that there exist constants C and N such that

${\displaystyle \forall n,m>N:f(n,m)\leq n^{2}+m^{3}+C(n+m).}$

To avoid ambiguity, the running variable should always be specified: the statement

${\displaystyle f(n,m)={\hbox{O}}(n^{m}){\mbox{ as }}n,m\to \infty }$

is quite different from

${\displaystyle \forall m:f(n,m)={\hbox{O}}(n^{m}){\mbox{ as }}n\to \infty .}$