0

I'm trying to figure out if:

  1. $\log^2n = O(n)$ and
  2. $ n = O(\log^2n)$

are true or if one or both are false.

So far I've concluded that both are false because if $n = 8$ for the first one, then $\log^2 8 = O(8)$ which is false since it simplifies to $9 = O(8)$ which does not belong to $O(n)$.

For the second one, I believe it to be false as well because if $n = 1024$ or (some other big number), you get $1024 = O(\log^2 1024)$ which simplifies to $1024 = O(100)$. And 1024 does not belong to $O(100)$.

Am I right or is one of these true? Thanks.

David Richerby
  • 82,470
  • 26
  • 145
  • 239
David Velasquez
  • 185
  • 1
  • 1
  • 4

1 Answers1

6

You're right about the second one, but your reasoning for both is sketchy. The definition of $f(n)=O(g(n))$ is that there exist constants $c,N$ both greater than zero such that $f(n)\le c\cdot g(n)$ for all $n\ge N$.

Trying a single value for $n$ won't generally give anything in the way of an answer. Consider, though, the intuition we get from trying $n=4, 8, 16, 32, \dotsc$: $$\begin{array}{lcccc} n: & 4 & 8 & 16 & 32 & 64 & 128\\ \log^2 n: & 4 & 9 & 16 & 25 & 36 & 49 \end{array}$$

It seems to be the case that $n$ grows much faster than $\log^2n$. In fact, for this example we have that at least for powers of 2, $\log^2(n)=\log^2(2^k)=k^2\le 1\cdot2^k=1\cdot n$ for all $n \ge 16$, so we might suspect that $\log^2n=O(n)$, which is indeed the case, as it happens.

What is most helpful is to get an intuition of the big-O relationship between frequently-used functions. Here are some examples (all of which should be proved, but that'll be up to you do do or look up). Assuming everything in sight is positive, then:

  1. $\log n = O(n^k)$ for any $k>0$. (polynomials beat logs, eventually)
  2. If $a \le b$, then $n^a=O(n^b)$. (polynomials behave as expected, by degree)
  3. If $a>1$, then $n^k=O(a^n)$. (exponentials beat polynomials)

and so on. The point here is that it's far better to develop your intuition and only use the definition of big-O when faced with a problem where intuition doesn't give you any help. That's what the experts do.

Rick Decker
  • 15,016
  • 5
  • 43
  • 54