I have two algorithms one with complexity $O(100)$ and the other with complexity $O(270)$. Can anyone give me a clear explanation of what exactly this means and how they are compared?
-
Check this Wikipedia article: https://en.wikipedia.org/wiki/Big_O_notation – user_194421 Jul 17 '16 at 10:13
1 Answers
Let $k>0$. If $T_A(n)$ is the cost of the algorithm $A$ for an input of size $n$, saying that $A$ is $O(k)$ is saying that $T_A(n)$ is $O(k)$, which means that there are constants $m_A$ and $M_A$ such that $T_A(n)\le kM_A$ whenever $n\ge m_A$.
Suppose that $A$ is $O(k)$, and let
$$M=\max\{kM_A,T_A(0),T_A(1),\ldots,T_A(m_A-1)\}\;;$$
Clearly $T_A(n)\le M$ for $0\le n<m_A$, and for $n\ge m_A$ we know that
$$T_A(n)\le kM_A\le M\;,$$
so $T_A(n)\le M$ for all $n\in\Bbb N$. Conversely, if $T_A(n)\le M$ for all $n\in\Bbb N$, let $m_A=0$ and $M_A=\frac{M}k$; then it’s certainly true that
$$T_A(n)\le M=kM_A$$
for all $n\ge 0=m_A$ and hence that $A$ is $O(k)$. Thus, $A$ is $O(k)$ if and only if there is an $M$ such that $T_A(n)\le M$ for all $n\in\Bbb N$, i.e., if and only if $T_A$ is a bounded function.
Thus, the algorithms $A$ that are $O(100)$ are those for which the function $T_A$ is bounded, and so are the algorithms that are $O(270)$: these are both simply the class of algorithms whose costs are bounded by some amount independent of the size of the input.
(You didn’t specify whether you’re interested in time complexity, space complexity, or something else, but whatever you’re measuring, it’s some kind of cost: execution time, space required, etc.)
- 616,228
-
Assuming space complexity, I want to know if $O(270)$ needs 2.7 times more memory that $O(100)$. I want to know how bad is $O(270)$ compared to $O(100)$ in the worst case. – tom Jul 17 '16 at 10:40
-
@tom: The argument that I gave shows that you cannot conclude anything about them save that for each there is a space bound within which the algorithm will operate no matter how big the input. The numbers $100$ and $270$ tell you no more than that. – Brian M. Scott Jul 17 '16 at 10:42
-
Can I at least conclude that they are almost in the same order of complexity? – tom Jul 17 '16 at 11:00
-
@tom: They are exactly the same order of complexity: $O(1)$. (Or $O(k)$ for any other $k>0$, but $k=1$ is the usual choice for algorithms of bounded complexity.) – Brian M. Scott Jul 17 '16 at 11:03