-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphColorBacktrack.c
More file actions
83 lines (65 loc) · 1.04 KB
/
graphColorBacktrack.c
File metadata and controls
83 lines (65 loc) · 1.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<stdio.h>
static int m, n, c=0, count=0;
int g[50][50], x[50];
void nextValue(int k);
void GraphColoring(int k);
void main()
{
int i, j, temp;
printf("\nEnter the number of nodes: " );
scanf("%d", &n);
printf("\nEnter Adjacency Matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d", &g[i][j]);
}
printf("\nPossible Solutions are\n");
for(m=1;m<=n;m++)
{
if(c==1)
break;
GraphColoring(1);
}
printf("\nThe chromatic number is %d", m-1);
//in for loop, m gets incremented first and then the condition is checked
//so it is m minus 1
printf("\nThe total number of solutions is %d", count);
}
void GraphColoring(int k)
{
int i;
while(1)
{
nextValue(k);
if(x[k]==0)
return;
if(k==n)
{
c=1;
for(i=1;i<=n;i++)
printf("%d ", x[i]);
count++;
printf("\n");
}
else
GraphColoring(k+1);
}
}
void nextValue(int k)
{
int j;
while(1)
{
x[k]=(x[k]+1)%(m+1);
if(x[k]==0)
return;
for(j=1;j<=n;j++)
{
if(g[k][j]==1&&x[k]==x[j])
break;
}
if(j==(n+1))
return;
}
}