-
Notifications
You must be signed in to change notification settings - Fork 2
/
m_graph_coloring.c
64 lines (55 loc) · 1.68 KB
/
m_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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int vertices,i,m_colors;
int *colors;
int count;
//vertices is declared globally so that every function can now access vertices variable without needing to be passed as parameter
bool is_safe(int vertex,int vertex_color,bool** graph){
for(i=0;i<vertex;i++){
if(graph[i][vertex] && colors[i] == vertex_color)
return false;
}
return true;
}
void print(int colors[]){
printf("Colored board is \n");
for(i=0;i<vertices;i++){
printf("%d ",colors[i]+1);
}
count++;
printf("\n");
}
void graph_color(int vertex, bool** graph){
//all vertices are colored
if(vertex==vertices){
print(colors);
return;
}
int color;
for(color = 0; color < m_colors; color++){
if(is_safe(vertex,color,graph)){
colors[vertex]=color;
graph_color(vertex+1, graph);
}
}
}
int main(){
printf("Enter the no. of vertices\n");
scanf("%d",&vertices);
printf("Enter the no. of colors\n");
scanf("%d",&m_colors);
int j;
bool** adjacency_matrix=(bool **)malloc(sizeof(bool *)*vertices);
for(i=0;i<vertices;i++) adjacency_matrix[i]=(bool *)malloc(sizeof(bool)*vertices);
printf("Enter the adjacency matrix for the graph\n");
for(i=0;i<vertices;i++){
for(j=0;j<vertices;j++)
scanf("%d",&adjacency_matrix[i][j]);
}
colors = (int *)calloc(vertices,sizeof(int));
graph_color(0,adjacency_matrix);
if(count == 0) printf("%d coloring for the graph is not possible \n",m_colors);
free(colors);
return 0;
}