3

I'm working on an algorithm and I'm trying to figure out its time complexity given the operations it takes to complete a input set of specific length, I have been testing the algorithm with varying input lengths.

The results shows that every time I double the input length, it takes 4 times more operations than before to complete:

  • 20 items = 1M (M=million)
  • 40 items = 4M
  • 80 items = 16M
  • 160 items = 64M
  • 320 items = 256M
  • 640 items = 1024M

What is the time complexity/running time that fits better with the above results?

Raphael
  • 72,336
  • 29
  • 179
  • 389
Jesus Salas
  • 519
  • 1
  • 4
  • 17
  • What is the time complexity that fits better Better than what? With n the number of items, the number of operations seems to be somewhere between n and two to the power of n. – greybeard Sep 02 '16 at 06:42

2 Answers2

5

Recurrent equation: $$T(n) = 4 \cdot T(\frac{n}{2})$$ $$T(1) = O(1)$$ Its solution: $$T(n) = \Theta(n^2)$$

HEKTO
  • 3,088
  • 15
  • 19
  • Hi @hekto, plotted it and matches perfectly :), thanks! – Jesus Salas Sep 02 '16 at 06:55
  • 1
    This is guesswork. A finite sample can not be used to infer asymptotic properties. – Raphael Sep 02 '16 at 18:34
  • @Raphael, I simplified the question and the data provided, thanks for the heads up – Jesus Salas Sep 02 '16 at 20:09
  • @Jesus Salas - Rafael means that a normal way to analyze an algorithm is to accurately count its operations. Also, algorithms can work differently on different data - so, we normally look for worst case and average case scenarios. – HEKTO Sep 02 '16 at 21:43
  • Just as a clarification. The way to obtain the O(n^2) from the recurrence relation is by applying case 1 of the master theorem. – Mario Cervera Sep 02 '16 at 21:58
  • @HEKTO I understand what Raphael means, (sorry for my short reply to him before, I really appreciate his help), the algorithm has been tested with instances specially crafted to be hard from the processing POV of the algorithm covering the full spectrum of data distributions. The results are consistent with the above figures for any given set, (the results provided here are not the real ones). Besides I wanted to confirm my findings by asking this question, I found very hard to understand CS theory sometimes so I try to simplify my questions as much as possible to try to help others around :) – Jesus Salas Sep 03 '16 at 04:59
  • 1
    @JesusSalas You may be interested in this answer. – Raphael Sep 03 '16 at 09:55
3

Unless you are promised that this pattern continues ad infinitum, you can not conclude anything. Asymptotic properties can not be inferred from finite samples, ever.

Raphael
  • 72,336
  • 29
  • 179
  • 389