1
- def is_safe (node : int , color : int , graph : list [list [int ]], num_vertices : int ,
2
- col : list [int ]) -> bool :
1
+ def is_safe (node : int , color : int , graph : list [list [int ]], num_vertices : int , col : list [int ]) -> bool :
3
2
"""
4
3
Check if it is safe to assign a color to a node.
5
4
@@ -8,12 +7,10 @@ def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int,
8
7
>>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1])
9
8
True
10
9
"""
11
- return all (not (graph [node ][k ] == 1 and col [k ] == color )
12
- for k in range (num_vertices ))
10
+ return all (not (graph [node ][k ] == 1 and col [k ] == color ) for k in range (num_vertices ))
13
11
14
12
15
- def solve (node : int , col : list [int ], max_colors : int , num_vertices : int ,
16
- graph : list [list [int ]]) -> bool :
13
+ def solve (node : int , col : list [int ], max_colors : int , num_vertices : int , graph : list [list [int ]]) -> bool :
17
14
"""
18
15
Recursively try to color the graph using at most max_colors.
19
16
@@ -33,8 +30,7 @@ def solve(node: int, col: list[int], max_colors: int, num_vertices: int,
33
30
return False
34
31
35
32
36
- def graph_coloring (graph : list [list [int ]], max_colors : int ,
37
- num_vertices : int ) -> bool :
33
+ def graph_coloring (graph : list [list [int ]], max_colors : int , num_vertices : int ) -> bool :
38
34
"""
39
35
Determine if the graph can be colored with at most max_colors.
40
36
@@ -50,7 +46,7 @@ def graph_coloring(graph: list[list[int]], max_colors: int,
50
46
if __name__ == "__main__" :
51
47
num_vertices = int (input ())
52
48
num_edges = int (input ())
53
- graph = [[0 ]* num_vertices for _ in range (num_vertices )]
49
+ graph = [[0 ] * num_vertices for _ in range (num_vertices )]
54
50
for _ in range (num_edges ):
55
51
u , v = map (int , input ().split ())
56
52
if 0 <= u < num_vertices and 0 <= v < num_vertices :
@@ -59,5 +55,4 @@ def graph_coloring(graph: list[list[int]], max_colors: int,
59
55
else :
60
56
raise ValueError ("Edge indices out of range" )
61
57
max_colors = int (input ())
62
- col = [0 ]* num_vertices
63
- print (solve (0 , col , max_colors , num_vertices , graph ))
58
+ print (graph_coloring (graph , max_colors , num_vertices ))
0 commit comments