Algorithm 如何用回溯法求图着色的时间复杂度?
我必须用回溯法找出图着色问题的时间复杂性。我发现它是O(n*m^n),其中n=没有顶点,m=颜色数 假设下面给出了我的代码,如何找到时间复杂度Algorithm 如何用回溯法求图着色的时间复杂度?,algorithm,graph,colors,backtracking,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; ret
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");
}
bool-isSafe(int-v,bool-graph[v][v],int-color[],int-c)
{
对于(int i=0;i 对于(int c=1;c,graphutil方法本身将执行n次。它在c循环中,c上升到m。
现在,由于递归(即m^n),c循环进行了n次,递归进行了n次,因此总计为O(nm^n)