1
- def is_safe (node : int , color : int , graph : list [list [int ]], num_vertices : int ,
2
- col : list [int ]) -> bool :
1
+ def is_safe (
2
+ node : int , color : int , graph : list [list [int ]], num_vertices : int , col : list [int ]
3
+ ) -> bool :
3
4
"""
4
5
Check if it is safe to assign a color to a node.
5
6
@@ -8,12 +9,18 @@ def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int,
8
9
>>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1])
9
10
True
10
11
"""
11
- return all (not (graph [node ][k ] == 1 and col [k ] == color )
12
- for k in range (num_vertices ))
12
+ return all (
13
+ not (graph [node ][k ] == 1 and col [k ] == color ) for k in range (num_vertices )
14
+ )
13
15
14
16
15
- def solve (node : int , col : list [int ], max_colors : int , num_vertices : int ,
16
- graph : list [list [int ]]) -> bool :
17
+ def solve (
18
+ node : int ,
19
+ col : list [int ],
20
+ max_colors : int ,
21
+ num_vertices : int ,
22
+ graph : list [list [int ]],
23
+ ) -> bool :
17
24
"""
18
25
Recursively try to color the graph using at most max_colors.
19
26
@@ -33,8 +40,7 @@ def solve(node: int, col: list[int], max_colors: int, num_vertices: int,
33
40
return False
34
41
35
42
36
- def graph_coloring (graph : list [list [int ]], max_colors : int ,
37
- num_vertices : int ) -> bool :
43
+ def graph_coloring (graph : list [list [int ]], max_colors : int , num_vertices : int ) -> bool :
38
44
"""
39
45
Determine if the graph can be colored with at most max_colors.
40
46
@@ -52,7 +58,7 @@ def graph_coloring(graph: list[list[int]], max_colors: int,
52
58
num_vertices = int (input ("Enter number of vertices: " ))
53
59
num_edges = int (input ("Enter number of edges: " ))
54
60
print ("Enter each edge as 'u v' (0-based indexing):" )
55
- graph = [[0 ]* num_vertices for _ in range (num_vertices )]
61
+ graph = [[0 ] * num_vertices for _ in range (num_vertices )]
56
62
for _ in range (num_edges ):
57
63
u , v = map (int , input ().split ())
58
64
graph [u ][v ] = 1
0 commit comments