0

I've calculated the real time in millisecond for my algo. I've plotted a graph comparing the actual time taken by my algorithm in Milli-Seconds(y-axis) to 'n'(x-axis) where n is the number of nodes in the tree I'm working on.

How do I relate this graph to O(log(n)) if my algorithm should ideally have a O(log(n)) complexity.

Identity1
  • 1,139
  • 16
  • 33

2 Answers2

3

Assuming your algorithm is in O(log n) the graphs should make for a nice comparison. But don't plot log n, you need k * log n + c for some constants k and c.

The constant k describes the duration of a single step of your algorithm, whereas c summaries all constant (initialization) cost.

Depending on what you want to achieve and your algorithm / implementation you might see effects like processor cache misses, garbage collection or similar stuff with increasing n.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
1

In case you can save n,log(n),runtime(n):

enter image description here

You can use 3 visualization approaches (I used Excel since it is easy and fast):

  1. Draw a QQ-plot between log(n) and your run time: enter image description here

This figure shows you the difference between the 'Theoretical' run time function and the 'Empirical' run time function. A straight line (or close) implies that they are close.

  1. Draw two plots on the same graph: the horizontal axes is n, and the two functions are log(n) and the run time you obtained for each n:

enter image description here

  1. The third analysis is the statistical approach: plot a graph where the horizontal axis is n, and the vertical axes is runtime(n). Now, add a logarithmic trend line and Rsquare.

enter image description here

The trend line can give you the best a,b where runtime(n)=a*log(n)+b . The correlation between the runtime and log(n) is better as Rsquare gets higher.

AvidLearner
  • 4,123
  • 5
  • 35
  • 48