Skip to content

Commit a6a3db2

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b8a0a74 commit a6a3db2

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

backtracking/m_coloring_problem.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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:
34
"""
45
Check if it is safe to assign a color to a node.
56
@@ -8,12 +9,18 @@ def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int,
89
>>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1])
910
True
1011
"""
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+
)
1315

1416

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:
1724
"""
1825
Recursively try to color the graph using at most max_colors.
1926
@@ -33,8 +40,7 @@ def solve(node: int, col: list[int], max_colors: int, num_vertices: int,
3340
return False
3441

3542

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:
3844
"""
3945
Determine if the graph can be colored with at most max_colors.
4046
@@ -52,7 +58,7 @@ def graph_coloring(graph: list[list[int]], max_colors: int,
5258
num_vertices = int(input("Enter number of vertices: "))
5359
num_edges = int(input("Enter number of edges: "))
5460
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)]
5662
for _ in range(num_edges):
5763
u, v = map(int, input().split())
5864
graph[u][v] = 1

0 commit comments

Comments
 (0)