Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by German mathematicians Paul Bachmann, Edmund Landau, and others, collectively called Bachmann–Landau notation or asymptotic notation. The letter O was chosen by Bachmann to stand for Ordnung, meaning the order of approximation.
In computer science, big O notation is used to classify algorithms according to how their run time or space requirements grow as the input size grows.[1] In analytic number theory, big O notation is often used to express a bound on the difference between an arithmetical function and a better understood approximation; a famous example of such a difference is the remainder term in the prime number theorem. Big O notation is also used in many other fields to provide similar estimates.
Big O notation characterizes functions according to their growth rates: different functions with the same asymptotic growth rate may be represented using the same O notation. The letter O is used because the growth rate of a function is also referred to as the order of the function. A description of a function in terms of big O notation usually only provides an upper bound on the growth rate of the function.
Associated with big O notation are several related notations, using the symbols, and, to describe other kinds of bounds on asymptotic growth rates.
Let
f
g
g(x)
x
f(x)
g(x)
f(x)
g(x)
x
f(x)=Ol(g(x)r)
M
x0
x
f
a
a=0
\delta
M
x
g(x)
x
a
infty
f
g
a
style\limsupx\to
In computer science, a slightly more restrictive definition is common:
f
g
f(x)=Ol(g(x)r)
M
n0
f(n)\leMg(n)
n\gen0
In typical usage the notation is asymptotical, that is, it refers to very large . In this setting, the contribution of the terms that grow "most quickly" will eventually make the other ones irrelevant. As a result, the following simplification rules can be applied:
For example, let, and suppose we wish to simplify this function, using notation, to describe its growth rate as approaches infinity. This function is the sum of three terms:,, and . Of these three terms, the one with the highest growth rate is the one with the largest exponent as a function of, namely . Now one may apply the second rule: is a product of and in which the first factor does not depend on . Omitting this factor results in the simplified form . Thus, we say that is a "big O" of . Mathematically, we can write . One may confirm this calculation using the formal definition: let and . Applying the formal definition from above, the statement that is equivalent to its expansion,for some suitable choice of a real number and a positive real number and for all . To prove this, let and . Then, for all :so
Big O notation has two main areas of application:
In both applications, the function appearing within the is typically chosen to be as simple as possible, omitting constant factors and lower order terms.
There are two formally close, but noticeably different, usages of this notation:
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.
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 might be found to be . As grows large, the term will come to dominate, so that all other terms can be neglected—for instance when, the term is 1000 times as large as the term. Ignoring the latter would have negligible effect on the expression's value for most purposes. Further, the coefficients become irrelevant if we compare to any other order of expression, such as an expression containing a term or . Even if, if, the latter will always exceed the former once grows larger than, viz. . Additionally, the number of steps depends on the details of the machine model on which the algorithm runs, but different types of machines typically vary by only a constant factor in the number of steps needed to execute an algorithm. So the big O notation captures what remains: we write either
T(n)=O(n2)
T(n)\inO(n2)
Big O can also be used to describe the error term in an approximation to a mathematical function. The most significant terms are written explicitly, and then the least-significant terms are summarized in a single big O term. Consider, for example, the exponential series and two expressions of it that are valid when is small:
\begin{align} ex&=1+x+
x2 | + | |
2! |
x3 | + | |
3! |
x4 | |
4! |
+...b&forallx\\[4pt] &=1+x+
x2 | |
2 |
+O(x3)&asx\to0\\[4pt] &=1+x+O(x2)&asx\to0 \end{align}
If the function can be written as a finite sum of other functions, then the fastest growing one determines the order of . For example,
f(n)=9logn+5(logn)4+3n2+2n3=O(n3) asn\toinfty.
We may ignore any powers of inside of the logarithms. The set is exactly the same as . The logarithms differ only by a constant factor (since) and thus the big O notation ignores that. Similarly, logs with different constant bases are equivalent. On the other hand, exponentials with different bases are not of the same order. For example, and are not of the same order.
Changing units may or may not affect the order of the resulting algorithm. Changing units is equivalent to multiplying the appropriate variable by a constant wherever it appears. For example, if an algorithm runs in the order of, replacing by means the algorithm runs in the order of, and the big O notation ignores the constant . This can be written as . If, however, an algorithm runs in the order of, replacing with gives . This is not equivalent to in general. Changing variables may also affect the order of the resulting algorithm. For example, if an algorithm's run time is when measured in terms of the number of digits of an input number, then its run time is when measured as a function of the input number itself, because .
f1=O(g1)andf2=O(g2) ⇒ f1f2=O(g1g2)
f ⋅ O(g)=O(fg)
If
f1=O(g1)
f2=O(g2)
f1+f2=O(max(g1,g2))
f1=O(g)
f2=O(g)
f1+f2\inO(g)
O(g)
Let be a nonzero constant. Then
O(|k| ⋅ g)=O(g)
f=O(g)
k ⋅ f=O(g).
Big O (and little o, Ω, etc.) can also be used with multiple variables. To define big O formally for multiple variables, suppose
f
g
\Rn
f(x)isO(g(x)) asx\toinfty
M
C>0
|f(x)|\leC|g(x)|
x
xi\geqM
i.
xi\geqM
i
\|x\|infty\geM
\|x\|infty
f(n,m)=n2+m3+O(n+m) asn,m\toinfty
|f(n,m)-(n2+m3)|\leC|n+m|
m\geqM
n\geqM
x
f(n,m)=O(nm) asn,m\toinfty
\existsC\existsM\foralln\forallm …
\forallm\colon~f(n,m)=O(nm) asn\toinfty
\forallm\existsC\existsM\foralln …
Under this definition, the subset on which a function is defined is significant when generalizing statements from the univariate setting to the multivariate setting. For example, if
f(n,m)=1
g(n,m)=n
f(n,m)=O(g(n,m))
f
g
[1,infty)2
[0,infty)2
This is not the only generalization of big O to multivariate functions, and in practice, there is some inconsistency in the choice of definition.[5]
The statement "f(x) is O(g(x))" as defined above is usually written as . Some consider this to be an abuse of notation, since the use of the equals sign could be misleading as it suggests a symmetry that this statement does not have. As de Bruijn says, is true but is not.[6] Knuth describes such statements as "one-way equalities", since if the sides could be reversed, "we could deduce ridiculous things like from the identities and ."[7] In another letter, Knuth also pointed out that "the equality sign is not symmetric with respect to such notations", as, in this notation, "mathematicians customarily use the = sign as they use the word "is" in English: Aristotle is a man, but a man isn't necessarily Aristotle".[8]
For these reasons, it would be more precise to use set notation and write (read as: "f(x) is an element of O(g(x))", or "f(x) is in the set O(g(x))"), thinking of O(g(x)) as the class of all functions h(x) such that for some positive real number C. However, the use of the equals sign is customary.[6] [7]
Big O notation can also be used in conjunction with other arithmetic operators in more complicated equations. For example, denotes the collection of functions having the growth of h(x) plus a part whose growth is limited to that of f(x). Thus,
g(x)=h(x)+O(f(x))
g(x)-h(x)=O(f(x)).
Suppose an algorithm is being developed to operate on a set of n elements. Its developers are interested in finding a function T(n) that will express how long the algorithm will take to run (in some arbitrary measurement of time) in terms of the number of elements in the input set. The algorithm works by first calling a subroutine to sort the elements in the set and then perform its own operations. The sort has a known time complexity of O(n2), and after the subroutine runs the algorithm must take an additional steps before it terminates. Thus the overall time complexity of the algorithm can be expressed as . Here the terms are subsumed within the faster-growing O(n2). Again, this usage disregards some of the formal meaning of the "=" symbol, but it does allow one to use the big O notation as a kind of convenient placeholder.
In more complicated usage, O(·) can appear in different places in an equation, even several times on each side. For example, the following are true for
n\toinfty
Big O is typeset as an italicized uppercase "O", as in the following example:
O(n2)
l{O}
Here is a list of classes of functions that are commonly encountered when analyzing the running time of an algorithm. In each case, c is a positive constant and n increases without bound. The slower-growing functions are generally listed first.
Notation | Name | Example | ||||
---|---|---|---|---|---|---|
O(1) | Finding the median value for a sorted array of numbers; Calculating (-1)n | |||||
O(\alpha(n)) | Amortized complexity per operation for the Disjoint-set data structure | |||||
O(loglogn) | double logarithmic | Average number of comparisons spent finding an item using interpolation search in a sorted array of uniformly distributed values | ||||
O(logn) | Finding an item in a sorted array with a binary search or a balanced search tree as well as all operations in a binomial heap | |||||
O((logn)c) | Matrix chain ordering can be solved in polylogarithmic time on a parallel random-access machine. | |||||
O(nc) | fractional power | Searching in a k-d tree | ||||
O(n) | Finding an item in an unsorted list or in an unsorted array; adding two n-bit integers by ripple carry | |||||
O(nlog*n) | n log-star n | Performing triangulation of a simple polygon using Seidel's algorithm, where log*(n)= \begin{cases} 0,&ifn\leq1\\ 1+log*(logn),&ifn>1 \end{cases} | ||||
O(nlogn)=O(logn!) | linearithmic, loglinear, quasilinear, or "n log n" | Performing a fast Fourier transform; fastest possible comparison sort; heapsort and merge sort | ||||
O(n2) | Multiplying two n-digit numbers by schoolbook multiplication; simple sorting algorithms, such as bubble sort, selection sort and insertion sort; (worst-case) bound on some usually faster sorting algorithms such as quicksort, Shellsort, and tree sort | |||||
O(nc) | polynomial or algebraic | Tree-adjoining grammar parsing; maximum matching for bipartite graphs; finding the determinant with LU decomposition | ||||
Ln[\alpha,c]=
| Factoring a number using the quadratic sieve or number field sieve | |||||
O(cn) | Finding the (exact) solution to the travelling salesman problem using dynamic programming; determining if two logical statements are equivalent using brute-force search | |||||
O(n!) | Solving the travelling salesman problem via brute-force search; generating all unrestricted permutations of a poset; finding the determinant with Laplace expansion; enumerating all partitions of a set |
f(n)=O(n!)
f(n)=O\left(nn\right)
k>0
O(nc(logn)k)
O(nc+\varepsilon)
Big O is widely used in computer science. Together with some other related notations, it forms the family of Bachmann–Landau notations.
Intuitively, the assertion " is " (read " is little-o of ") means that grows much faster than, or equivalently grows much slower than . As before, let f be a real or complex valued function and g a real valued function, both defined on some unbounded subset of the positive real numbers, such that g(x) is strictly positive for all large enough values of x. One writes
f(x)=o(g(x)) asx\toinfty
x0
|f(x)|\leq\varepsilong(x) forallx\geqx0.
2x=o(x2)
1/x=o(1),
x\toinfty.
The difference between the definition of the big-O notation and the definition of little-o is that while the former has to be true for at least one constant M, the latter must hold for every positive constant, however small.[14] In this way, little-o notation makes a stronger statement than the corresponding big-O notation: every function that is little-o of g is also big-O of g, but not every function that is big-O of g is little-o of g. For example,
2x2=O(x2)
If g(x) is nonzero, or at least becomes nonzero beyond a certain point, the relation
f(x)=o(g(x))
\limx
f(x) | |
g(x) |
=0
Little-o respects a number of arithmetic operations. For example,
if is a nonzero constant and
f=o(g)
c ⋅ f=o(g)
if
f=o(F)
g=o(G)
f ⋅ g=o(F ⋅ G).
if
f=o(g)
g=o(h)
f=o(h).
Another asymptotic notation is
\Omega
f(x)=\Omega(g(x))
x\toa
where a is some real number,
infty
-infty
The Hardy–Littlewood definition is used mainly in analytic number theory, and the Knuth definition mainly in computational complexity theory; the definitions are not equivalent.
In 1914 Godfrey Harold Hardy and John Edensor Littlewood introduced the new symbol
\Omega
f(x)=\Omega(g(x))
x\toinfty
\limsupx\left|
f(x) | |
g(x) |
\right|>0.
Thus
f(x)=\Omega(g(x))
f(x)=o(g(x))
In 1916 the same authors introduced the two new symbols
\OmegaR
\OmegaL
f(x)=\OmegaR(g(x))
x\toinfty
\limsupx
f(x) | |
g(x) |
>0
f(x)=\OmegaL(g(x))
x\toinfty
\liminfx
f(x) | |
g(x) |
<0.
These symbols were used by Edmund Landau, with the same meanings, in 1924.[18] After Landau, the notations were never used again exactly thus;
\OmegaR
\Omega+
\OmegaL
\Omega-
These three symbols
\Omega,\Omega+,\Omega-
f(x)=\Omega\pm(g(x))
f(x)=\Omega+(g(x))
f(x)=\Omega-(g(x))
We have
\sinx=\Omega(1)
x\toinfty,
and more precisely
\sinx=\Omega\pm(1)
x\toinfty.
We have
\sinx+1=\Omega(1)
x\toinfty,
and more precisely
\sinx+1=\Omega+(1)
x\toinfty;
however
\sinx+1\not=\Omega-(1)
x\toinfty.
In 1976 Donald Knuth published a paper to justify his use of the
\Omega
f(x)=\Omega(g(x))\Leftrightarrowg(x)=O(f(x))
with the comment: "Although I have changed Hardy and Littlewood's definition of
\Omega
Notation | Name | Description | Formal definition | Limit definition[22] [23] [24] | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
f(n)=o(g(n)) | Small O; Small Oh; Little O; Little Oh | is dominated by asymptotically (for any constant factor k | \forallk>0\existsn0\foralln>n0\colon | f(n) | < k\, g(n) | \limn
=0 | ||||||
f(n)=O(g(n)) | Big O; Big Oh; Big Omicron | f | is asymptotically bounded above by (up to constant factor k | \existsk>0\existsn0\foralln>n0\colon | f(n) | \leq k\, g(n) | \limsupn
<infty | |||||
f(n)\asympg(n) f(n)=\Theta(g(n)) | Of the same order as (Hardy); Big Theta (Knuth) | is asymptotically bounded by both above (with constant factor k2 k1 | \existsk1>0\existsk2>0\existsn0\foralln>n0\colon k1g(n)\leqf(n)\leqk2g(n) | f(n)=O(g(n)) g(n)=O(f(n)) | ||||||||
f(n)\simg(n) | Asymptotic equivalence | is equal to asymptotically | \forall\varepsilon>0\existsn0\foralln>n0\colon\left | \frac - 1 \right | < \varepsilon | \limn
=1 | ||||||
f(n)=\Omega(g(n)) | Big Omega in complexity theory (Knuth) | is bounded below by asymptotically | \existsk>0\existsn0\foralln>n0\colonf(n)\geqkg(n) | \liminfn
>0 | ||||||||
f(n)=\omega(g(n)) | Small Omega; Little Omega | dominates asymptotically | \forallk>0\existsn0\foralln>n0\colonf(n)>kg(n) | \limn
=infty | ||||||||
f(n)=\Omega(g(n)) | Big Omega in number theory (Hardy–Littlewood) | f | is not dominated by asymptotically | \existsk>0\foralln0\existsn>n0\colon | f(n) | \geq k\, g(n) | \limsupn
>0 |
The limit definitions assume
g(n)>0
n
o,O,\Theta,\sim,
\Omega,\omega
<,\leq, ≈ ,=,
\geq,>
\Omega
Computer science uses the big
O
\Theta
o
\omega
\Omega
O
o
\asymp
\Omega
\sim
\omega
Informally, especially in computer science, the big O notation often can be used somewhat differently to describe an asymptotic tight bound where using big Theta Θ notation might be more factually appropriate in a given context.[27] For example, when considering a function T(n) = 73n3 + 22n2 + 58, all of the following are generally acceptable, but tighter bounds (such as numbers 2 and 3 below) are usually strongly preferred over looser bounds (such as number 1 below).
The equivalent English statements are respectively:
So while all three statements are true, progressively more information is contained in each. In some fields, however, the big O notation (number 2 in the lists above) would be used more commonly than the big Theta notation (items numbered 3 in the lists above). For example, if T(n) represents the running time of a newly developed algorithm for input size n, the inventors and users of the algorithm might be more inclined to put an upper asymptotic bound on how long it will take to run without making an explicit statement about the lower asymptotic bound.
In their book Introduction to Algorithms, Cormen, Leiserson, Rivest and Stein consider the set of functions f which satisfy
f(n)=O(g(n)) (n\toinfty)~.
In a correct notation this set can, for instance, be called O(g), where
The authors state that the use of equality operator (=) to denote set membership rather than the set membership operator (∈) is an abuse of notation, but that doing so has advantages.[29] Inside an equation or inequality, the use of asymptotic notation stands for an anonymous function in the set O(g), which eliminates lower-order terms, and helps to reduce inessential clutter in equations, for example:[30]
2n2+3n+1=2n2+O(n).
Another notation sometimes used in computer science is Õ (read soft-O), which hides polylogarithmic factors. There are two definitions in use: some authors use f(n) = Õ(g(n)) as shorthand for for some k, while others use it as shorthand for .[31] When is polynomial in n, there is no difference; however, the latter definition allows one to say, e.g. that
n2n=\tildeO(2n)
logkn=\tildeO(1)
Also, the L notation, defined as
Ln[\alpha,c]=
(c+o(1))(lnn)\alpha(lnlnn)1-\alpha | |
e |
,
The generalization to functions taking values in any normed vector space is straightforward (replacing absolute values by norms), where f and g need not take their values in the same space. A generalization to functions g taking values in any topological group is also possible.The "limiting process" can also be generalized by introducing an arbitrary filter base, i.e. to directed nets f and g. The o notation can be used to define derivatives and differentiability in quite general spaces, and also (asymptotical) equivalence of functions,
f\simg\iff(f-g)\ino(g)
The symbol O was first introduced by number theorist Paul Bachmann in 1894, in the second volume of his book Analytische Zahlentheorie ("analytic number theory").[33] The number theorist Edmund Landau adopted it, and was thus inspired to introduce in 1909 the notation o;[34] hence both are now called Landau symbols. These notations were used in applied mathematics during the 1950s for asymptotic analysis.[35] The symbol
\Omega
\OmegaR
\OmegaL
\Omega+
\Omega-
\Omega
The symbol
\sim
\asymp
f(x)\asympg(x)
f(x)=O(g(x))
g(x)=O(f(x))
\asymp-
f\asymp-g
f\simKg
K\not=0
In the 1970s the big O was popularized in computer science by Donald Knuth, who proposed the different notation
f(x)=\Theta(g(x))
f(x)\asympg(x)
Two other symbols coined by Hardy were (in terms of the modern O notation)
f\preccurlyeqg\ifff=O(g)
f\precg\ifff=o(g);
(Hardy however never defined or used the notation
\prec\prec
\ll
\preccurlyeq
\prec
Hardy's symbols
\preccurlyeq
\prec
\asymp-
\ll
O
f\llg\ifff=O(g),
The big-O originally stands for "order of" ("Ordnung", Bachmann 1894), and is thus a Latin letter. Neither Bachmann nor Landau ever call it "Omicron". The symbol was much later on (1976) viewed by Knuth as a capital omicron, probably in reference to his definition of the symbol Omega. The digit zero should not be used.
lO(NlogN)
(k,n-k)
l{O}*(2p)