-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - Graph Colouring.c
201 lines (157 loc) · 5.69 KB
/
8960 - Graph Colouring.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**********************************************************************************************************
NAME: CANDIDA RUTH NORONHA
CLASS: SE COMPS B
ROLL NO. : 8960
BATCH: C
TITLE: GRAPH COLOURING
SUBMISSION DATE : 12th APRIL, 2021
**********************************************************************************************************/
#include<stdio.h>
int n,sol[10]={0},count=0,adj[11][11]={0},m;
int can_color(int node,int c)
{
int j;
for(j=1;j<=node-1;j++)
{
if(adj[j][node]==1 && sol[j]==c)//check whether the adjacent node's colour is same as c
return 0;
}
return 1;
}
void m_color(int node)
{
int i,j;
for(i=1;i<=m;i++)
{
if(can_color(node,i))//check whether the node can be coloured with i
{
sol[node]=i;
if(node==n) //print solution vector
{
count++;
printf("\n********** Solution Vector : %d **********",count);
printf("\n");
for(j=1;j<=n;j++)
{
if(sol[j]==1) //Changing 1 to Red
printf("RED\t");
else if(sol[j]==2) //Changing 2 to Green
printf("GREEN\t");
else if(sol[j]==3) //Changing 3 to Blue
printf("BLUE\t");
else if(sol[j]==4) //Changing 4 to Yellow
printf("YELLOW\t");
else if(sol[j]==5) //Changing 5 to Violet
printf("VIOLET\t");
}
printf("\n");
}
else
m_color(node+1); //Call to m_color method
}
}
}
int main()
{
int i,s,e,d,deg;
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n\t\t\t\t Graph Colouring\n");
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the degree of the graph : ");//degree of the graph
scanf("%d", °);
printf("\n------------------------------------------------------------------------------------------------\n");
m=deg+1;
printf("\n Enter the no of vertices : ");
scanf("%d", &n);
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the no of edges : ");
scanf("%d", &e);
printf("\n------------------------------------------------------------------------------------------------\n");
for (i = 1; i <= e; i++) //enter the graph
{
printf("\n For Relation No : %d", i);
printf("\n Enter source node : ");
scanf("%d", &s);
if (s > n || s == 0)
{
printf("\n Invalid source Node\n Enter again ");
i--;
continue;
}
printf(" Enter destination node : ");
scanf("%d", &d);
if (d > n || s == 0)
{
printf("\n Invalid destination Node\n Enter again ");
i--;
continue;
}
adj[s][d] = 1; //Undirected Graph
adj[d][s] = 1;
}
printf("\n------------------------------------------------------------------------------------------------\n");
m_color(1);
printf("\n------------------------------------------------------------------------------------------------\n");
}
/**********************************************************************************************************
OUTPUT:
------------------------------------------------------------------------------------------------
Graph Colouring
------------------------------------------------------------------------------------------------
Enter the degree of the graph : 2
------------------------------------------------------------------------------------------------
Enter the no of vertices : 4
------------------------------------------------------------------------------------------------
Enter the no of edges : 4
------------------------------------------------------------------------------------------------
For Relation No : 1
Enter source node : 1
Enter destination node : 2
For Relation No : 2
Enter source node : 2
Enter destination node : 3
For Relation No : 3
Enter source node : 3
Enter destination node : 4
For Relation No : 4
Enter source node : 4
Enter destination node : 1
------------------------------------------------------------------------------------------------
********** Solution Vector : 1 **********
RED GREEN RED GREEN
********** Solution Vector : 2 **********
RED GREEN RED BLUE
********** Solution Vector : 3 **********
RED GREEN BLUE GREEN
********** Solution Vector : 4 **********
RED BLUE RED GREEN
********** Solution Vector : 5 **********
RED BLUE RED BLUE
********** Solution Vector : 6 **********
RED BLUE GREEN BLUE
********** Solution Vector : 7 **********
GREEN RED GREEN RED
********** Solution Vector : 8 **********
GREEN RED GREEN BLUE
********** Solution Vector : 9 **********
GREEN RED BLUE RED
********** Solution Vector : 10 **********
GREEN BLUE RED BLUE
********** Solution Vector : 11 **********
GREEN BLUE GREEN RED
********** Solution Vector : 12 **********
GREEN BLUE GREEN BLUE
********** Solution Vector : 13 **********
BLUE RED GREEN RED
********** Solution Vector : 14 **********
BLUE RED BLUE RED
********** Solution Vector : 15 **********
BLUE RED BLUE GREEN
********** Solution Vector : 16 **********
BLUE GREEN RED GREEN
********** Solution Vector : 17 **********
BLUE GREEN BLUE RED
********** Solution Vector : 18 **********
BLUE GREEN BLUE GREEN
------------------------------------------------------------------------------------------------
**********************************************************************************************************/