-
Notifications
You must be signed in to change notification settings - Fork 0
/
total_sat.py
261 lines (218 loc) · 9.1 KB
/
total_sat.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python3
from pysat.solvers import Glucose3
import networkx
from bisect import bisect_left
# Assignes the colors to graph based on the solution
# if the value wasn't defined by the solver, random value is chosen
def fill_colors(graph, solution, color_values, variables, edge_nums):
solution = [x for x in solution if x > 0]
for u in graph.nodes():
node_col = -1
for c in color_values:
var_num = variables[u, c]
if binary_search(solution, var_num) is not None:
node_col = c
break
graph.nodes[u]["color"] = node_col - 1
for u, v in graph.edges():
edge_col = -1
for c in color_values:
var_num = variables[edge_nums[u, v], c]
if binary_search(solution, var_num) is not None:
edge_col = c
break
graph.edges[u, v]["color"] = edge_col - 1
# Validates the solution returned from iterative process
def validate_solution(graph):
for u, v in graph.edges():
# Neighboring vertices with same color
if graph.nodes[u]["color"] == graph.nodes[v]["color"]:
return False
# Vertex and its edge have the same color
if graph.nodes[u]["color"] == graph.edges[u, v]["color"] or graph.nodes[v]["color"] == graph.edges[u, v]["color"]:
return False
for u in graph.nodes():
incident_colors = [graph.edges[e]["color"] for e in graph.edges(u)]
unique = set()
for col in incident_colors:
# Neighboring edges with the same color
if col in unique:
return False
unique.add(col)
return True
def binary_search(a, x):
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
else:
return None
# Finds total chromatic index and assigns color to each node and edge
# in iterative manner, by providing not fully specified problem to the solver
def total_coloring_iterative(graph):
# Size of the chunks in which the problem will be iteratively defined
chunk_size = max(1, len(graph.nodes()) // 3)
nodes = []
# Initiate
max_deg = 0
for node in graph.nodes:
max_deg = max(max_deg, graph.degree[node])
nodes.append(node)
colors_count = max_deg
edge_nums = {}
edge_num = len(graph.nodes)
edge_nums_values = []
for e in graph.edges:
edge_nums[e[0], e[1]] = edge_num
edge_nums[e[1], e[0]] = edge_num
edge_nums_values.append(edge_num)
edge_num += 1
solution_found = False
solution = []
color_values = []
variables = {}
# Search for solution with the given amount of colors (lower bound is max_degree + 1)
while not solution_found:
colors_count += 1
color_values = [c + 1 for c in range(colors_count)]
# Define problem
g = Glucose3()
# Define variables
cnt = 0
variables = {}
for v in graph.nodes:
for c in color_values:
variables[(v, c)] = cnt * colors_count + c
cnt += 1
for e in edge_nums_values:
for c in color_values:
variables[(e, c)] = cnt * colors_count + c
cnt += 1
# Common clauses so that we get at least some colors assigned
for v in graph.nodes:
# Constraint - At least 1 color for each node
g.add_clause([variables[v, c] for c in color_values])
# Constraint - At most 1 color for each node
for i in range(len(color_values) - 1):
for j in range(i + 1, len(color_values)):
g.add_clause([-variables[v, color_values[i]], -variables[v, color_values[j]]])
for e0, e1 in graph.edges:
e = edge_nums[e0, e1]
# Constraint - At least 1 color for each edge
g.add_clause([variables[e, c] for c in color_values])
# Constraint - At least 1 color for each edge
for i in range(len(color_values) - 1):
for j in range(i + 1, len(color_values)):
g.add_clause([-variables[e, color_values[i]], -variables[e, color_values[j]]])
# Constraint - Different color for each (edge, v, u)
for c in color_values:
g.add_clause([-variables[e0, c], -variables[e1, c]])
g.add_clause([-variables[e0, c], -variables[e, c]])
g.add_clause([-variables[e1, c], -variables[e, c]])
# Iteratively find solutions
starting_at = 0
while starting_at < len(graph.nodes):
# Define the problem chunk
for node_i in range(starting_at, min(starting_at + chunk_size, len(graph.nodes()))):
v = nodes[node_i]
# Constraint - Different color for each pair of edges sharing a node
incident = [e for e in graph.edges(v)]
for c in color_values:
if len(incident) > 1:
for i in range(len(incident) - 1):
for j in range(i + 1, len(incident)):
g.add_clause([-variables[edge_nums[incident[i]], c], -variables[edge_nums[incident[j]], c]])
# Try to get solution
if g.solve():
solution = g.get_model()
fill_colors(graph, solution, color_values, variables, edge_nums)
if validate_solution(graph):
solution_found = True
break
starting_at += chunk_size
fill_colors(graph, solution, color_values, variables, edge_nums)
return colors_count
# Finds total chromatic index and assigns color to each node and edge
def total_coloring(graph: networkx.Graph):
# Initiate
max_deg = 0
for node in graph.nodes:
max_deg = max(max_deg, graph.degree[node])
colors_count = max_deg
edge_nums = {}
edge_num = len(graph.nodes)
edge_nums_values = []
for e in graph.edges:
edge_nums[e[0], e[1]] = edge_num
edge_nums[e[1], e[0]] = edge_num
edge_nums_values.append(edge_num)
edge_num += 1
solution_found = False
solution = []
color_values = []
variables = {}
# Search for solution with the given amount of colors (lower bound is max_degree + 1)
while not solution_found:
colors_count += 1
color_values = [c + 1 for c in range(colors_count)]
# Define problem
g = Glucose3()
# Define variables
cnt = 0
variables = {}
for v in graph.nodes:
for c in color_values:
variables[(v, c)] = cnt * colors_count + c
cnt += 1
for e in edge_nums_values:
for c in color_values:
variables[(e, c)] = cnt * colors_count + c
cnt += 1
for v in graph.nodes:
# Constraint - At least 1 color for each node
g.add_clause([variables[v, c] for c in color_values])
# Constraint - At most 1 color for each node
for i in range(len(color_values) - 1):
for j in range(i + 1, len(color_values)):
g.add_clause([-variables[v, color_values[i]], -variables[v, color_values[j]]])
# Constraint - Different color for each pair of edges sharing a node
incident = [e for e in graph.edges(v)]
for c in color_values:
if len(incident) > 1:
for i in range(len(incident) - 1):
for j in range(i + 1, len(incident)):
g.add_clause([-variables[edge_nums[incident[i]], c], -variables[edge_nums[incident[j]], c]])
for e0, e1 in graph.edges:
e = edge_nums[e0, e1]
# Constraint - At least 1 color for each edge
g.add_clause([variables[e, c] for c in color_values])
# Constraint - At least 1 color for each edge
for i in range(len(color_values) - 1):
for j in range(i + 1, len(color_values)):
g.add_clause([-variables[e, color_values[i]], -variables[e, color_values[j]]])
# Constraint - Different color for each (edge, v, u)
for c in color_values:
g.add_clause([-variables[e0, c], -variables[e1, c]])
g.add_clause([-variables[e0, c], -variables[e, c]])
g.add_clause([-variables[e1, c], -variables[e, c]])
# Get solution
if g.solve():
solution_found = True
solution = g.get_model()
solution = [x for x in solution if x > 0]
for u in graph.nodes():
node_col = -1
for c in color_values:
var_num = variables[u, c]
if binary_search(solution, var_num) is not None:
node_col = c
break
graph.nodes[u]["color"] = node_col - 1
for u, v in graph.edges():
edge_col = -1
for c in color_values:
var_num = variables[edge_nums[u, v], c]
if binary_search(solution, var_num) is not None:
edge_col = c
break
graph.edges[u, v]["color"] = edge_col - 1
return colors_count