Algorithm Search⌘K

Definitionintro

Asymptotic notation

Also called big O notation, Landau notation, big Theta, big Omega.

The notations O, Ω and Θ compare the growth of two functions for large inputs and ignore constant factors.

Definition

Let ff and gg be functions from the natural numbers to the non-negative real numbers.

  • f(n)=O(g(n))f(n) = O(g(n)) when there are constants c>0c > 0 and n0n_0 with f(n)≤c⋅g(n)f(n) \le c \cdot g(n) for each n≥n0n \ge n_0. So ff grows at most as fast as gg.
  • f(n)=Ω(g(n))f(n) = \Omega(g(n)) when there are constants c>0c > 0 and n0n_0 with f(n)≥c⋅g(n)f(n) \ge c \cdot g(n) for each n≥n0n \ge n_0. So ff grows at least as fast as gg.
  • f(n)=Θ(g(n))f(n) = \Theta(g(n)) when f(n)=O(g(n))f(n) = O(g(n)) and f(n)=Ω(g(n))f(n) = \Omega(g(n)). So ff and gg grow at the same rate.
  • f(n)=o(g(n))f(n) = o(g(n)) when f(n)/g(n)→0f(n)/g(n) \to 0, and f(n)=ω(g(n))f(n) = \omega(g(n)) when f(n)/g(n)→∞f(n)/g(n) \to \infty.

The sign == here means "is in the set". O(g)O(g) is a set of functions, and f=O(g)f = O(g) means f∈O(g)f \in O(g).

Examples and non-examples

  • 3n2+10n+7=Θ(n2)3n^2 + 10n + 7 = \Theta(n^2). Take c=4c = 4 and n0=12n_0 = 12 for the upper bound, and c=3c = 3 for the lower bound.
  • nlg⁡n=O(n2)n \lg n = O(n^2), but nlg⁡n≠Ω(n2)n \lg n \ne \Omega(n^2).
  • lg⁡(n!)=Θ(nlog⁡n)\lg(n!) = \Theta(n \log n). The base of a logarithm changes only a constant factor, so O(log⁡n)O(\log n) needs no base.
  • 2n+1=O(2n)2^{n+1} = O(2^n), but 22n≠O(2n)2^{2n} \ne O(2^n).
  • Not an example: nsin⁡nn^{\sin n} is not O(1)O(1), and it is not Ω(n1/2)\Omega(n^{1/2}) either. Some functions do not compare.
  • Cost measures. The time of an algorithm is a function of the size of the input. A claim such as "the worst-case time is Θ(nlog⁡n)\Theta(n \log n)" uses the maximum over the inputs of size nn. The comparison model is one way to fix what one step is.
  • Amortized and expected costs use the same notation on an average over a sequence of operations, or over random choices.

References

  1. Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Chapter 3
  2. Donald E. Knuth, Big Omicron and big Omega and big Theta (1976)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proof
  • add a proof in proofs/ (06.5)
Lean
  • name a Lean module in the lean field (07)
Visual
  • add viz/scene.ts or a figure (10)
Writing
  • the lead paragraph