SiriBlog

siriyang的个人博客


  • 首页

  • 排行榜

  • 标签115

  • 分类37

  • 归档321

  • 关于

  • 搜索

寻找无向图最小着色方案

发表于 2020-05-24 更新于 2021-10-29 分类于 计算机 , 算法题 , 技术 , C/C++ 阅读次数: Valine:
本文字数: 2.6k 阅读时长 ≈ 2 分钟

题目

(1)图的m可着色判定问题

  给定无向连通图G和m种不同的颜色。用这些颜色为图G的各顶点着色,每个顶点着一种颜色。是否有一种着色法使G中每条边的2个顶点着不同颜色。

(2)图的m可着色优化问题

  若一个图最少需要m种颜色才能使图中每条边连接的2个顶点着不同颜色,则称这个数m为该图的色


题解

二分法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "stdio.h"
#define dot 5
int count =0;//定义方案数量
int color[dot] = {0};//用0代表每个点当前未着色
int a[dot][dot]={0,1,1,1,0, //a数组代表无向图的邻接矩阵
1,0,1,1,1,
1,1,0,1,0,
1,1,1,0,1,
0,1,0,1,0};
int OK(int t){
for (int j =0; j<dot; j++) {
if (a[t][j]==1) {//判断第t个顶点与其他顶点是否相连,如果相连并且颜色一样返回0,否则就返回1.
if (color[t]==color[j]) {
return 0;
}
}
}
return 1;
}

int Traceback(int t,int sort){
int Oldvalue = 0;
if (t ==dot) {
count++;
if (count ==1) {
return 1;
}
return 0;
}
Oldvalue = color[t];
for (int j =1; j<=sort; j++) {
color[t] = j;
if (OK(t)) {
if(Traceback(t+1,sort))
return 1;
}
}
color[t] = Oldvalue;
return 0;

}
int main(void){

int left=0,right=dot;
int mid=(left+right)/2;

while(left<right){
printf("%d %d\n",left,right);
if(Traceback(0, mid)==1){
right=mid;
}else{
left=mid+1;
}
mid=(left+right)/2;
}

printf("m:%d",right);


return 0;
}
贪心法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <set>

using namespace std;

#define dot 5
int count =0;//定义方案数量
int color[dot] = {0};//用0代表每个点当前未着色
int a[dot][dot]={0,1,1,1,0, //a数组代表无向图的邻接矩阵
1,0,1,1,1,
1,1,0,1,0,
1,1,1,0,1,
0,1,0,1,0};

int getDu(int n){
int res=0;
for(int i=0;i<dot;i++){
res+=a[i][n];
}
return res;
}

int main(){

set<int> uncolor;
for(int i=0;i<dot;i++)
uncolor.insert(i);
int m=0;

while(uncolor.size()){
m++;
cout<<"---------"<<m<<"-----------"<<endl;
set<int> cancolor=uncolor;
uncolor.clear();
while(cancolor.size()){
int mini=*cancolor.begin();
int mind=getDu(mini);

for(auto i=cancolor.begin();i!=cancolor.end();i++){
int t=getDu(*i);
if(t<mind){
mini=*i;
mind=t;
}
}

for(int i=0;i<dot;i++){
if(a[i][mini]){
uncolor.insert(i);
cancolor.erase(i);
}
a[i][mini]=0;
a[mini][i]=0;
}
color[mini]=m;
cout<<mini<<endl;
cancolor.erase(mini);
}
}
cout<<endl;
cout<<"m:"<<m<<endl;

return 0;
}
-------- 本文结束 感谢阅读 --------
相关文章
  • 算法常用模板:并查集
  • 《C语言程序设计(第五版)谭浩强》课后习题答案源码
  • C语言基础编程练习题
  • OpenCV3使用中遇到的一些问题
  • Qt使用中遇到的一些问题
觉得文章写的不错的话,请我喝瓶怡宝吧!😀
SiriYang 微信支付

微信支付

SiriYang 支付宝

支付宝

  • 本文标题: 寻找无向图最小着色方案
  • 本文作者: SiriYang
  • 创建时间: 2020年05月24日 - 23时05分
  • 修改时间: 2021年10月29日 - 18时10分
  • 本文链接: https://blog.siriyang.cn/posts/20200524231423id.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
C/C++ 算法题
贪心学院xgboost细节讲解
Hexo:使用七牛云进行CDN加速
SiriYang

SiriYang

努力搬砖攒钱买镜头的摄影迷
321 日志
33 分类
88 标签
RSS
GitHub E-Mail
Creative Commons
Links
  • 友情链接
  • 作品商铺

蜀ICP备19008337号 © 2019 – 2025 SiriYang | 1.7m | 25:48
0%