If a run an algorithm I have calculated to be O(n^2) on two different n's, all else constant, can I verify it is O(n^2) by comparing execution times? For example, n1 = 50 and n2 = 100. Since n2 is n1, time2 should be (n2 - n1)^2 times larger than time1, right? Or is verification only possible with a graph?
2 Answers
It depends on meaning of "verify".
If you mean formally verify (i.e. prove) then:
No, but both plotting or just comparing the number can give you confidence that it is O(n^2) - and it is normally easier to prove that something is true if you are already confident what the result should be, and that it is true.
The reasons you cannot prove it that way are e.g.:
- The algorithm may be slow for some inputs, and you are not triggering that. Note that "random" inputs are not ideal for such testing.
- The complexity could be 100*n+n*log(n) - you are likely to see this as O(n) if you plot it for "small" values of n.
- The complexity is normally stated for an ideal computer, whereas you run on an actual computer. Thus when timing you will see a slow-down due to running out of memory caches which shouldn't be part of the ideal complexity.
If you use "verify" to mean "experimentally test" that e.g. an O(n^2) algorithm is in fact O(n^2), then, yes you can do that with both methods.
1 Comment
Can I verify it is O(n^2) by comparing execution times?
No.
Time complexity is pure theoretical concept. You cannot directly establish relation between execution times and time complexity .
To verify if an algorithm is O(n^2), you should be dependent only on the algorithm itself.
(n2/n1)^2, not(n2-n1)^2