-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_Graph_Colouring.c
More file actions
58 lines (58 loc) · 1.23 KB
/
11_Graph_Colouring.c
File metadata and controls
58 lines (58 loc) · 1.23 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
//11_Graph_Colouring.c
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int graph[MAX][MAX];
int color[MAX];
int n;
int is_safe(int vertex, int c)
{
int i;
for (i = 0; i < n; i++) {
if (graph[vertex][i] && c == color[i]) {
return 0;
}
}
return 1;
}
int graph_coloring(int vertex)
{
int c;
if (vertex == n) {
return 1;
}
for (c = 1; c <= n; c++) {
if (is_safe(vertex, c)) {
color[vertex] = c;
if (graph_coloring(vertex + 1)) {
return 1;
}
color[vertex] = 0;
}
}
return 0;
}
int main()
{
int i, j;
printf("Enter the number of vertices (max %d): ", MAX);
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
}
}
for (i = 0; i < n; i++) {
color[i] = 0;
}
if (graph_coloring(0)) {
printf("The graph can be colored with %d colors:\n", n);
for (i = 0; i < n; i++) {
printf("Vertex %d: Color %d\n", i, color[i]);
}
} else {
printf("The graph cannot be colored with %d colors.\n", n);
}
return 0;
}