Skip to content

Commit 9e6f3de

Browse files
committed
Add Graph Colouring algorithm with Backtracking
1 parent cf92959 commit 9e6f3de

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

graphs/graph_colouring.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,44 @@
77
"""
88

99

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:
1113
"""
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.
1315
1416
Parameters:
1517
graph (list[list[int]]): The adjacency matrix of the graph.
1618
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.
1921
2022
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.
2224
2325
Example:
2426
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
2527
>>> color = [-1, -1, -1]
2628
>>> is_safe(graph, color, 0, 1)
2729
True
2830
"""
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+
)
3035

3136

3237
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
3439
) -> bool:
3540
"""
3641
Utility function that uses backtracking to solve the m-coloring problem.
3742
3843
Parameters:
3944
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.
4146
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.
4348
4449
Returns:
4550
bool: True if all vertices are successfully colored, otherwise False.
@@ -50,34 +55,34 @@ def graph_coloring_util(
5055
>>> graph_coloring_util(graph, 3, color, 0)
5156
True
5257
"""
53-
if v == len(graph):
58+
if vertex == len(graph):
5459
return True
5560

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):
6065
return True
61-
color[v] = -1 # Backtrack
66+
color[vertex] = -1 # Backtrack
6267

6368
return False
6469

6570

66-
def graph_coloring(graph: list[list[int]], m: int) -> bool:
71+
def graph_coloring(graph: list[list[int]], num_colors: int) -> bool:
6772
"""
6873
Solves the m-coloring problem using backtracking.
6974
7075
Parameters:
7176
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.
7378
7479
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.
7681
7782
Example:
7883
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]
7984
>>> graph_coloring(graph, 3)
8085
True
8186
"""
8287
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

Comments
 (0)