forked from rafi007akhtar/c-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_coloring.c
More file actions
66 lines (54 loc) · 1.25 KB
/
graph_coloring.c
File metadata and controls
66 lines (54 loc) · 1.25 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
65
66
#include <stdio.h>
#include <stdlib.h>
int *x, n, m; // solution array (x)
void nextValue(int k, int graph[n][n]);
void mColoring(int k, int graph[n][n]);
int main()
{
int i, j;
printf("Enter the number of colors: ");
scanf("%d", &m);
printf("Enter the number of nodes: ");
scanf("%d", &n);
x = (int *)malloc(n*sizeof(int));
for (i = 0; i < n; i++) x[i] = 0;
int graph[n][n];
printf("Enter the adjacency matrix of the graph:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
scanf("%d", &graph[i][j]);
}
printf("\nThe solution array is:\n");
mColoring(0, graph);
return 0;
}
void mColoring(int k, int graph[n][n])
{
int i;
nextValue(k, graph);
if (x[k] == 0) return;
if (k == n-1)
{
for (i = 0; i < n; i++)
printf("%d ", x[i]);
printf("\n");
}
else mColoring(k+1, graph);
}
void nextValue(int k, int graph[n][n])
{
int i;
while(1)
{
x[k] = (x[k]+1) % (m+1); // fetch the next color using this formula
if (x[k] == 0) return; // all colors have been tried
for (i = 0; i < n; i++)
{
int con1 = (graph[i][k] != 0); // if (i,k) is an edge
int con2 = (x[i] == x[k]); // if the adjacent vertices have same color
if (con1 && con2) break;
}
if (i == n) return; // all vertices have beeen colored
}
}