-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathgraph_coloring.c
83 lines (78 loc) · 1.92 KB
/
graph_coloring.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
/*Write a program to implement the graph coloring problem with m=3 using back tracking method. Consider the
following adjacency matrix for the graph.
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
*/
#include<stdio.h>
#define v 4
void printsol(int color[]) {
int i;
printf("The assigned colors are: ");
for (i = 0; i < v; i++)
printf(" %d ", color[i]);
printf("\n");
}
int isSafe(int adj[v][v], int color[]) {
int i,j;
for (i = 0; i < v; i++)
for (j = i + 1; j < v; j++)
if (adj[i][j] && color[j] == color[i]){
return 1;
}
return 0;
}
int graphcolor(int adj[v][v], int m, int i, int color[v]){
int j;
if (i == v){ // If we got all the vertex colored we check if it is safe
if (isSafe(adj, color) == 0) {
// If yes we print solution and return 0
printsol(color);
return 0;
} else{
return 1;
}
}
//Recursively colors all the vertex
for(j=1;j<=m;j++){
color[i] = j;
if (graphcolor(adj,m,i+1,color) == 0){ // Since we returned 0 i.e we got one solution we return the func
return 0;
}
color[i] = 0; // If we didn't get safe solu (i.e return value is 1) we start again
}
return 1;
}
int main(){
int i,j,m;
printf("Enter the number of colors [m] : ");
scanf("%d",&m);
int adj[v][v];
printf("\nEnter values of the Adjacency Matrix: \n");
for(i=0;i<v;i++){
for(j=0;j<v;j++){
printf("\nEnter the value of adj[%d][%d]: ",i,j);
scanf("%d",&adj[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("Adjacency Matrix: \n");
for(i=0;i<v;i++){
for(j=0;j<v;j++){
printf("%d ",adj[i][j]);
}
printf("\n");
}
printf("\nThe value of m is: %d \nThe value of v is: %d\n",m,v);
int color[v]; //Keeps record of the color, each vertex is colored with
for (i = 0; i < v; i++){
color[i] = 0; //first set all the nodes color as 0
}
if (graphcolor(adj, m, 0, color) == 1){
printf("Solution does not exist");
}
return 0;
}