algorithm - 如何使用回溯求图着色的时间复杂度?

标签 algorithm graph colors backtracking

我必须使用回溯找出图着色问题的时间复杂度。我发现它是 O(n*m^n) 的地方,其中 n=无顶点,m=颜色数。

假设我的代码如下,如何找到时间复杂度?

bool isSafe (int v, bool graph[V][V], int color[], int c)
{
    for (int i = 0; i < V; i++)
        if (graph[v][i] && c == color[i])
            return false;
    return true;
}

bool graphColoringUtil(bool graph[V][V], int m, int color[], int v)
{
    if (v == V)
        return true;

    for (int c = 1; c <= m; c++)
    {
        if (isSafe(v, graph, color, c))
        {
           color[v] = c;

           if (graphColoringUtil (graph, m, color, v+1) == true)
             return true;

           color[v] = 0;
        }
    }

    return false;
}
bool graphColoring(bool graph[V][V], int m)
{
    int *color = new int[V];
    for (int i = 0; i < V; i++)
       color[i] = 0;

    if (graphColoringUtil(graph, m, color, 0) == false)
    {
      printf("Solution does not exist\n");
      return false;
    }

    printSolution(color);
    return true;
}
void printSolution(int color[])
{
    printf("Solution Exists:"
            " Following are the assigned colors \n");
    for (int i = 0; i < V; i++)
      printf(" %d ", color[i]);
    printf("\n");
} 

最佳答案

graphutil 方法本身会执行 n 次。它在 c 循环中,并且 c 一直到 m 。 现在由于递归(即 m^n),c 循环进行了 n 次,并且递归进行了 n 次,所以总共将是 O(nm^n )

关于algorithm - 如何使用回溯求图着色的时间复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887348/

相关文章:

javascript - 如何改进此算法以仅使用三个循环?

algorithm - 考虑到这些要求,最合适的数据结构是什么?

php - 按值搜索时返回数组的索引

android - 如何在颜色状态列表资源中指定背景颜色?

algorithm - 用于文本聚类的欧氏距离与曼哈顿距离

java - 如何防止在 OrientDB 中的相同顶点之间创建重复边?

algorithm - NP完全?具有特定约束的图的最佳图嵌入

python - 从边缘列表读入后,Networkx 在节点名称前附加 'u'。如何摆脱?

r - 链接颜色与整数ggplot2

css - 仅更改 CSS 中的默认链接颜色