**
回溯法C++ 图着色问题
**
问题描述:给定无向连通图G = (V, E),求最小的整数m,用m种颜色对G中的顶点着色,使得任意两个相邻顶点着色不同。
对应算法实现:
int color[n];
int arc[n][n];
void GraphColor (int m) {
int i, k;
for (i = 0; i < n; i++)
color[i] = 0;
k = 0;
while (k >= 0) {
color[k]++;
while (color[k] <= m) {
if (ok(k)) break;
else color[k]++;
}
if (color[k] <= m && k == n - 1) {
for (i = 0; i < n; i++)
cout << color[i] << " ";
return;
}
if (color[k] <= m && k < n - 1)
k += 1;
else
color[k--] = 0;
}
}
int ok(int i) {
for (int j = 0; j < n; j++)
if (arc[i][j] == 1 && color[i] == color[j]) return 0;
return 1;
}
|