Skip to content
Open
Changes from all commits
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
86 changes: 86 additions & 0 deletions backtracking/m_coloring_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
def is_safe(
node: int,
color: int,
graph: list[list[int]],
num_vertices: int,
col: list[int],
) -> 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(num_vertices)
)


def solve(
node: int,
col: list[int],
max_colors: int,
num_vertices: int,
graph: list[list[int]],
) -> bool:
"""
Recursively try to color the graph using at most max_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]])
True
"""
if node == num_vertices:
return True
for c in range(1, max_colors + 1):
if is_safe(node, c, graph, num_vertices, col):
col[node] = c
if solve(node + 1, col, max_colors, num_vertices, graph):
return True
col[node] = 0
return False


def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) -> bool:
"""
Determine if the graph can be colored with at most max_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] * num_vertices
return solve(0, col, max_colors, num_vertices, graph)


if __name__ == "__main__":
import doctest

doctest.testmod()

num_vertices = int(input("Enter vertices: "))
num_edges = int(input("Enter edges: "))
graph = [[0] * num_vertices for _ in range(num_vertices)]

print("Enter edges (u v):")
for _ in range(num_edges):
try:
u, v = map(int, input().split())
if 0 <= u < num_vertices and 0 <= v < num_vertices:
graph[u][v] = 1
graph[v][u] = 1
else:
print("Invalid edge.")
except ValueError:
print("Invalid input.")

max_colors = int(input("Enter max colors: "))

if graph_coloring(graph, max_colors, num_vertices):
print("Coloring possible.")
else:
print("Coloring not possible.")