-
Notifications
You must be signed in to change notification settings - Fork 0
/
mColoring.py
54 lines (44 loc) · 1.47 KB
/
mColoring.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Problem: Given adjacency matrix of a graph, check if the graph vertices
# can be colored with m colors. Print solution if exists.
# Print the colors assigned
def printSolution(solution):
print('Assigned colors are: ', end='')
for color in solution:
print(color, end=' ')
# Check if adjacent vertices have the chosen color
def isSafe(n, graph, vertex, color, c):
for i in range(n):
if graph[vertex][i] == 1 and color[i] == c:
return False
return True
# Check if m colors can be sufficient to color a graph,
# if yes assign the colors
def graphColor(n, graph, m, color, vertex):
# base case: If all n vertices are assigned a color then
# return true
if vertex == n:
return True
# Consider this vertex v and try different colors
for c in range(1, m + 1):
# Check if assignment of color c to v is fine
if isSafe(n, graph, vertex, color, c):
color[vertex] = c
if graphColor(n, graph, m, color, vertex + 1):
return True
# else backtrack
color[vertex] = 0
return False
# Driver Function
if __name__ == "__main__":
N = 4 # number of vertices
# '1' indicates the n are adjacent
Graph = [[0, 1, 1, 1],
[1, 0, 1, 0],
[1, 1, 0, 1],
[1, 0, 1, 0]]
M = 4
Color = [0] * N
if not graphColor(N, Graph, M, Color, 0):
print('No solution')
else:
printSolution(Color)