maths.free › Discrete Math & Logic › Discrete structures › Big O notation
Big O notation
Big O notation is a mathematical notation that describes the approximate size of a function on a domain.
Big O notation
Big O notation is a mathematical notation that describes the approximate size of a function on a domain. Big O is a member of a family of notations invented by the German mathematicians Paul Bachmann and Edmund Landau and expanded by others, collectively called Bachmann-Landau notation. The letter O stands for Ordnung, that is, the order of approximation.
In computer science, big O notation is used to classify algorithms by how their run time or space requirements grow with the input. In analytic number theory, big O notation expresses bounds on the growth of an arithmetical function, as for the remainder term in the prime number theorem. In mathematical analysis, including calculus, Big O notation bounds the error when truncating a power series and expresses the quality of approximation of a real or complex valued function by a simpler function.
Often, big O notation characterizes functions according to their growth rates as the variable becomes large: 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 only provides an upper bound on the growth rate of the function.
Associated with big O notation are several related notations, using the symbols \(o\), \(\sim\), \(\Omega\), \(\ll\), \(\gg\), \(\asymp\), \(\omega\), and \(\Theta\) to describe other kinds of bounds on growth rates.
Bachmann proposed the notation in 1894 and Landau extended it in 1909. An earlier notation was proposed by Paul du Bois-Reymond in 1870.
Formal definition
Let \(f,\) the function to be estimated, be either a real or complex valued function defined on a domain \(D,\) and let \(g,\) the comparison function, be a non-negative real valued function defined on the same set \(D.\) Common choices for the domain are intervals of real numbers, bounded or unbounded, the set of positive integers, the set of complex numbers and tuples of real/complex numbers. With the domain written explicitly or understood implicitly, one writes
\[f(x) = O\bigl(g(x)\bigr)\]
which is read as "\(f(x)\) is big \(O\) of \(g(x)\)" if there exists a positive real number \(M\) such that
\[\left| f(x) \right|\le M\ g(x) \qquad ~ \mathsf{\ for\ all\ } ~ \quad x \in D.\]
If \(g(x) > 0\) (i.e. g is also never zero) throughout the domain \(D,\) an equivalent definition is that the ratio \(\frac{f(x)}{g(x)}\) is bounded, i.e. there is a positive real number \(M\) so that \(\Big|\frac{f(x)}{g(x)}\Big| \le M\) for all \(x \in D.\) These encompass all the uses of big \(O\) in computer science and mathematics, including its use where the domain is finite, infinite, real, complex, single variate, or multivariate. In most applications, one chooses the function \(g(x)\) appearing within the argument of \(O\bigl( \cdot \bigr)\) to be as simple a form as possible, omitting constant factors and lower order terms. The number \(M\) is called the implied constant because it is normally not specified. When using big \(O\) notation, what matters is that some finite \(M\) exists, not its specific value. This simplifies the presentation of many analytic inequalities.
For functions defined on positive real numbers or positive integers, a more restrictive and somewhat conflicting definition is still in common use, especially in computer science. When restricted to functions which are eventually positive, the notation
\[f(x) =O\bigl(g(x)\bigr) \qquad ~ \mathsf{ as } \quad x \to \infty\]
means that for some real number \(a,\) \(f(x) = O\bigl(g(x)\bigr)\) in the domain \(\left[a,\infty\right).\) Here, the expression \(x \to \infty\) does not indicate a limit, but the notion that the inequality holds for large enough \(x.\) The expression \(x \to \infty\) often is omitted.
Similarly, for a real number \(a,\) the notation
\[f(x) = O\bigl(g(x)\bigr) \qquad ~ \text{ as } \ x \to a\]
means that for some constant \(c > 0,\) \(f(x) = O\bigl(g(x)\bigr)\) on the interval \(\left[a-c, a+c\right];\) that is, in a small neighborhood of \(a.\) In addition, the notation \[\ f(x) = h(x) + O\bigl(g(x)\bigr)\] means \(f(x) - h(x)= O\bigl(g(x)\bigr).\) More complicated expressions are also possible.
\[\ f \ll g \iff f = O\bigl(g\bigr).\]
Condensed: the full section is in Wikipedia.
Set version of big O
In computer science it is common to define big \(O\) as also defining a set of functions. With the positive (or non-negative) function \(g(x)\) specified, one interprets \(O\bigl(g(x)\bigr)\) as representing the set of all functions \(\tilde f\) that satisfy \(\tilde f(x) = O\bigl(g(x)\bigr).\) One can then equivalently write \(f(x) \in O\bigl(g(x)\bigr),\) read as "the function \(\ f(x)\) is among the set of all functions of order at most \(g(x).\)"
Examples with an infinite domain
In typical usage the \(O\) notation is applied to an infinite interval of real numbers \([a,\infty)\) and captures the behavior of the function for very large \(x\). 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 \(f(x)\) is a sum of several terms, if there is one with largest growth rate, it can be kept, and all others omitted.
- If \(f(x)\) is a product of several factors, any constants (factors in the product that do not depend on \(x\)) can be omitted.
For example, let \(f(x)=6x^4-2x^3+5\), and suppose we wish to simplify this function, using \(O\) notation, to describe its growth rate for large \(x\). This function is the sum of three terms: \(6x^4\), \(-2x^3\), and \(5\). Of these three terms, the one with the highest growth rate is the one with the largest exponent as a function of \(x\), namely \(6x^4\). Now one may apply the second rule: \(6x^4\)is a product of \(6\) and \(x^4\) in which the first factor does not depend on \(x\). Omitting this factor results in the simplified form \(x^4\). Thus, we say that \(f(x)\) is a "big O" of \(x^4\). Mathematically, we can write \(f(x)=O(x^4)\) for all \(x\ge 1\). One may confirm this calculation using the formal definition: let \(f(x)=6x^4-2x^3+5\) and \(g(x)=x^4\). Applying the formal definition from above, the statement that \(f(x)=O(x^4)\) is equivalent to its expansion, \[|f(x)| \le M x^4\] for some suitable choice of a positive real number \(M\) and for all \(x \ge 1\). To prove this, let \(M=13\). Then, for all \(x\ge 1\): \[\begin{aligned} |6x^4 - 2x^3 + 5| &\le 6x^4 + |-2x^3| + 5\\ &\le 6x^4 + 2x^4 + 5x^4\\ &= 13x^4 \end{aligned}\] so \[|6x^4 - 2x^3 + 5| \le 13 x^4 .\] While it is also true, by the same argument, that \(f(x)=O(x^{10})\), this is a less precise approximation of the function \(f\). On the other hand, the statement \(f(x)=O(x^3)\) is false, because the term \(6x^4\) causes \(f(x)/x^3\) to be unbounded.
When a function \(T(n)\) describes the number of steps required in an algorithm with input \(n\), an expression such as \[T(n)=O(n^2)\] with the implied domain being the set of positive integers, may be interpreted as saying that the algorithm has at most the order of \(n^2\) time complexity.
Example with a finite domain
Big O can also be used to describe the error term in an approximation to a mathematical function on a finite interval. 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 \(x\) is small: \[\begin{aligned} e^x &= 1 + x + \frac{\; x^2\ } {2! }+\frac{\; x^3\ }{3!}+\frac{\; x^4\ }{4!} + \dotsb && \text{ for all finite } x\\[4pt] &= 1 + x + \frac{\; x^2\ }{ 2 }+O(|x|^3) && \text{ for all } |x|\le 1 \\[4pt] &= 1 + x + O(x^2) && \text{ for all } |x|\le 1. \end{aligned}\] The middle expression (the line with "\(O(|x^3|)\)") means the absolute-value of the error \(\ e^x- (1 + x + \frac{\; x^2\ }{2} )\) is at most some constant times \(~ |x^3|\) when \(\ x ~\) is small. This is an example of the use of Taylor's theorem.
The behavior of a given function may be very different on finite domains than on infinite domains, for example, \[(x+1)^8 = x^8 + O(x^7) \quad \text{ for } x\ge 1\] while \[(x+1)^8 = 1 + 8x + O(x^2) \quad \text{ for } |x|\le 1.\]
Multivariate examples
\[x \sin y = O(x) \quad \text{ for }x\ge 1,y\text{ any real number}\]
\[3a^2+7ab+2b^2+a+3b+14 \ll a^2+b^2 \ll a^2 \quad \text{ for all } a\ge b\ge 1\]
\[\frac{xy}{x^2+y^2} = O(1) \quad \text{ for all real } x,y \text{ that are not both } 0\]
\[x^{it} = O(1) \quad \text{ for } x\ne 0,t\in \mathbb{R}.\]
Here we have a complex variable function of two variables. In general, any bounded function is \(O(1)\).
\[(x+y)^{10} = O(x^{10}) \quad \text{ for }x\ge 1, -2\le y\le 2.\]
The last example illustrates a mixing of finite and infinite domains on the different variables.
Condensed: the full section is in Wikipedia.
Transitive property
If \(f=O(g)\) and \(g=O(h)\) then \(f=O(h)\).
If the function \(f\) of a positive integer \(n\) can be written as a finite sum of other functions, then the fastest growing one determines the order of \(f(n)\). For example,
\(f(n) = 9 \log n + 5 (\log n)^4 + 3n^2 + 2n^3 = O(n^3) \qquad\text{for } n\ge 1 .\)
Some general rules about growth toward infinity; the 2nd and 3rd property below can be proved rigorously using L'Hôpital's rule:
Powers dominate logarithms
For any positive \(a,b,\) \[(\log n)^a = O_{a,b}(n^b),\] no matter how large \(a\) is and how small \(b\) is. Here, the implied constant depends on both \(a\) and \(b\).
Exponentials dominate powers
For any positive \(a,b,\) \[n^a = O_{a,b}(e^{bn}),\] no matter how large \(a\) is and how small \(b\) is.
A function that grows faster than \(n^c\) for any \(c\) is called superpolynomial. One that grows more slowly than any exponential function of the form \(c^n\) with \(c>1\) 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 and the function \(n^{\log n}\).
We may ignore any powers of \(n\) inside of the logarithms. For any positive \(c\), the notation \(O(\log n)\) means exactly the same thing as \(O(\log (n^c))\), since \(\log(n^c)=c\log n\). Similarly, logs with different constant bases are equivalent with respect to Big O notation. On the other hand, exponentials with different bases are not of the same order. For example, \(2^n\) and \(3^n\) are not of the same order.
More complicated expressions
In more complicated usage, \(O(\cdot)\) can appear in different places in an equation, even several times on each side. For example, the following are true for \(n\) a positive integer: \[\begin{aligned} (n+1)^2 & = n^2 + O(n), \\ (n + O(n^{1/2})) \cdot (n + O(\log n))^2 & = n^3 + O(n^{5/2}), \\ n^{O(1)} & = O(e^n). \end{aligned}\] The meaning of such statements is as follows: for any functions which satisfy each \(O(\cdot)\) on the left side, there are some functions satisfying each \(O(\cdot)\) on the right side, such that substituting all these functions into the equation makes the two sides equal. For example, the third equation above means: "For any function satisfying \(f(n)=O(1)\), there is some function \(g(n)=O(e^n)\) such that \(n^{f(n)}=g(n)\)". The implied constant in the statement "\(g(n)=O(e^n)\)" may depend on the implied constant in the expression "\(f(n)=O(1)\)".
Some further examples: \[\begin{aligned} f=O(g)\; &\Rightarrow\; \int_a^b f = O\bigg( \int_a^b g \bigg) \\ f(x)=g(x)+O(1)\; &\Rightarrow\; e^{f(x)}=O(e^{g(x)}) \\ (1+O(1/x))^{O(x)} &= O(1) \quad \text{ for } x>0\\ \sin x &= O(|x|) \quad \text{ for all real } x. \end{aligned}\]
Vinogradov's ≫ and Knuth's big Ω
When \(f, g\) are both positive functions, Vinogradov introduced the notation \(f(x) \gg g(x)\), which means the same as \(g(x) = O(f(x))\). Vinogradov's two notations enjoy visual symmetry, as for positive functions \(f,g\), we have \[f(x) \ll g(x) \Longleftrightarrow g(x) \gg f(x).\]
In 1976, Donald Knuth defined
\(f(x)=\Omega(g(x))\Longleftrightarrow g(x)=O(f(x))\)
which has the same meaning as Vinogradov's \(f(x) \gg g(x)\).
However, much earlier, Hardy and Littlewood had defined \(\Omega\) differently, and their notation enjoys widespread use today in analytic number theory. Justifying his use of the \(\Omega\)-symbol to describe a stronger property, Knuth wrote: "For all the applications I have seen so far in computer science, a stronger requirement ... is much more appropriate". Knuth further wrote, "Although I have changed Hardy and Littlewood's definition of \(\Omega\), I feel justified in doing so because their definition is by no means in wide use, and because there are other ways to say what they want to say in the comparatively rare cases when their definition applies." Knuth's big \(\Omega\) enjoys widespread use today in computer science and combinatorics.
Hardy's ≍ and Knuth's big Θ
In analytic number theory, the notation \(f(x) \asymp g(x)\) means both \(f(x)=O(g(x))\) and \(g(x)=O(f(x))\). This notation is originally due to Hardy. Knuth's notation for the same notion is \(f(x) = \Theta(g(x))\). Roughly speaking, these statements assert that \(f(x)\) and \(g(x)\) have the same order. These notations mean that there are positive constants \(M,N\) so that \[N g(x) \le f(x) \le M g(x)\] for all \(x\) in the common domain of \(f,g\). When the functions are defined on the positive integers or positive real numbers, as with big O, writers oftentimes interpret statements \(f(x) = \Omega(g(x))\) and \(f(x)=\Theta(g(x))\) as holding for all sufficiently large \(x\), that is, for all \(x\) beyond some point \(x_0\). Sometimes this is indicated by appending \(x\to\infty\) to the statement. For example, \[2n^2 - 10n = \Theta(n^2)\] is true for the domain \(n\ge 6\) but false if the domain is all positive integers, since the function is zero at \(n=5\).
Orders of common functions
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.
The statement \(f(n) = O(n!)\) is sometimes weakened to \(f(n) = O\left(n^n\right)\) to derive simpler formulas for asymptotic complexity. In many of these examples, the running time is actually \(\Theta(g(n))\), which conveys more precision.
Little-o notation
For real or complex-valued functions of a real variable \(x\) with \(g(x)>0\) for sufficiently large \(x\), one writes
\(f(x) = o(g(x)) \quad \text{ as } x \to \infty\)
if \[\lim_{x\to\infty} \frac{f(x)}{g(x)} = 0.\] That is, for every positive constant ε there exists a constant \(x_0\) such that
\(|f(x)| \leq \varepsilon g(x) \quad \text{ for all } x \geq x_0.\)
Intuitively, this means that \(g(x)\) grows much faster than \(f(x)\), or equivalently \(f(x)\) grows much slower than \(g(x)\). For example, one has
\(200x = o(x^2)\) and \(1/x = o(1),\) both as \(x \to \infty .\)
When one is interested in the behavior of a function for large values of \(x\), 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\) on some interval \([a,\infty)\), but not every function that is big-O of \(g\) is little-o of \(g\). For example, \(2x^2 = O(x^2)\) but \(2x^2 \neq o(x^2)\) for \(x\ge 1\).
Little-o respects a number of arithmetic operations. For example,
if \(c\) is a nonzero constant and \(f = o(g)\) then \(c \cdot f = o(g)\), and
if \(f = o(F)\) and \(g = o(G)\) then \(f \cdot g = o(F \cdot G).\)
if \(f = o(F)\) and \(g = o(G)\) then \(f+g=o(F+G)\)
It also satisfies a transitivity relation:
if \(f = o(g)\) and \(g = o(h)\) then \(f = o(h).\)
Little-o can also be generalized to the finite case: \(f(x) = o(g(x)) \quad \text{ as } x \to x_0\) if \[\lim_{x\to x_0} \frac{f(x)}{g(x)} = 0.\] In other words, \(f(x) = \alpha(x)g(x)\) for some \(\alpha(x)\) with \(\lim_{x\to x_0} \alpha(x) = 0\).
Condensed: the full section is in Wikipedia.
Zdaj pa ti. Noben kalkulator ne poravna tega, vendar kosov tega je kompakten. Poskusite enega spodaj, ali vpišite svoje.
Brezplačen račun dodaja opombe o vsaki lekciji, zapis o tem, kar ste končali, vaše rešene težave na enem mestu, in tutor lahko vprašate o tej strani. Sama matematika je odprta za vse, prijavljena ali ne.
Registriraj se PrijavaSimboli, ki se uporabljajo tukaj
Tapnite vsak simbol za popolno opredelitev, sliko in kaj pomeni vsaka črka v njej.
Vprašanja, ki jih ljudje sprašujejo
What makes mathematics "discrete"?
It deals with separate, countable objects (integers, graphs, statements) rather than continuous quantities. No limits, no infinitesimals; instead induction, counting and logic.
How does a proof by induction work?
Show the statement for the first case, then show that whenever it holds for n it holds for n + 1. Like dominoes: the first falls, and each knocks over the next.
Deli te strani so prilagojeni od Wikipedia (CC BY-SA 4.0). Tu je zbrano in razjasnjeno. Napake so naše.
Več v Discrete Math & Logic
Truth tablesSums and inductionProof by inductionAlgorithms and growth of functions