1- from typing import List
2-
3-
4- def is_safe (
5- node : int , color : int , graph : List [List [int ]], n : int , col : List [int ]
6- ) -> bool :
1+ def is_safe (node : int , color : int , graph : list [list [int ]], num_vertices : int , col : list [int ]) -> bool :
72 """
83 Check if it is safe to assign a color to a node.
94
@@ -12,37 +7,37 @@ def is_safe(
127 >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1])
138 True
149 """
15- return all (not (graph [node ][k ] == 1 and col [k ] == color ) for k in range (n ))
10+ return all (not (graph [node ][k ] == 1 and col [k ] == color ) for k in range (num_vertices ))
1611
1712
18- def solve (node : int , col : List [int ], m : int , n : int , graph : List [ List [int ]]) -> bool :
13+ def solve (node : int , col : list [int ], max_colors : int , num_vertices : int , graph : list [ list [int ]]) -> bool :
1914 """
20- Recursively try to color the graph using at most m colors .
15+ Recursively try to color the graph using at most max_colors .
2116
2217 >>> solve(0, [0]*3, 3, 3, [[0,1,0],[1,0,1],[0,1,0]])
2318 True
2419 >>> solve(0, [0]*3, 2, 3, [[0,1,0],[1,0,1],[0,1,0]])
2520 False
2621 """
27- if node == n :
22+ if node == num_vertices :
2823 return True
29- for c in range (1 , m + 1 ):
30- if is_safe (node , c , graph , n , col ):
24+ for c in range (1 , max_colors + 1 ):
25+ if is_safe (node , c , graph , num_vertices , col ):
3126 col [node ] = c
32- if solve (node + 1 , col , m , n , graph ):
27+ if solve (node + 1 , col , max_colors , num_vertices , graph ):
3328 return True
3429 col [node ] = 0
3530 return False
3631
3732
38- def graph_coloring (graph : List [ List [int ]], m : int , n : int ) -> bool :
33+ def graph_coloring (graph : list [ list [int ]], max_colors : int , num_vertices : int ) -> bool :
3934 """
40- Determine if the graph can be colored with at most m colors .
35+ Determine if the graph can be colored with at most max_colors .
4136
4237 >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 3, 3)
4338 True
4439 >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 2, 3)
4540 False
4641 """
47- col = [0 ] * n
48- return solve (0 , col , m , n , graph )
42+ col = [0 ] * num_vertices
43+ return solve (0 , col , max_colors , num_vertices , graph )
0 commit comments