File tree 1 file changed +42
-0
lines changed
Graph Coloring using Backtracking
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ // YASH ASHOK SHIRSATH
2
+ // Implement a program for Graph Coloring using Backtracking
3
+
4
+ #include <stdio.h>
5
+ int G [50 ][50 ],x [50 ]; //G:adjacency matrix,x:colors
6
+ void next_color (int k ){
7
+ int i ,j ;
8
+
9
+ x [k ]= 1 ; //coloring vertex with color1
10
+ for (i = 0 ;i < k ;i ++ ){ //checking all k-1 vertices-backtracking
11
+ if (G [i ][k ]!= 0 && x [k ]== x [i ]) //if connected and has same color
12
+ x [k ]= x [i ]+ 1 ; //assign higher color than x[i]
13
+ }
14
+ }
15
+
16
+ int main (){
17
+ int n ,e ,i ,j ,k ,l ;
18
+ printf ("Enter no. of vertices : " );
19
+ scanf ("%d" ,& n ); //total vertices
20
+ printf ("Enter no. of edges : " );
21
+ scanf ("%d" ,& e ); //total edges
22
+
23
+ for (i = 0 ;i < n ;i ++ )
24
+ for (j = 0 ;j < n ;j ++ )
25
+ G [i ][j ]= 0 ; //assign 0 to all index of adjacency matrix
26
+
27
+ printf ("Enter indexes where value is 1-->\n" );
28
+ for (i = 0 ;i < e ;i ++ ){
29
+ scanf ("%d %d" ,& k ,& l );
30
+ G [k ][l ]= 1 ;
31
+ G [l ][k ]= 1 ;
32
+ }
33
+
34
+ for (i = 0 ;i < n ;i ++ )
35
+ next_color (i ); //coloring each vertex
36
+
37
+ printf ("Colors of vertices -->\n" );
38
+ for (i = 0 ;i < n ;i ++ ) //displaying color of each vertex
39
+ printf ("Vertex[%d] : %d\n" ,i + 1 ,x [i ]);
40
+
41
+ return 0 ;
42
+ }
You can’t perform that action at this time.
0 commit comments