Skip to content
48 changes: 48 additions & 0 deletions backtracking/m_coloring_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import List

Check failure on line 1 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

backtracking/m_coloring_problem.py:1:1: UP035 `typing.List` is deprecated, use `list` instead


def is_safe(
node: int, color: int, graph: List[List[int]], n: int, col: List[int]

Check failure on line 5 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:5:65: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:5:40: UP006 Use `list` instead of `List` for type annotation

Check failure on line 5 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:5:35: UP006 Use `list` instead of `List` for type annotation
) -> bool:
"""
Check if it is safe to assign a color to a node.

>>> is_safe(0, 1, [[0,1],[1,0]], 2, [0,1])
False
>>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1])
True
"""
return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n))


def solve(node: int, col: List[int], m: int, n: int, graph: List[List[int]]) -> bool:

Check failure on line 18 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:18:66: UP006 Use `list` instead of `List` for type annotation

Check failure on line 18 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:18:61: UP006 Use `list` instead of `List` for type annotation

Check failure on line 18 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:18:27: UP006 Use `list` instead of `List` for type annotation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: m

Please provide descriptive name for the parameter: n

"""
Recursively try to color the graph using at most m colors.

>>> solve(0, [0]*3, 3, 3, [[0,1,0],[1,0,1],[0,1,0]])
True
>>> solve(0, [0]*3, 2, 3, [[0,1,0],[1,0,1],[0,1,0]])
False
"""
if node == n:
return True
for c in range(1, m + 1):
if is_safe(node, c, graph, n, col):
col[node] = c
if solve(node + 1, col, m, n, graph):
return True
col[node] = 0
return False


def graph_coloring(graph: List[List[int]], m: int, n: int) -> bool:

Check failure on line 38 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:38:32: UP006 Use `list` instead of `List` for type annotation

Check failure on line 38 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:38:27: UP006 Use `list` instead of `List` for type annotation
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: m

Please provide descriptive name for the parameter: n

"""
Determine if the graph can be colored with at most m colors.

>>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 3, 3)
True
>>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 2, 3)
False
"""
col = [0] * n
return solve(0, col, m, n, graph)
Loading