-
Notifications
You must be signed in to change notification settings - Fork 1
/
two_sat.py
216 lines (186 loc) · 6.19 KB
/
two_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
"""
The two-satisfiability problem,
used in a graph 3-colouring problem
"""
from typing import Dict, Tuple, List, Set, Iterator
def read_csv(path: str) -> Tuple[Dict[int, List[int]], Dict[int, int]]:
"""
Read a graph from file
Args:
path: str path to file
Returns:
Tuple[Dict[int, List[int]], Dict[int, int]]: - the graph, and its vertice colours
"""
gr = {}
cols = {}
with open(path, "r", encoding="utf-8") as infile:
quints = map(
lambda x: tuple(map(int, x)),
[line.split(",") for line in infile.readlines()],
)
for obj in quints:
if obj[0] in gr:
gr[obj[0]].add(obj[1])
else:
gr[obj[0]] = {obj[1]}
if obj[1] in gr:
gr[obj[1]].add(obj[0])
else:
gr[obj[1]] = {obj[0]}
cols[obj[0]] = obj[2]
cols[obj[1]] = obj[3]
return {v: list(gr[v]) for v in gr}, cols
def write_csv(
path: str, graph: Dict[int, List[int]], colours: List[Tuple[int]]
) -> None:
"""
Write a graph with its colours to a file
Args:
path: str - the path to file
graph: Dict[int, List[int]] - the graph
colours: List[Tuple[int]] - the vertice colours
"""
colours = dict(colours)
written = set()
with open(path, "w", encoding="utf-8") as outfile:
for vertice, adjacent_list in graph.items():
for adjacent in adjacent_list:
if (vertice, adjacent) not in written and (
adjacent,
vertice,
) not in written:
outfile.write(
f"{vertice},{adjacent},{colours[vertice]},{colours[adjacent]}\n"
)
written.add((vertice, adjacent))
# We should've taken the Catalan numbers
def make_impl_graph(edges: List[Tuple[int, int]]) -> Dict[int, List[int]]:
"""
Make a directed implication graph from an undirected graph
Args:
edges: Tuple[int, int] - a pair of vertice literals (could be 1 or -1).
should be off-by-1, because there should be -0 and +0
Returns:
bool - whether the graph edge colouring is valid
x v y = !x -> y & !y -> x
"""
res = {}
for v1, v2 in edges:
if -v1 not in res:
res[-v1] = [v2]
else:
res[-v1].append(v2)
if -v2 not in res:
res[-v2] = [v1]
else:
res[-v2].append(v1)
return {v: list(set(e)) for v, e in res.items()}
def dfs(
grp: Dict[int, List[int]], cur: int, visited: Set[int], path: List[int]
) -> List[int]:
"""
Perform dfs on graph
Args:
graph: a directed graph
Returns:
List[int] - the order of the nodes
"""
if cur in visited:
return
graph = {vertice: sorted(edges) for vertice, edges in grp.items()}
result = []
stack = [cur]
while stack:
s = stack[-1]
visited.add(s)
if s not in path:
path.append(s)
if s in graph:
graph[s] = list(filter(lambda x: x not in visited, graph[s]))
if graph[s] != []:
stack.append(graph[s][0])
continue
stack.remove(s)
return path
def invert_graph(graph: Dict[int, List[int]]) -> Dict[int, List[int]]:
"""
Invert the edges in a graph
Args:
graph: Dict[int, Lisst[int]] - a graph to be inverted
Returns:
Dict[int, List[int]] - an inverted graph
"""
new_graph = {}
for node, items in graph.items():
for item in items:
if item not in new_graph:
new_graph[item] = [node]
else:
new_graph[item].append(node)
return new_graph
def scc(graph: Dict[int, List[int]]) -> List[Set[int]]:
"""
Perform Kosaraju's algorith on graph
Args:
graph: a idrected graph
Returns:
List[Set[int]] - a list of strongly connected components
"""
visited = set()
base_path = []
for i in graph.keys():
if i not in visited:
dfs(graph, i, visited, base_path)
graph_inv = invert_graph(graph)
visited.clear()
paths = []
while len(base_path):
node = base_path.pop()
if node not in visited:
path = dfs(graph_inv, node, visited, [])
yield path[:]
path.clear()
return
def colour_graph(
graph: Dict[int, List[int]],
colours: Dict[int, int]
) -> List[Tuple[int]]:
"""
Colour a graph with 2-SAT
Args:
graph: Dict[int, List[int]] - the graph
colours: Dict[int, int]] - starting colours of vertices
Returns:
List[Tuple[int]] - list of pairs: a vertice and it's colour
"""
graph = {(vertice+1)*3 : [(v+1)*3 for v in graph[vertice]] for vertice in graph}
colours = {(vertice+1)*3: col for vertice, col in colours.items()}
clauses = []
for vertice in graph:
col = colours[vertice]
lst = list(filter(lambda x: x!=col, range(3)))
clauses.append((vertice+lst[0], vertice+lst[1]))
clauses.append((-vertice-lst[0], -vertice-lst[1]))
for adjacent in graph[vertice]:
for i in lst:
if colours[adjacent] != i and (-adjacent-i, -vertice-i) not in clauses:
clauses.append((-vertice-i, -adjacent-i))
impl_graph = make_impl_graph(clauses)
res = list(scc(impl_graph))
colouring = {}
nots = {}
while res and len(colouring) != len(graph):
last = res.pop()
if all(-x not in last for x in last):
while last != []:
col = last.pop()
if col > 0 and abs(col)%3 != colours[3*(abs(col)//3)]:
#if abs(col)//3 not in nots or abs(col)%3 not in nots[abs(col)//3]:
colouring[abs(col)//3] = abs(col)%3
elif abs(col)//3 not in nots:
nots[abs(col)//3] = list(set([abs(col)%3, colours[3*(abs(col)//3)]]))
else:
print("A vertice cannot be coloured!")
if colouring == {}:
colouring = {v: e[0] for v, e in nots.items()}
return sorted([(v-1, col) for v, col in colouring.items()], key=lambda x: x[0])