Can someone tell me what the rules are for moving log or exponents into the $O(n)$ notation so it is still correct?
For example: Is this $\log(O(n))= O(\log(n))$ correct? Or is this correct $O(n)^2=O(n^2)$? Or am I not allowed to do this?
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityCan someone tell me what the rules are for moving log or exponents into the $O(n)$ notation so it is still correct?
For example: Is this $\log(O(n))= O(\log(n))$ correct? Or is this correct $O(n)^2=O(n^2)$? Or am I not allowed to do this?
To prove or disprove this kind of equality with $\mathcal{O}$, you need to go back to the definition of $\mathcal{O}$ with inequalities.
For example, let's study the question $\log(\mathcal{O}(n)) = \mathcal{O}(\log n)$:
$f\in \log(\mathcal{O}(n))$ means that there exists a function $g\in\mathcal{O}(n)$ so that $f = \log g$. That means there exists a constant $A>0$ such that:
$$f(n) = \log g(n) \leqslant \log (An) = \log A + \log n\leqslant 2\log n$$
The first inequality holds true because $\log$ is an increasing function. The second inequality holds true for $n$ big enough. We proved that $f \in \mathcal{O}(\log n)$.
Reciprocally, $f\in \mathcal{O}(\log n)$ means that there exists $B>0$ such that $f(n) \leqslant B\log n$. That means: $$2^{f(n)}\leqslant 2^{B\log n} = n^B$$ Therefore, we cannot conclude that $2^f\in \mathcal{O}(n)$, or equivalently that $f\in \log(\mathcal{O}(n))$.
For example, consider $f(n) = 2\log n$. Then clearly, $f\in \mathcal{O}(\log n)$, but $2^{f(n)} = n^2 \notin \mathcal{O}(n)$.
In the general case, we have:
In order for $f(O(n)) \in O(f(n))$ to hold you essentially want $f$ to satisfy $f(cn) \le df(n)$ where $n$ is sufficiently large. Here the inequality must hold for all sufficiently large constants $c$, while $d$ is a constant that can be chosen as a function of $c$ (but not as a function of $n$).
For example $\log cn \le \log c + \log n \le (1+ \log c) \log n$ for $n \ge 2$ and $c \ge 1$, so you can pick $d=(1+ \log c)$.
Also: $(cn)^2 = c^2 n^2$ for any $c \ge 0$ so you can pick $d=c^2$.
Notice that you can't just move any function into the Big-Oh notation. For example $2^{O(n)} \not\in O(2^n)$. Indeed, when $c > 1$, you can always satisfy $2^{cn} > d 2^n$ for any $d$ chosen independently of $n$, by simply considering values of $n$ that are large enough.
Although $\mathcal O(f(n))$ will often be given as a function, e.g. $\mathcal n^2+n=\mathcal O(n^2)$, strictly speaking $\mathcal O(f(n))$ is a set of functions, so the precise statement is $n^2+n \in \mathcal O(n^2)$. The expressions $\mathcal \log(O(f(n))$ and $\mathcal O(f(n))^2$ don't really make sense. You can't do math on $\mathcal O(f(n))$, other than set operations.