This is my fifth question in an attempt to figure out what $f(n) \in O\left(n^2\right)$ actually means. I'll try to make it as simple as possible.
Facts I'm already aware of:
- Definition of Big O.
- If we have two functions from different Big O sets, we can figure out which one is going to eventually be bigger.
What we're given (in my question):
- We're given some algorithm that takes $f(n)$ steps in the worst case. And $f(n) \in O\left(n^2\right)$.
Preliminaries:
- I showed visually in What does the word "scalability" mean in terms of Big O? that you never know how exactly function $f(n)$ scales in the sense that the ratio $\frac{f(2n)}{f(n)}$ can differ significantly from $\frac{\left(2n\right)^2}{n^2}$ for any value of $n$ (no matter how large $n$ is) despite the fact that $f(n) \in O\left(n^2\right)$.
- In simple words it just means that the statement: "Double the input size $\implies$ quadruple the runtime" can't be taken on faith just from knowing that $f(n) \in O\left(n^2\right)$.
We often encounter statements like these:
- Function $f(n) \in O\left(n^2\right)$ grows / scales quadratically.
- For function $f(n) \in O\left(n^2\right)$ we know that if we double its input size, then its runtime is going to quadruple (assuming $n$ is large enough).
Question:
We already showed that you can never know how your algorithm's runtime will increase if you double (or just change somehow) the input size. Then what do statements like these mean? Are they referring to the function $f(n)$ itself or maybe to its upper bound? Here's what I mean:
- Do they (statements) assume that $f(n) = kn^2$ for some value of $k$ and then talk about how $f(n) = kn^2$ scales? Meaning, instead of finding the ratio of $\frac{f(2n)}{f(n)}$ you can just find the ratio $\frac{\left(2n\right)^2}{n^2} = 4$ and then you can say: "Double the input size of your algorithm $\implies$ quadruple the runtime". But then it's just an approximation and, moreover, it can be very wrong, since we know nothing about $f(n)$ itself except for the fact that $f(n) \in O\left(n^2\right)$. If that's the case, then why do people trust this approximation if they don't even know how wrong or right it is?
- My second guess (that might be useless though) is that statements like these refer to the upper bound itself. I mean, since we know that $f(n) \in O\left(n^2\right)$, then we know that eventually $f(n) \leqslant Cn^2$ for some constant $C$. From here we can talk about how the upper bound $Cn^2$ scales. If that's the case, then I don't understand why it even makes sense to talk about how the upper bound scales?
I'm not asking why people write such statements. I'm just asking what they mean exactly. Though I admit some of these statements might be wrong. Whatever. I just want to understand it right. Thank you in advance!
In my limited experience in competitive programming, people often write $O(n^2)$ even if they actually know that the runtime is $f(n) \sim Cn^2$ for some $C > 0$, where $g(n) \sim h(n)$ means $\lim_{n \to \infty}\frac{g(n)}{h(n)} = 1$. In this case, $f(2n) \sim 4Cn^2$ so $\frac{f(2n)}{f(n)} \sim 4$. But you are right that if you interpret $O$ literally, then the bound might not be sharp enough to conclude that $\frac{f(2n)}{f(n)} \sim 4$.