友人が彼のアルゴリズムの複雑さを分析するのを手伝おうとしていますが、Big-O 記法についての私の理解はかなり限られています。
コードは次のようになります。
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++;
}
コード内には 2 つの関数がありますが、単純なため複雑さを特定できました。ただし、内側のwhile
ループの複雑さを表現することはできませんでしたが、測定した後でも、これらすべての複雑さをアルゴリズムの計算の複雑さを表す式にまとめるには、まだ助けが必要です。
この件に関して真剣に助けが必要です。ありがとう!