Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate running time using samples

Suppose that you time a program as a function of N and produce the following table:

        N   seconds
-------------------
     4096      0.00
    16384      0.01
    65536      0.06    
   262144      0.51   
  1048576      4.41   
  4194304     38.10  
 16777216    329.13  
 67108864   2842.87

Estimate the order of growth of the running time as a function of N. Assume that the running time obeys a power law T(N) ~ a N^b.

like image 201
Mohamed Naguib Avatar asked Sep 15 '26 22:09

Mohamed Naguib


2 Answers

Your N's are all consecutive powers of 4. Taking 4-based logarithm of consecutive ratios of times you'll see they converge to some constant which is known as 'b'. When you substitute N, T(N) and b from last entry of your table to power law (T(N) = a * N ^ b), you'll get 'a'. In your case log4 of times ratios converges to 1.555, so that's 'b'.

I guess you're taking Coursera's "Algorithms, Part I' course (as I do). Then, this thread must be available for you:

https://class.coursera.org/algs4partI-002/forum/thread?thread_id=149

Or, you may refer to "Analysis of Algorithms" slides beginning from page 16.

like image 77
Rail Suleymanov Avatar answered Sep 19 '26 09:09

Rail Suleymanov


You need to to use a logarithmic graph (logN), and then take the slope of the line. That will indicate the exponent b.

like image 30
Srikant Krishna Avatar answered Sep 19 '26 10:09

Srikant Krishna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!