Processing math: 100%

Glossary

Algo: Big-O Notation

Figure 1

Saying f=O(g) is a very loose analog of “fg.” It differs from the usual notion of because of the constant c, so that for instance 10n=O(n). This constant also allows us to disregard what happens for small values of n. For example, suppose we are choosing between two algorithms for a particular computational task. One takes f1(n)=n2 steps, while the other takes f2(n)=2n+20 steps (Figure 1). Which is better? Well, this depends on the value of n. For n5, f1 is smaller; thereafter, f2 is the clear winner. In this case, f2 scales much better as n grows, and therefore it is superior.

This superiority is captured by the big-O notation: f2=O(f1), because f2(n)f1n=2n+20n222 or all n; on the other hand, f1O(f2), since the ratio f1(n)/f2(n)=n2/(2n+20) can get arbitrarily large, and so no constant c will make the definition work. Now another algorithm comes along, one that uses f3(n)=n+1 steps. Is this better than f2? Certainly, but only by a constant factor. The discrepancy between f2 and f3 is tiny compared to the huge gap between f1 and f2. In order to stay focused on the big picture, we treat functions as equivalent if they differ only by multiplicative constants.

Returning to the definition of big-O, we see that f2=O(f3): f2(n)f3n=2n+20n+120, and of course f3=O(f2), this time with c=1.

Just as O() is an analog of , we can also define analogs of and = as follows: f=Ω(g) means g=O(f) f=Θ(g) means f=O(g) and f=Ω(g).

In the preceding example, f2=Θ(f3) and f1=Ω(f3).

Big-O notation lets us focus on the big picture. When faced with a complicated function like 3n2+4n+5, we just replace it with O(f(n)), where f(n) is as simple as possible. In this particular example we’d use O(n2), because the quadratic portion of the sum dominates the rest. Here are some commonsense rules that help simplify functions by omitting dominated terms:

  1. Multiplicative constants can be omitted: 14n2 becomes n2.
  2. na dominates nb if a>b: for instance, n2 dominates n.
  3. Any exponential dominates any polynomial: 3n dominates n5 (it even dominates 2n).
  4. Likewise, any polynomial dominates any logarithm: n dominates (logn)3. This also means, for example, that n2 dominates nlogn.

Don’t misunderstand this cavalier attitude toward constants. Programmers and algorithm developers are very interested in constants and would gladly stay up nights in order to make an algorithm run faster by a factor of 2. But understanding algorithms at the basic level would be impossible without the simplicity afforded by big-O notation.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Wikipedia