图的m着色问题
1、问题
给定无向连通图G和m种颜色,用这些颜色给图的顶点着色,每个顶点一种颜色。如果要求G的每条边的两个顶点着不同颜色。给出所有可能的着色方案;如果不存在,则回答“NO”。
2、解析
- 深度优先搜索
- 回溯
设G有n个顶点,将顶点编号为1,2,…,n,则搜索空间为深度n的叉完全树,将颜色编号为1,2,…,m,结点<x1,x2,…,xk>(x1,x2,…xk∈{1,…,m},1≤k≤n)表示顶点1的颜色,顶点2的颜色x2,…,顶点k的颜色xk。
-
过程:
- 用图的邻接矩阵a表示无向图连通图G=(V,E)。
若存在相连的边,则a[i][j] = 1,否则 a[i][j]=0.
整数1,2,3…m用来表示为一棵高度为n+1的完全m叉树。
解空间树的第i层中每一结点都有m个儿子,每个儿子相应于x[i]的m个可能的着色之一。
第n+1层为叶子结点。
- 回溯
1、当i>n时,算法搜索至叶节点,得到新的m着色方案,当前找到可m着色的方案树增1.
2、当i<=n时,当前扩展结点Z是解空间中的内部结点。该结点有x[i]=1,2,3…m共m个儿子结点。对当前扩展结点Z的每一个儿子结点,检查其可行性,并以深度优先的方式递归地对可行子树搜索,或剪去不可行子树。
3、设计
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int t = 1000;
int dot,color,side,f,a[t][t],c[t];
int check_ok(int x) {
for (int i=1;i<=dot;i++) {
if (!a[x][i])
continue;
if (c[i] == c[x])
return 0;
}
return 1;
}
void dfs(int p) {
if (p>dot) {
f++;
for (int i=1;i<=dot;i++)
cout<<c[i]<<" ";
cout<<endl;
return;
}
for (int i=1;i<=color;i++) {
c[p] = i;
if (check_ok(p))
dfs(p+1);
c[p] = 0;
}
}
int main() {
cout<<"请输入顶点数、边数和颜色数:"<<endl;
cin>>dot>>side>>color;
int m,n;
cout<<"请输入相连的节点:"<<endl;
for(int i=0;i<side;i++) {
cin>>m>>n;
a[m][n]=a[n][m]=1;
}
dfs(1);
if (f) {
cout<<"共有 "<<f<<" 种"<<endl;
} else {
cout<<"NO"<<endl;
}
return 0;
}
/*
input:
4 5 3
1 2
1 3
1 4
2 3
2 4
output:
1 2 3 3
1 3 2 2
2 1 3 3
2 3 1 1
3 1 2 2
3 2 1 1
共有 6 种
*/
4、分析
-
搜索树有:1+m+m 2 ^2 2+…+m n ^n n&=(m n ^n n + ^+ + 1 ^1 1-1)/(m-1) <=(m n ^n n + ^+ + 1 ^1 1)/(m/2) m≥2个结点2每个节点要和其他所有顶点的颜色进行比较,进行n-1次比较
-
最坏情况下遍历深度为n的m叉完全数,时间复杂度为O(n*m n ^n n)