-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph_coloring.c
82 lines (82 loc) · 1.12 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
#include<stdio.h>
#include<conio.h>
static int m, n;
static int c=0;
static int count=0;
int g[50][50];
int x[50];
void nextValue(int k);
void GraphColoring(int k);
void main()
{
int i, j;
int 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);
printf("\nThe total number of solutions is %d", count);
getch();
}
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;
}
}
}
//complexity : O(m^V)