Skip to content
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eacebd7
Create Calc.py
sgindeed Oct 6, 2025
21ef5e1
Rename Calc.py to calc.py
sgindeed Oct 6, 2025
2df824d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
913c90a
Create rat_in_a_maze.py
sgindeed Oct 6, 2025
16b6d78
Delete dynamic_programming/rat_in_a_maze.py
sgindeed Oct 6, 2025
d0909dc
Create m-coloring-problem.py
sgindeed Oct 6, 2025
152c0c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
7647b47
Rename m-coloring-problem.py to m_coloring_problem.py
sgindeed Oct 6, 2025
80cfe21
Delete other/calc.py
sgindeed Oct 6, 2025
5dba8a1
Update m_coloring_problem.py
sgindeed Oct 6, 2025
ecab4cf
Update m_coloring_problem.py
sgindeed Oct 6, 2025
8c00f5e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
67a6eb1
Update m_coloring_problem.py
sgindeed Oct 6, 2025
b1ae455
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
b8a0a74
Update m_coloring_problem.py
sgindeed Oct 6, 2025
a6a3db2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
a841332
Update m_coloring_problem.py
sgindeed Oct 6, 2025
15b2b7b
Update m_coloring_problem.py
sgindeed Oct 6, 2025
a729c20
Update m_coloring_problem.py
sgindeed Oct 6, 2025
96ba61e
Update m_coloring_problem.py
sgindeed Oct 6, 2025
0d3bd86
Update m_coloring_problem.py
sgindeed Oct 6, 2025
547da5e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
719da79
Update m_coloring_problem.py
sgindeed Oct 6, 2025
4f43023
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
6a5897a
Update m_coloring_problem.py
sgindeed Oct 6, 2025
18dd29f
Update m_coloring_problem.py
sgindeed Oct 6, 2025
bcc609c
Merge branch 'master' into master
sgindeed Oct 6, 2025
7e23518
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update m_coloring_problem.py
  • Loading branch information
sgindeed authored Oct 6, 2025
commit 5dba8a180317b764ebfb8ace152031ec9257f687
35 changes: 9 additions & 26 deletions backtracking/m_coloring_problem.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
def isSafe(node, color, graph, n, col):
for k in range(n):
if graph[node][k] == 1 and col[k] == color:
return False
return True
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 solve(node, col, m, n, graph):
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
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 8 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 8 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 8 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:8:27: UP006 Use `list` instead of `List` for type annotation
if node == n:
return True
for c in range(1, m + 1):
if isSafe(node, c, graph, n, col):
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 graphColoring(graph, m, n):
def graph_coloring(graph: List[List[int]], m: int, n: int) -> bool:

Check failure on line 20 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

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

Check failure on line 20 in backtracking/m_coloring_problem.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

backtracking/m_coloring_problem.py:20:27: UP006 Use `list` instead of `List` for type annotation
col = [0] * n
if solve(0, col, m, n, graph):
return True
return False


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")
return solve(0, col, m, n, graph)