I understand how to find the time complexity of an algorithm when I’ve been presented with an algorithm, but I can’t seem to get my head around how to work it out when I’ve been given the number of times the algorithm is executed, and the time taken.
I can sometimes get it, when it’s obvious things like O(n), O(n) or O(n^2) but take this question for example:
An algorithm runs a given input of size n.
If n is 4096 the run time is 512 milliseconds.
If n is 16384 the run time is 1024 milliseconds.
If n is 36864 the run time is 1536 milliseconds.
What is the time complexity?
I see that as n * 2, t * 1.5, but I’m not quite sure how to work it out.
Thank you for your help 🙂
I would say you need more than just three datapoints for this kind of question, because of complexities in the system rather than just the algorithm.
What I would do is compare the iterations and the elapsed time and see if you can find a pattern that matches one of the standard time complexities:
Let’s go through your problem:
When n goes up by a factor of 4 (from 4096 to 16384), the time goes up by a factor of 2 (from 512 to 1024 ms).
When n goes up by a factor of 9 (from 4096 to 36864), the time goes up by a factor of 3 (from 512 to 1536 ms).
A function that matches this is f(n) = n^(1/2). When n goes up by a factor of 4, f(n) goes up by a factor of sqrt(4), etc…
So this is of order O(n^.5), which is polynomial
TLDR: Graph it out and match it to a common function for time-complexity. In the real-world you would probably need more than three datapoints.
edit: I’d like to add that that this should really be more complex. There is likely a constant term in every kind of time complexity. I.e. O(n^c) is more likely O(n^c + K) where K is a constant. We ignore the constant for simplicity when writing this out, but it would show up in your graph.