7
7
"""
8
8
9
9
10
- def is_safe (graph : list [list [int ]], color : list [int ], v : int , c : int ) -> bool :
10
+ def is_safe (
11
+ graph : list [list [int ]], color : list [int ], vertex : int , color_choice : int
12
+ ) -> bool :
11
13
"""
12
- Helper function to check if it is safe to color vertex `v` with color `c` .
14
+ Helper function to check if it is safe to color a vertex with a specific color .
13
15
14
16
Parameters:
15
17
graph (list[list[int]]): The adjacency matrix of the graph.
16
18
color (list[int]): The list of colors assigned to each vertex.
17
- v (int): The vertex to check.
18
- c (int): The color to be assigned.
19
+ vertex (int): The vertex to check.
20
+ color_choice (int): The color to be assigned.
19
21
20
22
Returns:
21
- bool: True if it's safe to assign color `c` to vertex `v` , otherwise False.
23
+ bool: True if it's safe to assign the color , otherwise False.
22
24
23
25
Example:
24
26
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
25
27
>>> color = [-1, -1, -1]
26
28
>>> is_safe(graph, color, 0, 1)
27
29
True
28
30
"""
29
- return all (not (graph [v ][i ] == 1 and color [i ] == c ) for i in range (len (graph )))
31
+ return all (
32
+ not (graph [vertex ][i ] == 1 and color [i ] == color_choice )
33
+ for i in range (len (graph ))
34
+ )
30
35
31
36
32
37
def graph_coloring_util (
33
- graph : list [list [int ]], m : int , color : list [int ], v : int
38
+ graph : list [list [int ]], num_colors : int , color : list [int ], vertex : int
34
39
) -> bool :
35
40
"""
36
41
Utility function that uses backtracking to solve the m-coloring problem.
37
42
38
43
Parameters:
39
44
graph (list[list[int]]): The adjacency matrix of the graph.
40
- m (int): The maximum number of colors.
45
+ num_colors (int): The maximum number of colors.
41
46
color (list[int]): The list of colors assigned to each vertex.
42
- v (int): The current vertex to be colored.
47
+ vertex (int): The current vertex to be colored.
43
48
44
49
Returns:
45
50
bool: True if all vertices are successfully colored, otherwise False.
@@ -50,34 +55,34 @@ def graph_coloring_util(
50
55
>>> graph_coloring_util(graph, 3, color, 0)
51
56
True
52
57
"""
53
- if v == len (graph ):
58
+ if vertex == len (graph ):
54
59
return True
55
60
56
- for c in range (1 , m + 1 ):
57
- if is_safe (graph , color , v , c ):
58
- color [v ] = c
59
- if graph_coloring_util (graph , m , color , v + 1 ):
61
+ for color_choice in range (1 , num_colors + 1 ):
62
+ if is_safe (graph , color , vertex , color_choice ):
63
+ color [vertex ] = color_choice
64
+ if graph_coloring_util (graph , num_colors , color , vertex + 1 ):
60
65
return True
61
- color [v ] = - 1 # Backtrack
66
+ color [vertex ] = - 1 # Backtrack
62
67
63
68
return False
64
69
65
70
66
- def graph_coloring (graph : list [list [int ]], m : int ) -> bool :
71
+ def graph_coloring (graph : list [list [int ]], num_colors : int ) -> bool :
67
72
"""
68
73
Solves the m-coloring problem using backtracking.
69
74
70
75
Parameters:
71
76
graph (list[list[int]]): The adjacency matrix of the graph.
72
- m (int): The maximum number of colors.
77
+ num_colors (int): The maximum number of colors.
73
78
74
79
Returns:
75
- bool: True if the graph can be colored with `m ` colors, otherwise False.
80
+ bool: True if the graph can be colored with `num_colors ` colors, otherwise False.
76
81
77
82
Example:
78
83
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
79
84
>>> graph_coloring(graph, 3)
80
85
True
81
86
"""
82
87
color = [- 1 ] * len (graph )
83
- return graph_coloring_util (graph , m , color , 0 )
88
+ return graph_coloring_util (graph , num_colors , color , 0 )
0 commit comments