big-O and little-o

2/5/2016

Bringing some consistency to the definitions of Big-O and litte-o

The definitions of big-O and little-o are quite similar, but little-o has an alternative definition in terms of taking a limit. In this post we explore building up a similar definition for big-O. We will be thinking of big-O and little-o from the perspective of asymptotic anaysis in computer science, so we don’t lose anything by assuming all or our functions are positve. Let’s start with the standard definitions of big-O and little-o. We say that, f(n)f(n) is O(g(n))O(g(n)) or (by a slight abuse of notation) that f(n)=O(g(n))f(n) = O(g(n)), if there exist positive constants cc and n0n_0, such that for all nn0n \geq n_0,

f(n)cg(n)f(n) \leq cg(n)

Contrast this with the definition of little-o, f(n)=o(g(n))f(n) = o(g(n))

if for all c>0c > 0, there exist n0n_0, such that for all nn0n \geq n_0,

f(n)cg(n)f(n) \leq cg(n)

The difference is that this inequality must hold for all cc, so we can pick cc to be arbitrarily small (Note that in the definition of little-o, n0n_0 may depend on the choice of cc). An equivalent definition of little-o is, f(n)=o(g(n))f(n) = o(g(n)) if

limnf(n)g(n)=0\lim_{n \rightarrow \infty} \frac{f(n)}{g(n)} = 0

It is easy to see using the definition of limit that these two definitions of little-o are equivalent. This second definition is the one most often used for checking if f(n)f(n) is o(g(n))o(g(n)), since we can apply the tools of calculus, including e.g. l’hospital’s rule, to calculate the limit instead of trying to find an n0n_0 for each cc, which is usually more difficult. Do we have a similar definition for big-O? It may seem at first that the following is an equivalent to the one above, i.e. that a necessary and sufficient condition for f(n)f(n) to be O(g(n))O(g(n)) is,

limnf(n)g(n)<\lim_{n \rightarrow \infty} \frac{f(n)}{g(n)} < \infty

Unfortunately, this is not a valid definition of big-O. The problem is that the limit is not guaranteed to exist. This was not a problem in our definition of little-o, since if the limit is equal 00 it must exit.

Here is an example of the trouble we could run into. Take f(n)f(n) to be the function on the positive integers {0,1,,}\{0,1, \ldots, \} such that:

f(n)={0if n is even,1if n is oddf(n) = \begin{cases} 0 & \text{if $n$ is even,}\\ 1 & \text{if $n$ is odd} \end{cases}

and let g(n)=1g(n)=1. Then the limit as nn \rightarrow \infty of f(n)/g(n)f(n)/g(n) does not exist. The function oscillates between 00 and 11 forever. On the other hand f(n)f(n) is clearly O(g(n))O(g(n)), since

f(n)g(n) for all n0f(n) \leq g(n) \textrm{ for all } n \geq 0

This demonstrates that if the definition of big-O is satisfied it may not be the case that the above limit exists. What about the other direction, i.e. if the limit exists, does this imply big-O, after all, we are usually trying to prove that some function is big-O of some other function. Let’s see, if the limit exists it is finite by definition, and hence equal to some constant cc so we have,

limnf(n)g(n)c\lim_{n \rightarrow \infty} \frac{f(n)}{g(n)} \leq c

This means that there must be a cc and an n0n_0 such that, for all nn0n \geq n_0

f(n)g(n)cf(n)cg(n)\frac{f(n)}{g(n)} \leq c \equiv f(n) \leq cg(n)

Voila, that’s the definition of big-O, so in fact if the limit exits then big-O is implied. That’s good, it means that if we can calculate the limit, then we can prove big-O and if the limit turns out to be zero then we have little-o as well. Still it would be nice to have a definition that worked even when the limit doesn’t exist like in our oscillating function from before.

lim sup\limsup to the rescue

We need something more general than lim\lim, something that always exists. That’s where the lim sup\limsup comes in. Unlike lim\lim, which must always be finite, we allow lim sup\limsup to be ++\infty. OK, so just what is the lim sup\limsup otherwise known as the limit superior. Here is the formal definition,

lim supnh(n)=limnsupjnh(j)\limsup_{n \rightarrow \infty} h(n) = \lim_{n \rightarrow \infty} \sup_{j \geq n} h(j)

Let’s take this piece by piece. First, what does sup\sup mean in supjnh(j)sup_{j \geq n} h(j). Easy, sup\sup means least upper bound. sup\sup is similar to max\max, so supjnh(j)\sup_{j \geq n} h(j) is like the maximum value of h(j)h(j) on the set of all integers jnj \geq n. The reason we can’t use max\max is that it does not always exist, whereas the least upper bound (sup\sup) will always exist even if it is infinite.

For example suppose h(n)h(n) is the function 12n1-2^{-n}, hh has a horizontal asymptote at y=1y=1. This function does not have a maxmax, if you give me any number, say c<1c < 1 I’ll give you an integer jj making h(j)h(j) bigger, c<h(j)<1c < h(j) < 1. On the other hand h(n)h(n) is always less than 11 and there is no smaller number for which this is true, so 11 is the supn0h(n)\sup_{n \geq 0} h(n). Nice, using supsup guarantees we don’t get into trouble with cases where the maximum is not attained.

Now that we understand what the supremum is, let’s replace the limit with it’s definition and see what it means for the lim sup\limsup to be finite. If something is finite it must be equal some constant cc. That is,

limnsupjnh(j)=c<\lim_{n \rightarrow \infty} \sup_{j \geq n} h(j) = c < \infty

is equivalent to the statement that there exists a positive constant n0n_0 such that for all nn0n \geq n_0,

supjnh(j)c\sup_{j \geq n} h(j) \leq c

observe that supjnh(j)csup_{j \geq n} h(j) \leq c is the same as saying that all values of h(j)h(j) on the set of integers {jn}\{j \geq n\} are less than cc — since if the supsup is less than cc all values are. Finally, we arrive at the statement, there exist positive constants n0n_0 and cc such that for all nNn \geq N,

h(n)ch(n) \leq c

I like to think of the lim sup\limsup as saying that h(n)ch(n) \leq c eventually. All we have to do now is replace h(n)h(n) with f(n)/g(n)f(n)/g(n) to arrive at

f(n)g(n)cf(n)cg(n)\frac{f(n)}{g(n)} \leq c \equiv f(n) \leq cg(n)

and we have the definition of big-O, proving that a finite lim sup\limsup implies big-O. The proof in the other direction, i.e. that big-O implies that the lim sup\limsup is finite is pretty much the same proof in reverse. Hence we have shown that the two definitions are equivalent! That is, an alternative definition of f(n)=O(g(n))f(n) = O(g(n)) is

lim supnf(n)g(n)<\limsup_{n \rightarrow \infty} \frac{f(n)}{g(n)} < \infty

The only difference between this result and our naive guess is that we had to use lim sup\limsup instead of lim\lim to handle cases where the limit does not exist. By the way, when the limit exists it is equal to the lim sup\limsup. So we can summarize what we have so far by saying that if the lim sup\limsup of f/gf/g is 0 then ff is o(g)o(g), if it is finite but not zero ff is O(g)O(g).