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!