If $g(n_0)\le cf(n_0)$ for some $(n_0,c)$ then $g(n)\le cf(n)$ for every $n>n_0$, or not?

136 Views Asked by At

I'm currently studying Algorithms Design and Analysis and our teacher today started talking about Asymptotic Analysis. He said that, after choosing an arbitrary $c$, if you manage to find a $n_0$ that satisfies $f(n_0) \ge c*g(n)$, that in turn means that $g(n) \le f(n) \ \forall \ n > n_0$.

He gave this example:

Given $g(n) = n + 34\ $ and $f(n) = n$, is $g(n) = O(f(n))$?

$ n + 34 \le c*n \\ \text {let $c = 2$ } \\ n + 34 \le 2n \\ \text {if $n_0 = 34$, then} \\ \color {green} {68 \le 68} $

Since we got a valid inequation after finding 34, it is therefore true that $$g(n) \le c * f(n) \ \forall \ n > 34$$

That works for that example. However, take this curve, for instance (red is the $c * f(n)$ - the asymptote; blue is $g(n)$):

enter image description here

An almost identical one was even shown in the teacher's presentation. If we were to take the image's $n_0$ as our $n_0$ (as it was shown in the presentation), then the assertion would be correct. However, by using the teacher's method, I could come up with an arbitrary $c$ and find a $n_1$, therefore ending up with a valid pair of $(n_0, c)$ that satisfies the condition $f(n_0) \ge c*g(n)$; obviously, though, it would be wrong to conclude, in that case, that $g(n) \le c * f(n) \ \forall \ n > n_0$.

A similar thing could happen by analyzing a sinusoidal function.

The way I tried to resolve the example he gave was this:

$ n + 34 \le c*n \\ \text {let $c = 2$ } \\ n + 34 \le 2n \\ \color {green} {n \ge 34} $

I'm not really sure though if my solution is any different and if it would work in those cases I mentioned.

Is the teacher correct? If not, is my method any different? What is the correct way of proving a function $g(n)$ is $O(f(n))$?

1

There are 1 best solutions below

4
On

Your example with $n_1$ clearly shows that what your teacher said was wrong (or that you misinterpreted what your teacher said in a way that made it wrong). Establishing that $68\geq68$ cannot possibly establish any property of a function for sufficiently large $n$.

However, your attempt is also incorrect. You've started with the assumption that $f(n)\geq c\,g(n)$ and concluded that $n\geq 34$, which is backwards. You're supposed to be proving that $f(n)\geq c\,g(n)$ is true for all large enough $n$: in other words, you need to prove that "$n$ is big" implies the inequality. What you've done is shown that, if the inequality holds, then $n$ must be big. But that doesn't prove that the inequality holds for all $n$: it just proves that any values of $n$ for which the inequality holds must be at least $34$.