-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph coloring.c
74 lines (58 loc) · 1.33 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
#include <stdio.h>
#include <stdbool.h>
#define V 4
int i;
void printSolution(int color[])
{
printf("Vertex colors:\n");
for ( i = 0; i < V; i++)
printf("%d ", color[i]);
printf("\n");
}
bool isSafe(int v, bool graph[V][V], int color[], int c)
{
for ( i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}
bool graphColoringUtil(bool graph[V][V], int m, int color[], int v)
{
int c;
if (v == V)
return true;
for ( c = 1; c <= m; c++)
{
if (isSafe(v, graph, color, c))
{
color[v] = c;
if (graphColoringUtil(graph, m, color, v + 1))
return true;
color[v] = 0;
}
}
return false;
}
bool graphColoring(bool graph[V][V], int m)
{
int color[V];
for ( i = 0; i < V; i++)
color[i] = 0;
if (!graphColoringUtil(graph, m, color, 0))
{
printf("Solution does not exist.\n");
return false;
}
printSolution(color);
return true;
}
int main()
{
bool graph[V][V] = {{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}};
int m = 3;
graphColoring(graph, m);
return 0;
}