File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ def is_safe (node , graph , color , c ):
2
+ for neighbor in range (len (graph )):
3
+ if graph [node ][neighbor ] == 1 and color [neighbor ] == c :
4
+ return False
5
+ return True
6
+ def graph_coloring_util (graph , m , color , node ):
7
+ if node == len (graph ):
8
+ return True
9
+ for c in range (1 , m + 1 ):
10
+ if is_safe (node , graph , color , c ):
11
+ color [node ] = c
12
+ if graph_coloring_util (graph , m , color , node + 1 ):
13
+ return True
14
+ color [node ] = 0
15
+ return False
16
+ def graph_coloring (graph , m ):
17
+ color = [0 ] * len (graph )
18
+ if not graph_coloring_util (graph , m , color , 0 ):
19
+ print ("No solution exists." )
20
+ return False
21
+ print ("Color assignment possible:" )
22
+ for vertex , c in enumerate (color ):
23
+ print (f"Vertex { vertex } --> Color { c } " )
24
+ return True
25
+ graph = [
26
+ [0 , 1 , 1 , 1 ],
27
+ [1 , 0 , 1 , 0 ],
28
+ [1 , 1 , 0 , 1 ],
29
+ [1 , 0 , 1 , 0 ]
30
+ ]
31
+ m = 3
32
+ graph_coloring (graph , m )
You can’t perform that action at this time.
0 commit comments