Skip to content
46 changes: 46 additions & 0 deletions backtracking/m_coloring_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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

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]) -> bool:

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/m_coloring_problem.py:4:89: E501 Line too long (91 > 88)

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/m_coloring_problem.py:4:89: E501 Line too long (91 > 88)

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 4 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:4:43: 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.

As there is no test file in this pull request nor any test function or class in the file backtracking/m_coloring_problem.py, please provide doctest for the function is_safe

Please provide descriptive name for the parameter: n

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: n

"""
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 16 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 16 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 16 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 16 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 16 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 16 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:16: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.

As there is no test file in this pull request nor any test function or class in the file backtracking/m_coloring_problem.py, please provide doctest for the function solve

Please provide descriptive name for the parameter: m

Please provide descriptive name for the parameter: n

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 36 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 36 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 36 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 36 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:36: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.

As there is no test file in this pull request nor any test function or class in the file backtracking/m_coloring_problem.py, please provide doctest for the function graph_coloring

Please provide descriptive name for the parameter: m

Please provide descriptive name for the parameter: n

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