-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphlet_kernel.py
More file actions
294 lines (260 loc) · 9.32 KB
/
Copy pathgraphlet_kernel.py
File metadata and controls
294 lines (260 loc) · 9.32 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import math
from functools import reduce
from collections import defaultdict
from itertools import combinations, permutations
import torch
import numpy as np
class GraphLet:
def __init__(self, num_v, e_list=None):
self.num_v = num_v
self.e_list = e_list
self.adj_list = defaultdict(set)
if e_list:
for u, v in e_list:
self.adj_list[u].add(v)
self.adj_list[v].add(u)
self.num_e = len(e_list)
@staticmethod
def from_adj_list(adj_list):
_g = GraphLet(len(adj_list))
_g.adj_list = adj_list
_g.num_e = sum(len(v) for v in adj_list.values()) // 2
return _g
def sub(self, sub_v):
v_map = {v: i for i, v in enumerate(sub_v)}
sub_adj_list = {
v_map[v]: set([v_map[u] for u in self.adj_list[v] & sub_v]) for v in sub_v
}
return GraphLet.from_adj_list(sub_adj_list)
def permute_adj(self, perm):
v_map = {v: i for i, v in enumerate(perm)}
perm_adj_list = {
v_map[v]: set([v_map[u] for u in self.adj_list[v]])
for v in range(self.num_v)
}
return perm_adj_list
def is_isomorphic(self, g):
if self.num_v != g.num_v or self.num_e != g.num_e:
return False
for perm_v in permutations(range(self.num_v)):
if self.permute_adj(perm_v) == g.adj_list:
return True
return False
class GraphletSampling:
def __init__(self, k=5, normalize=True, sampling=None, symmetric=True):
self.normalize = normalize
if sampling is None:
def draw_graphlet(g: GraphLet):
for s in ConSubg(g.adj_list, k, symmetric):
yield g.sub(s)
else:
a_map = {1: 1, 2: 2, 3: 4, 4: 8, 5: 19, 6: 53, 7: 209, 8: 1253, 9: 13599}
delta = sampling.get("delta", 0.05)
epsilon = sampling.get("epsilon", 0.05)
a = a_map[k]
self.n_samples = math.ceil(
2 * (a * np.log10(2) + np.log10(1 / delta)) / (epsilon**2) / 200
)
def draw_graphlet(g: GraphLet):
cur_v = min(g.num_v, k)
s = list(range(g.num_v))
for _ in range(self.n_samples):
_idx = set(np.random.choice(s, cur_v, replace=False))
yield g.sub(_idx)
self.draw_graphlet = draw_graphlet
self._graph_bins = list()
def count2mat(self, count):
row_idx, col_idx, data = [], [], []
for idx, g in enumerate(count):
for lbl, cnt in g.items():
row_idx.append(idx)
col_idx.append(lbl)
data.append(cnt)
return (
torch.sparse_coo_tensor(
torch.tensor([row_idx, col_idx]),
torch.tensor(data),
size=(len(count), len(self._graph_bins)),
)
.coalesce()
.float()
)
def fit_transform(self, g_list):
self._count = [defaultdict(int) for _ in range(len(g_list))]
for g in g_list:
g["glet"] = GraphLet(g["num_v"], g["dhg"].e[0])
for g_idx, g in enumerate(g_list):
print(f"Processing graph {g_idx}/{len(g_list)}")
for glet in self.draw_graphlet(g["glet"]):
new_glet = True
for glet_idx, glet_bin in enumerate(self._graph_bins):
if glet.is_isomorphic(glet_bin):
self._count[g_idx][glet_idx] += 1
new_glet = False
break
if new_glet:
self._count[g_idx][len(self._graph_bins)] += 1
self._graph_bins.append(glet)
self.train_cnt = self.count2mat(self._count)
self.train_ft = self.train_cnt.mm(self.train_cnt.t()).to_dense()
if self.normalize:
self.train_ft_diag = torch.diag(self.train_ft)
self.train_ft = (
self.train_ft
/ torch.outer(self.train_ft_diag, self.train_ft_diag).sqrt()
)
self.train_ft[torch.isnan(self.train_ft)] = 0
self.train_ft[torch.isinf(self.train_ft)] = 0
return self.train_ft
def transform(self, g_list):
count = [defaultdict(int) for _ in range(len(g_list))]
for g in g_list:
g["glet"] = GraphLet(g["num_v"], g["dhg"].e[0])
for g_idx, g in enumerate(g_list):
for glet in self.draw_graphlet(g["glet"]):
for glet_idx, glet_bin in enumerate(self._graph_bins):
if glet.is_isomorphic(glet_bin):
count[g_idx][glet_idx] += 1
break
test_cnt = self.count2mat(count)
test_ft = test_cnt.mm(self.train_cnt.t()).to_dense()
if self.normalize:
test_ft_diag = torch.diag(test_ft)
test_ft = test_ft / torch.outer(test_ft_diag, self.train_ft_diag).sqrt()
test_ft[torch.isnan(test_ft)] = 0
test_ft[torch.isinf(test_ft)] = 0
return test_ft
# ConSubg from:
# Karakashian, Shant Kirakos et al. “An Algorithm for Generating All Connected Subgraphs with k Vertices of a Graph.” (2013).
def ConSubg(G, k, symmetric):
# G: dict of sets
l = set()
if symmetric:
sG = G
for u in G.keys():
l |= CombinationsWithV(u, k, sG)
sGP = dict()
for v in sG.keys():
if u != v:
sGP[v] = sG[v] - {u}
sG = sGP
else:
for u in G.keys():
l |= CombinationsWithV(u, k, G)
return l
def CombinationsWithV(u, k, G_init):
l = list()
tree = defaultdict(set)
treeL = {0: u}
MarkN = dict()
def CombinationTree(u, k, G):
root = u
l = [set() for i in range(k)]
l[0].add(u)
MarkV = dict()
def BuildTree(nt, depth, k):
# globals l, MarkN, MarkV, tree
l[depth] = set(l[depth - 1])
for v in G[treeL[nt]]:
if v != nt and v not in l[depth]:
ntp = len(treeL)
treeL[ntp] = v
tree[nt].add(ntp)
l[depth].add(v)
if not MarkV.get(v, False):
MarkN[ntp], MarkV[v] = True, True
else:
MarkN[ntp] = False
if depth + 1 <= k - 1:
BuildTree(ntp, depth + 1, k)
BuildTree(0, 1, k)
def unionProduct(S1, S2):
# globals tree, MarkN
# print("To compare", S1, S2)
if not len(S1):
return set()
elif not len(S2):
return {S1}
else:
return {
s1 | s2
for s1 in S1
for s2 in S2
for s1p, s2p in [({treeL[i] for i in s1}, {treeL[i] for i in s2})]
if not len(s1p & {treeL[i] for i in s2})
and (
any(MarkN[j] for j in s2)
or all(not len({treeL[j] for j in tree[i]} & s2p) for i in s1)
)
}
# Memoization
CFM = dict()
def CombinationsFromTree(root, k):
# Globals tree
t = root
lnodesets = set()
if k == 1:
return {frozenset({t})}
for i in range(1, min(len(tree[t]), k - 1) + 1):
for NodeComb in combinations(tree[t], i):
for string in compositions(k - 1, i):
fail = False
S = list()
for pos in range(i):
stRoot = NodeComb[pos]
size = string[pos]
m = CFM.get((stRoot, size), None)
if m is None:
m = CFM[stRoot, size] = CombinationsFromTree(stRoot, size)
S.append(m)
if not len(S[-1]):
fail = True
break
if fail:
continue
for combProduct in reduce(unionProduct, S):
lnodesets.add(frozenset(combProduct | {t}))
return lnodesets
CombinationTree(u, k, G_init)
return {frozenset({treeL[f] for f in fs}) for fs in CombinationsFromTree(0, k)}
def compositions(n, k):
if n < 0 or k < 0:
return
elif k == 0:
if n == 0:
yield []
return
elif k == 1:
yield [n]
return
else:
for i in range(1, n):
for comp in compositions(n - i, k - 1):
yield [i] + comp
if __name__ == "__main__":
adj_list = {
0: {1, 5},
1: {0, 2},
2: {1, 3, 13},
3: {9, 2, 4},
4: {3, 5},
5: {0, 4, 6},
6: {8, 5, 7},
7: {6},
8: {6},
9: {11, 10, 3},
10: {9},
11: {9, 12, 13},
12: {11},
13: {2, 11},
}
g = GraphLet.from_adj_list(adj_list)
for s in ConSubg(g.adj_list, 3, True):
print(s)
print(g.sub(s).adj_list)
print("Testing is_isomorphic")
g1 = GraphLet(4, [(0, 1), (0, 2), (0, 3)])
g2 = GraphLet(4, [(3, 1), (3, 2), (3, 0)])
g3 = GraphLet(4, [(0, 1), (0, 2), (0, 3), (1, 2)])
print(g1.is_isomorphic(g2))
print(g1.is_isomorphic(g3))