3

I have an algorithm and I determined the asymptotic worst-case runtime, represented by Landau notation. Let's say $T(n) = O(n^2)$; this is measured in number of operations.

But this is the worst case, how about in average? I tried to run my algorithm 1000 times for each $n$ from $1$ to $1000$.I get another graph which is the average running time against $n$ but measured in real seconds.

Is there any possible way to compare these figures?

Raphael
  • 73,212
  • 30
  • 182
  • 400
geasssos
  • 161
  • 5

1 Answers1

4

You can't, not really, for three reasons:

  1. Number of operations and runtime do not compare well; too many factors (typically) left out in analysis influence actual runtime.
  2. Asymptotic results (which hold in the limit) and a finite sets of observations do not compare.
  3. Landau classes don't compare with "exact" functions. For instance, if you have $f \in O(n^3)$ and $g(n) = 3n^2$, you have no idea which grows faster.

All hope is not lost, though. With some additional work you can get to reliable statements.

  • Perform an (asymptotic) average-case analysis. That is, assume a random distribution on the input set and calculate the expected number of executed operations. That can be tough, but if you succeed you can compare the result to your worst-case asymptotic. (Note that you may need $\Theta$ to make meaningful statements.)

  • If you can, improve your analysis to yield constant factors (not only $\Theta$). That way, you can separate, say, a $2n^2 + 5n$ worst-case from $1.5n^2 - 3 \log n$ average-case.

  • Run the program for worst-case instances (you should know them from your WC-analysis) and compare the real measurements with the average case.

    Note: This can only provide guesses for asymptotic behaviour.

  • Alternatively, run the program average and worst-case instances but count statements. That gives you more reliable measurements and removes platform-dependent artifacts from consideration. Still, you get only guesses for asymptotics.

Bottom line, you have to have the same "kind" of statement for both worst- and average-case if you want to compare them.

Raphael
  • 73,212
  • 30
  • 182
  • 400