这是我写的用于查找图的色数的算法。我在弄清楚该算法的时间复杂度时遇到了麻烦。由于我有3个嵌套循环,因此我认为时间复杂度至少为n³。由于我还要遍历每个顶点的边缘,是否会在O(n2ⁿ)周围?这个算法也正确吗?
int ChromaticNumber()
{
for (int i = 0; i < Vertices.size(); ++i)
{
++Vertices[i].Color;
bool Ok = false;
while (!Ok)
{
Ok = true;
for (int j = 0; j < Vertices[i].Edges.size(); ++j)
{
int AdjacentVertex = Vertices[i].Edges[j];
if (Vertices[i].Color == Vertices[AdjacentVertex].Color)
{
++Vertices[i].Color;
Ok = false;
}
}
}
}
int MaxColor = 0;
for (int i = 0; i < Vertices.size(); ++i)
{
if (Vertices[i].Color > MaxColor)
MaxColor = Vertices[i].Color;
}
return MaxColor;
}