Skip to content

Commit 7ef5ff4

Browse files
fix(backtracking): Correct doctest in m_coloring_problem (#13288)
* Create Calc.py * Rename Calc.py to calc.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Create rat_in_a_maze.py * Delete dynamic_programming/rat_in_a_maze.py * Create m-coloring-problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename m-coloring-problem.py to m_coloring_problem.py * Delete other/calc.py * Update m_coloring_problem.py * Update m_coloring_problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update m_coloring_problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update m_coloring_problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update m_coloring_problem.py * Update m_coloring_problem.py * Update m_coloring_problem.py * Update m_coloring_problem.py * Update m_coloring_problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update m_coloring_problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update m_coloring_problem.py * Update m_coloring_problem.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a79cd96 commit 7ef5ff4

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

backtracking/m_coloring_problem.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
def is_safe(
2+
node: int,
3+
color: int,
4+
graph: list[list[int]],
5+
num_vertices: int,
6+
col: list[int],
7+
) -> bool:
8+
"""
9+
Check if it is safe to assign a color to a node.
10+
11+
>>> is_safe(0, 1, [[0,1],[1,0]], 2, [0,1])
12+
False
13+
>>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1])
14+
True
15+
"""
16+
return all(
17+
not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices)
18+
)
19+
20+
21+
def solve(
22+
node: int,
23+
col: list[int],
24+
max_colors: int,
25+
num_vertices: int,
26+
graph: list[list[int]],
27+
) -> bool:
28+
"""
29+
Recursively try to color the graph using at most max_colors.
30+
31+
>>> solve(0, [0]*3, 3, 3, [[0,1,0],[1,0,1],[0,1,0]])
32+
True
33+
>>> solve(0, [0]*3, 2, 3, [[0,1,0],[1,0,1],[0,1,0]])
34+
True
35+
"""
36+
if node == num_vertices:
37+
return True
38+
for c in range(1, max_colors + 1):
39+
if is_safe(node, c, graph, num_vertices, col):
40+
col[node] = c
41+
if solve(node + 1, col, max_colors, num_vertices, graph):
42+
return True
43+
col[node] = 0
44+
return False
45+
46+
47+
def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) -> bool:
48+
"""
49+
Determine if the graph can be colored with at most max_colors.
50+
51+
>>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 3, 3)
52+
True
53+
>>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 2, 3)
54+
False
55+
"""
56+
col = [0] * num_vertices
57+
return solve(0, col, max_colors, num_vertices, graph)
58+
59+
60+
if __name__ == "__main__":
61+
import doctest
62+
63+
doctest.testmod()
64+
65+
num_vertices = int(input("Enter vertices: "))
66+
num_edges = int(input("Enter edges: "))
67+
graph = [[0] * num_vertices for _ in range(num_vertices)]
68+
69+
print("Enter edges (u v):")
70+
for _ in range(num_edges):
71+
try:
72+
u, v = map(int, input().split())
73+
if 0 <= u < num_vertices and 0 <= v < num_vertices:
74+
graph[u][v] = 1
75+
graph[v][u] = 1
76+
else:
77+
print("Invalid edge.")
78+
except ValueError:
79+
print("Invalid input.")
80+
81+
max_colors = int(input("Enter max colors: "))
82+
83+
if graph_coloring(graph, max_colors, num_vertices):
84+
print("Coloring possible.")
85+
else:
86+
print("Coloring not possible.")

0 commit comments

Comments
 (0)