Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions backtracking/m_coloring_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def isSafe(node, color, graph, n, col):

Check failure on line 1 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/m_coloring_problem.py:1:5: N802 Function name `isSafe` should be lowercase

Check failure on line 1 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/m_coloring_problem.py:1:5: N802 Function name `isSafe` should be lowercase
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 return type hint for the function: isSafe. If the function does not return a value, please provide the type hint as: def function() -> None:

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 isSafe

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: isSafe

Please provide type hint for the parameter: node

Please provide type hint for the parameter: color

Please provide type hint for the parameter: graph

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

Please provide type hint for the parameter: col

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 return type hint for the function: isSafe. If the function does not return a value, please provide the type hint as: def function() -> None:

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 isSafe

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: isSafe

Please provide type hint for the parameter: node

Please provide type hint for the parameter: color

Please provide type hint for the parameter: graph

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

Please provide type hint for the parameter: col

for k in range(n):
if graph[node][k] == 1 and col[k] == color:
return False
return True

Check failure on line 5 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM110)

backtracking/m_coloring_problem.py:2:5: SIM110 Use `return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n))` instead of `for` loop

Check failure on line 5 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM110)

backtracking/m_coloring_problem.py:2:5: SIM110 Use `return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n))` instead of `for` loop


def solve(node, col, m, n, graph):
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 return type hint for the function: solve. If the function does not return a value, please provide the type hint as: def function() -> None:

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 type hint for the parameter: node

Please provide type hint for the parameter: col

Please provide descriptive name for the parameter: m

Please provide type hint for the parameter: m

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

Please provide type hint for the parameter: graph

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 return type hint for the function: solve. If the function does not return a value, please provide the type hint as: def function() -> None:

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 type hint for the parameter: node

Please provide type hint for the parameter: col

Please provide descriptive name for the parameter: m

Please provide type hint for the parameter: m

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

Please provide type hint for the parameter: graph

if node == n:
return True
for c in range(1, m + 1):
if isSafe(node, c, graph, n, col):
col[node] = c
if solve(node + 1, col, m, n, graph):
return True
col[node] = 0
return False


def graphColoring(graph, m, n):

Check failure on line 20 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/m_coloring_problem.py:20:5: N802 Function name `graphColoring` should be lowercase

Check failure on line 20 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/m_coloring_problem.py:20:5: N802 Function name `graphColoring` should be lowercase
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 return type hint for the function: graphColoring. If the function does not return a value, please provide the type hint as: def function() -> None:

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 graphColoring

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: graphColoring

Please provide type hint for the parameter: graph

Please provide descriptive name for the parameter: m

Please provide type hint for the parameter: m

Please provide descriptive name for the parameter: n

Please provide type hint 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 return type hint for the function: graphColoring. If the function does not return a value, please provide the type hint as: def function() -> None:

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 graphColoring

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: graphColoring

Please provide type hint for the parameter: graph

Please provide descriptive name for the parameter: m

Please provide type hint for the parameter: m

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: n

col = [0] * n
if solve(0, col, m, n, graph):
return True
return False

Check failure on line 24 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM103)

backtracking/m_coloring_problem.py:22:5: SIM103 Return the condition `bool(solve(0, col, m, n, graph))` directly

Check failure on line 24 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM103)

backtracking/m_coloring_problem.py:22:5: SIM103 Return the condition `bool(solve(0, col, m, n, graph))` directly


if __name__ == "__main__":
V = int(input())
E = int(input())
graph = [[0 for _ in range(V)] for _ in range(V)]
for _ in range(E):
u, v = map(int, input().split())
graph[u][v] = 1
graph[v][u] = 1
m = int(input())
if graphColoring(graph, m, V):
print("True")
else:
print("False")
Loading