Big ''O'' notation is a mathematical notation that describes the
limiting behavior of a
function when the
argument
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
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
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, big O notation is used to
classify algorithms according to how their run time or space requirements grow as the input size grows.
In
analytic number theory
In mathematics, analytic number theory is a branch of number theory that uses methods from mathematical analysis to solve problems about the integers. It is often said to have begun with Peter Gustav Lejeune Dirichlet's 1837 introduction of Dir ...
, big O notation is often used to express a bound on the difference between an
arithmetical function
In number theory, an arithmetic, arithmetical, or number-theoretic function is generally any Function (mathematics), function whose Domain of a function, domain is the set of natural number, positive integers and whose range is a subset of the co ...
and a better understood approximation; one well-known example is the remainder term in the
prime number theorem
In mathematics, the prime number theorem (PNT) describes the asymptotic analysis, asymptotic distribution of the prime numbers among the positive integers. It formalizes the intuitive idea that primes become less common as they become larger by p ...
. 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
In mathematics, particularly in order theory, an upper bound or majorant of a subset of some preordered set is an element of that is every element of .
Dually, a lower bound or minorant of is defined to be an element of that is less ...
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.
Formal definition
Let
the function to be estimated, be a
real or
complex
Complex commonly refers to:
* Complexity, the behaviour of a system whose components interact in multiple ways so possible interactions are difficult to describe
** Complex system, a system composed of many components which may interact with each ...
valued function, and let
the comparison function, be a real valued function. Let both functions be defined on some
unbounded subset
In mathematics, a Set (mathematics), set ''A'' is a subset of a set ''B'' if all Element (mathematics), elements of ''A'' are also elements of ''B''; ''B'' is then a superset of ''A''. It is possible for ''A'' and ''B'' to be equal; if they a ...
of the positive
real number
In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
s, and
be non-zero (often, but not necessarily, strictly positive) for all large enough values of
One writes
and it is read "
is big O of
" or more often "
is of the order of
" if the
absolute value
In mathematics, the absolute value or modulus of a real number x, is the non-negative value without regard to its sign. Namely, , x, =x if x is a positive number, and , x, =-x if x is negative (in which case negating x makes -x positive), ...
of
is at most a positive constant multiple of the absolute value of
for all sufficiently large values of
That is,
if there exists a positive real number
and a real number
such that
In many contexts, the assumption that we are interested in the growth rate as the variable
goes to infinity or to zero is left unstated, and one writes more simply that
The notation can also be used to describe the behavior of
near some real number
(often,
): we say
if there exist positive numbers
and
such that for all defined
with
As
is non-zero for adequately large (or small) values of
both of these definitions can be unified using the
limit superior:
if
And in both of these definitions the
limit point
In mathematics, a limit point, accumulation point, or cluster point of a set S in a topological space X is a point x that can be "approximated" by points of S in the sense that every neighbourhood of x contains a point of S other than x itself. A ...
(whether
or not) is a
cluster point of the domains of
and
i. e., in every neighbourhood of
there have to be infinitely many points in common. Moreover, as pointed out in the article about the
limit inferior and limit superior
In mathematics, the limit inferior and limit superior of a sequence can be thought of as limiting (that is, eventual and extreme) bounds on the sequence. They can be thought of in a similar fashion for a function (see limit of a function). For ...
, the
(at least on the
extended real number line) always exists.
In computer science, a slightly more restrictive definition is common:
and
are both required to be functions from some unbounded subset of the
positive integers
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positiv ...
to the nonnegative real numbers; then
if there exist positive integer numbers
and
such that
for all
Example
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:
*If
is a sum of several terms, if there is one with largest growth rate, it can be kept, and all others omitted.
*If
is a product of several factors, any constants (factors in the product that do not depend on
) can be omitted.
For example, let
, and suppose we wish to simplify this function, using
notation, to describe its growth rate as
. 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
Use
Big O notation has two main areas of application:
* In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, it is commonly used to describe
how closely a finite series approximates a given function, especially in the case of a truncated
Taylor series or
asymptotic expansion
In mathematics, an asymptotic expansion, asymptotic series or Poincaré expansion (after Henri Poincaré) is a formal series of functions which has the property that truncating the series after a finite number of terms provides an approximation ...
.
* In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, it is useful in the
analysis of algorithms.
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:
*
infinite asymptotics
*
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
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
coefficient
In mathematics, a coefficient is a Factor (arithmetic), multiplicative factor involved in some Summand, term of a polynomial, a series (mathematics), series, or any other type of expression (mathematics), expression. It may be a Dimensionless qu ...
s 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
:
or
:
and say that the algorithm has ''order of '' time complexity. The sign "" is not meant to express "is equal to" in its normal mathematical sense, but rather a more colloquial "is", so the second expression is sometimes considered more accurate (see the "
Equals sign
The equals sign (British English) or equal sign (American English), also known as the equality sign, is the mathematical symbol , which is used to indicate equality. In an equation it is placed between two expressions that have the same valu ...
" discussion below) while the first is considered by some as an
abuse of notation
In mathematics, abuse of notation occurs when an author uses a mathematical notation in a way that is not entirely formally correct, but which might help simplify the exposition or suggest the correct intuition (while possibly minimizing errors an ...
.
Infinitesimal asymptotics
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:
The middle expression (the one with
) means the absolute-value of the error
is at most some constant times
when
is close enough to
.
Properties
If the function can be written as a finite sum of other functions, then the fastest growing one determines the order of . For example,
:
In particular, if a function may be bounded by a polynomial in , then as tends to ''infinity'', one may disregard ''lower-order'' terms of the polynomial. The sets and are very different. If is greater than one, then the latter grows much faster. A function that grows faster than for any is called ''superpolynomial''. One that grows more slowly than any exponential function of the form is called ''subexponential''. An algorithm can require time that is both superpolynomial and subexponential; examples of this include the fastest known algorithms for
integer factorization
In mathematics, integer factorization is the decomposition of a positive integer into a product of integers. Every positive integer greater than 1 is either the product of two or more integer factors greater than 1, in which case it is a comp ...
and the function .
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 .
Product
:
:
Sum
If
and
then
. It follows that if
and
then
.
Multiplication by a constant
Let be a nonzero constant. Then
. In other words, if
, then
Multiple variables
Big ''O'' (and little o, Ω, etc.) can also be used with multiple variables. To define big ''O'' formally for multiple variables, suppose
and
are two functions defined on some subset of
. We say
:
if and only if there exist constants
and
such that
for all
with
for some
Equivalently, the condition that
for some
can be written
, where
denotes the
Chebyshev norm. For example, the statement
:
asserts that there exist constants ''C'' and ''M'' such that
:
whenever either
or
holds. This definition allows all of the coordinates of
to increase to infinity. In particular, the statement
:
(i.e.,
) is quite different from
:
(i.e.,
).
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
and
, then
if we restrict
and
to