-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMColoring.cpp
More file actions
64 lines (49 loc) · 1.05 KB
/
MColoring.cpp
File metadata and controls
64 lines (49 loc) · 1.05 KB
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<bits/stdc++.h>
using namespace std;
bool isPossible(int node,int g[][10],int colors[], int color, int n){
for(int i=0;i<n;i++){
if(g[node][i] == 1 && colors[i] == color){
return false;
}
}
return true;
}
bool mColoring(int g[][10], int node, int colors[], int m, int n){
if(node == n)
return true;
for(int i=1;i<=m;i++){
if(isPossible(node,g,colors,i,n)){
colors[node] = i;
if(mColoring(g, node+1, colors, m, n))
return true;
colors[node]=0;
}
}
return false;
}
int main(){
fstream infile;
infile.open("Ex.txt", ios::in);
if (!infile) {
printf("\nError to open the file\n");
exit (1);
}
int n,m;
infile>>n;
int graph[10][10];
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
infile>>graph[i][j];
}
}
infile>>m;
infile.close();
int colors[n] = {0};
cout<<m<<endl;
if(mColoring(graph,0,colors,m,n))
for(int i=0;i<n;i++)
cout<<"Vertex -> "<<i<<" Color -> "<<colors[i]<<"\n";
else
cout<<"Not Possible to Color the Graph\n";
}