-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcolor.py
32 lines (32 loc) · 947 Bytes
/
mcolor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def is_safe(node, graph, color, c):
for neighbor in range(len(graph)):
if graph[node][neighbor] == 1 and color[neighbor] == c:
return False
return True
def graph_coloring_util(graph, m, color, node):
if node == len(graph):
return True
for c in range(1, m + 1):
if is_safe(node, graph, color, c):
color[node] = c
if graph_coloring_util(graph, m, color, node + 1):
return True
color[node] = 0
return False
def graph_coloring(graph, m):
color = [0] * len(graph)
if not graph_coloring_util(graph, m, color, 0):
print("No solution exists.")
return False
print("Color assignment possible:")
for vertex, c in enumerate(color):
print(f"Vertex {vertex} --> Color {c}")
return True
graph = [
[0, 1, 1, 1],
[1, 0, 1, 0],
[1, 1, 0, 1],
[1, 0, 1, 0]
]
m = 3
graph_coloring(graph, m)