An algorithm having worst-case running time of O(N^2) took 30secs to run for input size N=20. How long will the same algorithm take for input size N=400 ?
+3
A:
O(n^2) implies proportionality to the square of n (see this guide). So
T = K (n^2)
30 = K (20^2)
K = 30 / 400
Hence time for 400 items
= (30 / 400)( 400 ^ 2 )
So that 12000 seconds.
Now, that's not necessarily true unless you know that the original 20 item test was a worst case scenario, if it isn't then we have a bad estimate of K. Even if if we have a good estimate of K so we we know the worst case scenarion for 400 items, we don't know that these 400 items will take that long.
djna
2010-07-24 15:20:22
Thanks, I guess it just helps establish an upper bound on the running time.
Shailesh Tainwala
2010-07-24 15:31:02
Note my point about the original "20 takes 30 seconds" datum. Unless there is some reason to know that this *is* a worst case input set you have not actually established an upper bound.
djna
2010-07-24 15:34:49
Ahh, didn't think about that. Thanks, I've got some clarity now, it was easy for me to miss that point.
Shailesh Tainwala
2010-07-25 05:35:21