0

I'm trying to help a friend analyze the complexity of his algorithm but my understanding of Big-O notation is quite limited.

The code goes like this:

int SAMPLES = 2000;
int K_SAMPLES = 5000;

int i = 0; // initial index position    
while (i < SAMPLES)
{
    enumerate();                       // Complexity: O(SAMPLES)
    int neighbors = find_neighbors(i); // Complexity: O(1) 

    // Worst case scenario, neighbors is the same number of SAMPLES
    int f = 0;
    while (f < neighbors) // This loop is probably O(SAMPLES) as well.
    {
        int k = 0; // counter variable
        while (k < K_SAMPLES) // Not sure how to express the complexity of this loop.
        {                     // Worst case scenario K_SAMPLES might be bigger than SAMPLES. 

            // do something!

            k++;
        }
        f++;
    }

    i++;
}

There are 2 functions inside the code but I was able to identify their complexity since they are simple. However, I was unable to express the complexity of the inner while loop, but even after it is measured, I still need help to assemble all these complexities into a formula that represents the computational complexity of the algorithm.

I seriously need help on this matter. Thanks!

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 2
    `while (i < neighbors)` won't this loop infinitely? `i` and `neighbors` are not modified. – nhahtdh Mar 05 '13 at 20:50
  • 1
    What are the variables here? Is N the only variable? I'm guessing N represents the set of all neighbors. Do you assume SAMPLES and K_SAMPLES to be fixed? Because then they wont even matter when calculating the big-O. – Daniel Williams Mar 05 '13 at 20:53
  • True. Fixed the issue, thanks @nhahtdh – karlphillip Mar 05 '13 at 20:58
  • 1
    What is `n`? Which of these inputs are variable? You may need to express it in terms of multiple variables if, for example, `SAMPLES` and `K_SAMPLES` vary independently of one another. – Aaron Dufour Mar 05 '13 at 21:42
  • I meant to write `O(SAMPLES)` and not `O(n)`. Thanks for pointing that out! – karlphillip Mar 06 '13 at 04:12
  • The issues were fixed. What do you guys think of the answers so far? – karlphillip Mar 06 '13 at 21:12

2 Answers2

3

Worst case analysis going from inner most loop to outer most (with mild abuse of the "=" sign):

->  O(K_SAMPLES)                    -- complexity of just the k-loop

->  neighbors * O(K_SAMPLES)         -- complexity of f-loop accounted for
 =  SAMPLES * O(K_SAMPLES)          -- since neighbors = SAMPLES in worst case
 =  O(SAMPLES * K_SAMPLES)

->  O(SAMPLES) + O(SAMPLES * K_SAMPLES)  -- adding complexity of enumerate()
 =  O(SAMPLES + SAMPLES * K_SAMPLES)
 =  O(SAMPLES * K_SAMPLES)

The SAMPLES term was dropped since SAMPLES * K_SAMPLES grows asymptotically faster. More formally,

When C >= 2, SAMPLES >= 1, K_SAMPLES >= 1 then

SAMPLES + SAMPLES * K_SAMPLES  <=  C(SAMPLES * K_SAMPLES)
SAMPLES * (K_SAMPLES + 1)  <=  SAMPLES * C * K_SAMPLES
K_SAMPLES + 1  <=  C * K_SAMPLES

For more info on big-O with multiple variables, see here. Continuing on with the last loop we have:

->  SAMPLES * O(SAMPLES * K_SAMPLES)    -- complexity of i-loop accounted for
 =  O(SAMPLES^2 * K_SAMPLES)

Note that depending on the average number returned by find_neighbors(i), it can make the average big-O different.

DPenner1
  • 10,037
  • 5
  • 31
  • 46
  • +1 For abuse. Kidding aside, the answer looks good. I wish somebody else could evaluate it too so I can close the thread. – karlphillip Mar 06 '13 at 13:31
0

O(neighbors * K_SAMPLES)

if neighbors << K then this is closer to linear in K_SAMPLES

If neighbors on order of K_SAMPLES this is quadratic in K_SAMPLES

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560