1
+ #include <stdbool.h>
2
+ #include <stdio.h>
3
+
4
+ int V , m ; // m is the number of colors
5
+
6
+ void input (){
7
+ printf ("Enter number of vertices in the graph: " );
8
+ scanf ("%d" , & V );
9
+ printf ("Enter number of colors: " );
10
+ scanf ("%d" , & m );
11
+ }
12
+
13
+ void addEdge (int u , int v , bool graph [V ][V ]){
14
+ graph [u ][v ] = 1 ;
15
+ graph [v ][u ] = 1 ;
16
+ }
17
+ void printSolution (int color []);
18
+
19
+ /* To check if the current color assignment
20
+ is safe for vertex v i.e. checks
21
+ whether the edge exists or not
22
+ (i.e, graph[v][i]==1). If it exists
23
+ then checks whether the color to
24
+ be filled in the new vertex(c is
25
+ sent in the parameter) is already
26
+ used by its adjacent
27
+ vertices(i-->adj vertices) or
28
+ not (i.e, color[i]==c) */
29
+ bool isSafe (int v , bool graph [V ][V ], int color [], int c )
30
+ {
31
+ for (int i = 0 ; i < V ; i ++ )
32
+ if (graph [v ][i ] && c == color [i ])
33
+ return false;
34
+ return true;
35
+ }
36
+
37
+ /* A recursive function
38
+ to solve m coloring problem */
39
+ bool graphColoringUtil (bool graph [V ][V ], int m , int color [],
40
+ int v )
41
+ {
42
+ /* base case: If all vertices are
43
+ assigned a color then return true */
44
+ if (v == V )
45
+ return true;
46
+
47
+ /* Consider this vertex v and
48
+ try different colors */
49
+ for (int c = 1 ; c <= m ; c ++ ) {
50
+ /* Check if assignment of color
51
+ c to v is fine*/
52
+ if (isSafe (v , graph , color , c )) {
53
+ color [v ] = c ;
54
+
55
+ /* recur to assign colors to
56
+ rest of the vertices */
57
+ if (graphColoringUtil (graph , m , color , v + 1 )
58
+ == true)
59
+ return true;
60
+
61
+ /* If assigning color c doesn't
62
+ lead to a solution then remove it */
63
+ color [v ] = 0 ;
64
+ }
65
+ }
66
+
67
+ /* If no color can be assigned to
68
+ this vertex then return false */
69
+ return false;
70
+ }
71
+
72
+ /* This function solves the m Coloring
73
+ problem using Backtracking. It
74
+ uses graphColoringUtil() to solve the
75
+ problem. It returns false if the m
76
+ colors cannot be assigned, otherwise
77
+ return true and prints assignments of
78
+ colors to all vertices. Please note
79
+ that there may be more than one solutions,
80
+ this function prints one of the
81
+ feasible solutions.*/
82
+ bool graphColoring (bool graph [V ][V ], int m )
83
+ {
84
+ // Initialize all color values as 0.
85
+ // This initialization is needed
86
+ // correct functioning of isSafe()
87
+ int color [V ];
88
+ for (int i = 0 ; i < V ; i ++ )
89
+ color [i ] = 0 ;
90
+
91
+ // Call graphColoringUtil() for vertex 0
92
+ if (graphColoringUtil (graph , m , color , 0 ) == false) {
93
+ printf ("Solution does not exist" );
94
+ return false;
95
+ }
96
+
97
+ // Print the solution
98
+ printSolution (color );
99
+ return true;
100
+ }
101
+
102
+ /* A utility function to print solution */
103
+ void printSolution (int color [])
104
+ {
105
+ printf ("Solution Exists:"
106
+ " Following are the assigned colors \n" );
107
+ for (int i = 0 ; i < V ; i ++ )
108
+ printf (" %d " , color [i ]);
109
+ printf ("\n" );
110
+ }
111
+
112
+ // Driver code
113
+ int main ()
114
+ {
115
+ /* Create following graph and test
116
+ whether it is 3 colorable
117
+ (3)---(2)
118
+ | / |
119
+ | / |
120
+ | / |
121
+ (0)---(1)
122
+ */
123
+ input ();
124
+
125
+ bool graph [V ][V ];
126
+ int i , j , t ;
127
+ for (i = 0 ; i < V ; i ++ ){
128
+ for (j = 0 ; j < V ; j ++ ){
129
+ graph [i ][j ] = 0 ;
130
+ }
131
+ }
132
+
133
+ for (i = 0 ; i < V ; i ++ ){
134
+ for (j = 0 ; j < V ; j ++ ){
135
+ printf ("Enter edge between %d and %d (1->Yes and 0->No): " , i , j );
136
+ scanf ("%d" , & t );
137
+ if (t == 1 )
138
+ addEdge (i , j , graph );
139
+ }
140
+ }
141
+
142
+ printf ("\nAdjacency Matrix:\n" );
143
+ for (i = 0 ; i < V ; i ++ ){
144
+ for (j = 0 ; j < V ; j ++ ){
145
+ printf ("%d " , graph [i ][j ]);
146
+ }
147
+ printf ("\n" );
148
+ }
149
+
150
+ // Function call
151
+ graphColoring (graph , m );
152
+ return 0 ;
153
+ }
0 commit comments