-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_graphdata.py
204 lines (169 loc) · 6.08 KB
/
gen_graphdata.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
import random
import numpy as np
import sys
MAX_GRAPH_SIZE = 50
COLOURS = ['green', 'red']
THRESHOLD = 0.5
np.set_printoptions(threshold=sys.maxsize)
def gen_graph(min_node=1, max_node=MAX_GRAPH_SIZE, prob=THRESHOLD):
n = random.randint(min_node, max_node)
colours = [random_colour() for _ in range(n)]
edges = make_edges(n, prob=prob)
return colours, edges
def random_colour():
return random.choice(COLOURS)
def make_edges(n, prob=THRESHOLD):
edges = np.random.rand(n, n)
edges[edges > prob] = 1
edges[edges <= prob] = 0
for i in range(n):
edges[i][i] = 0
return edges
def is_connected(edges, i, j):
visited, neighbour = set(), [i]
while neighbour:
neighbour = [j for n in neighbour for j in range(len(edges)) if edges[n][j] and j not in visited]
visited.update(neighbour)
if j in neighbour:
return True
return False
def path(edges, s, d):
visited = [False] * len(edges)
queue = []
queue.append([s, [s]])
while queue:
[n, path] = queue.pop(0)
for i in range(len(edges)):
if edges[n][i] and visited[i] == False:
queue.append([i, path + [i]])
visited[i] = True
if i == d:
return path + [i]
return False
class AdjacentToRed:
@staticmethod
def gen_pos(k):
colours, edges = gen_graph(min_node=2)
[i, j] = random.sample(list(range(len(edges))), 2)
edges[i][j] = 1
colours[j] = "red"
return [k, colours, edges], f"f(n_{k}_{i})"
@staticmethod
def gen_neg(k):
colours, edges = gen_graph(min_node=2)
while True:
[i] = random.sample(list(range(len(edges))), 1)
adjacent_red = [j for j in range(len(edges)) if edges[i][j] and colours[j] == 'red']
for j in adjacent_red:
edges[i][j] = 0
return [k, colours, edges], f"f(n_{k}_{i})"
class Connectedness:
@staticmethod
def gen_pos(k):
while True:
colours, edges = gen_graph(min_node=2, prob=0.9)
[start] = random.sample(list(range(len(edges))), 1)
n = random.randint(1, len(edges))
i, j, unvisited = start, start, set([i for i in range(len(edges))])
while n:
[j] = random.sample(unvisited, 1)
unvisited.remove(j)
edges[i][j] = 1
i = j
n -= 1
return [k, colours, edges], f"f(n_{k}_{start}, n_{k}_{j})"
@staticmethod
def gen_neg(k):
colours, edges = gen_graph(min_node=2, prob=0.9)
[i, j] = random.sample(list(range(len(edges))), 2)
while path(edges, i, j):
path_i_j = path(edges, i, j)
u = random.randint(0, len(path_i_j)-1)
edges[path_i_j[u], path_i_j[(u+1) % len(path_i_j)]] = 0
assert not is_connected(edges, i, j)
return [k, colours, edges], f"f(n_{k}_{i}, n_{k}_{j})"
class Cyclic:
@staticmethod
def gen_pos(k):
while True:
colours, edges = gen_graph(min_node=2, prob=0.9)
[start] = random.sample(list(range(len(edges))), 1)
n = random.randint(1, len(edges))
i, j, unvisited = start, start, set([i for i in range(len(edges))])
while n:
[j] = random.sample(unvisited, 1)
unvisited.remove(j)
edges[i][j] = 1
i = j
n -= 1
edges[j][start] = 1
return [k, colours, edges], f"f(n_{k}_{start})"
@staticmethod
def gen_neg(k):
colours, edges = gen_graph(min_node=2, prob=0.9)
[i] = random.sample(list(range(len(edges))), 1)
while path(edges, i, i):
cyclic_path = path(edges, i, i)
u = random.randint(0, len(cyclic_path)-1)
edges[cyclic_path[u], cyclic_path[(u+1) % len(cyclic_path)]] = 0
assert not is_connected(edges, i, i)
return [k, colours, edges], f"f(n_{k}_{i})"
class GraphColouring:
@staticmethod
def gen_pos(k):
colours, edges = gen_graph(min_node=2)
[i, j] = random.sample(list(range(len(edges))), 2)
edges[i][j] = 1
colours[j] = colours[i]
return [k, colours, edges], f"f(n_{k}_{i})"
@staticmethod
def gen_neg(k):
while True:
colours, edges = gen_graph(min_node=2)
[i] = random.sample(list(range(len(edges))), 1)
bad_neighbors = [j for j in range(len(edges)) if edges[i][j] and colours[j] == colours[i]]
if not bad_neighbors:
return [k, colours, edges], f"f(n_{k}_{i})"
class TwoChildren:
@staticmethod
def gen_pos(k):
colours, edges = gen_graph(min_node=3)
[i, j, l] = random.sample(list(range(len(edges))), 3)
edges[i][j] = 1
edges[i][l] = 1
return [k, colours, edges], f"f(n_{k}_{i})"
@staticmethod
def gen_neg(k):
while True:
colours, edges = gen_graph(min_node=3)
[i] = random.sample(list(range(len(edges))), 1)
if sum(edges[i]) <= 1:
return [k, colours, edges], f"f(n_{k}_{i})"
class UndirectedEdge:
@staticmethod
def gen_pos(k):
colours, edges = gen_graph(min_node=2)
[i, j] = random.sample(list(range(len(edges))), 2)
x = random.random()
if x > 0.5:
edges[i][j] = 1
else:
edges[j][i] = 1
return [k, colours, edges], f"f(n_{k}_{i}, n_{k}_{j})"
@staticmethod
def gen_neg(k):
while True:
colours, edges = gen_graph(min_node=2)
[i, j] = random.sample(list(range(len(edges))), 2)
if not edges[i][j] and not edges[j][i]:
return [k, colours, edges], f"f(n_{k}_{i}, n_{k}_{j})"
def bk_exs(bk_data):
bk = ""
[name, colours, edges] = bk_data
for i, c in enumerate(colours):
bk += f"colour(n_{name}_{i},{c}).\n"
for i in range(len(edges)):
for j in range(len(edges)):
if edges[i][j]:
bk += f"edge(n_{name}_{i},n_{name}_{j}).\n"
return bk