-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSE.py
More file actions
298 lines (260 loc) · 11.7 KB
/
Copy pathSE.py
File metadata and controls
298 lines (260 loc) · 11.7 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
295
296
297
298
import networkx as nx
from networkx.algorithms import cuts
import math
from itertools import chain
class SE:
def __init__(self, graph: nx.Graph):
self.graph = graph.copy()
self.vol = self.get_vol()
self.division = {} # {comm1: [node11, node12, ...], comm2: [node21, node22, ...], ...}
self.struc_data = {} # {comm1: [vol1, cut1, community_node_SE, leaf_nodes_SE], comm2:[vol2, cut2, community_node_SE, leaf_nodes_SE],... }
self.struc_data_2d = {} # {comm1: {comm2: [vol_after_merge, cut_after_merge, comm_node_SE_after_merge, leaf_nodes_SE_after_merge], comm3: [], ...}, ...}
def get_vol(self):
'''
get the volume of the graph
'''
return cuts.volume(self.graph, self.graph.nodes, weight = 'weight')
def calc_1dSE(self):
'''
get the 1D SE of the graph
'''
SE = 0
for n in self.graph.nodes:
d = cuts.volume(self.graph, [n], weight = 'weight')
SE += - (d / self.vol) * math.log2(d / self.vol)
return SE
def update_1dSE(self, original_1dSE, new_edges):
'''
get the updated 1D SE after new edges are inserted into the graph
'''
affected_nodes = []
for edge in new_edges:
affected_nodes += [edge[0], edge[1]]
affected_nodes = set(affected_nodes)
original_vol = self.vol
original_degree_dict = {node:0 for node in affected_nodes}
for node in affected_nodes.intersection(set(self.graph.nodes)):
original_degree_dict[node] = self.graph.degree(node, weight = 'weight')
# insert new edges into the graph
self.graph.add_weighted_edges_from(new_edges)
self.vol = self.get_vol()
updated_vol = self.vol
updated_degree_dict = {}
for node in affected_nodes:
updated_degree_dict[node] = self.graph.degree(node, weight = 'weight')
updated_1dSE = (original_vol / updated_vol) * (original_1dSE - math.log2(original_vol / updated_vol))
for node in affected_nodes:
d_original = original_degree_dict[node]
d_updated = updated_degree_dict[node]
if d_original != d_updated:
if d_original != 0:
updated_1dSE += (d_original / updated_vol) * math.log2(d_original / updated_vol)
updated_1dSE -= (d_updated / updated_vol) * math.log2(d_updated / updated_vol)
return updated_1dSE
def get_cut(self, comm):
'''
get the sum of the degrees of the cut edges of community comm
'''
return cuts.cut_size(self.graph, comm, weight = 'weight')
def get_volume(self, comm):
'''
get the volume of community comm
'''
return cuts.volume(self.graph, comm, weight = 'weight')
def calc_2dSE(self):
'''
get the 2D SE of the graph
'''
SE = 0
for comm in self.division.values():
g = self.get_cut(comm)
v = self.get_volume(comm)
SE += - (g / self.vol) * math.log2(v / self.vol)
for node in comm:
d = self.graph.degree(node, weight = 'weight')
SE += - (d / self.vol) * math.log2(d / v)
return SE
def show_division(self):
print(self.division)
def show_struc_data(self):
print(self.struc_data)
def show_struc_data_2d(self):
print(self.struc_data_2d)
def print_graph(self):
fig, ax = plt.subplots()
nx.draw(self.graph, ax=ax, with_labels=True)
plt.show()
def update_struc_data(self):
'''
calculate the volume, cut, communitiy mode SE, and leaf nodes SE of each cummunity,
then store them into self.struc_data
'''
self.struc_data = {} # {comm1: [vol1, cut1, community_node_SE, leaf_nodes_SE], comm2:[vol2, cut2, community_node_SE, leaf_nodes_SE],... }
for vname in self.division.keys():
comm = self.division[vname]
volume = self.get_volume(comm)
cut = self.get_cut(comm)
if volume == 0:
vSE = 0
else:
vSE = - (cut / self.vol) * math.log2(volume / self.vol)
vnodeSE = 0
for node in comm:
d = self.graph.degree(node, weight = 'weight')
if d != 0:
vnodeSE -= (d / self.vol) * math.log2(d / volume)
self.struc_data[vname] = [volume, cut, vSE, vnodeSE]
def update_struc_data_2d(self):
'''
calculate the volume, cut, communitiy mode SE, and leaf nodes SE after merging each pair of cummunities,
then store them into self.struc_data_2d
'''
self.struc_data_2d = {} # {(comm1, comm2): [vol_after_merge, cut_after_merge, comm_node_SE_after_merge, leaf_nodes_SE_after_merge], (comm1, comm3): [], ...}
comm_num = len(self.division)
for i in range(comm_num):
for j in range(i + 1, comm_num):
v1 = list(self.division.keys())[i]
v2 = list(self.division.keys())[j]
if v1 < v2:
k = (v1, v2)
else:
k = (v2, v1)
comm_merged = self.division[v1] + self.division[v2]
gm = self.get_cut(comm_merged)
vm = self.struc_data[v1][0] + self.struc_data[v2][0]
if self.struc_data[v1][0] == 0 or self.struc_data[v2][0] == 0:
vmSE = self.struc_data[v1][2] + self.struc_data[v2][2]
vmnodeSE = self.struc_data[v1][3] + self.struc_data[v2][3]
else:
vmSE = - (gm / self.vol) * math.log2(vm / self.vol)
vmnodeSE = self.struc_data[v1][3] - (self.struc_data[v1][0]/ self.vol) * math.log2(self.struc_data[v1][0] / vm) + \
self.struc_data[v2][3] - (self.struc_data[v2][0]/ self.vol) * math.log2(self.struc_data[v2][0] / vm)
self.struc_data_2d[k] = [vm, gm, vmSE, vmnodeSE]
def init_division(self):
'''
initialize self.division such that each node assigned to its own community
'''
self.division = {}
for node in self.graph.nodes:
new_comm = node
self.division[new_comm] = [node]
self.graph.nodes[node]['comm'] = new_comm
def add_isolates(self):
'''
add any isolated nodes into graph
'''
all_nodes = list(chain(*list(self.division.values())))
all_nodes.sort()
edge_nodes = list(self.graph.nodes)
edge_nodes.sort()
if all_nodes != edge_nodes:
for node in set(all_nodes)-set(edge_nodes):
self.graph.add_node(node)
def update_division_MinSE(self):
'''
greedily update the encoding tree to minimize 2D SE
'''
def Mg_operator(v1, v2):
'''
MERGE operator. It calculates the delta SE caused by mergeing communities v1 and v2,
without actually merging them, i.e., the encoding tree won't be changed
'''
v1SE = self.struc_data[v1][2]
v1nodeSE = self.struc_data[v1][3]
v2SE = self.struc_data[v2][2]
v2nodeSE = self.struc_data[v2][3]
if v1 < v2:
k = (v1, v2)
else:
k = (v2, v1)
vm, gm, vmSE, vmnodeSE = self.struc_data_2d[k]
delta_SE = vmSE + vmnodeSE - (v1SE + v1nodeSE + v2SE + v2nodeSE)
return delta_SE
# continue merging any two communities that can cause the largest decrease in SE,
# until the SE can't be further reduced
while True:
comm_num = len(self.division)
delta_SE = 99999
vm1 = None
vm2 = None
for i in range(comm_num):
for j in range(i + 1, comm_num):
v1 = list(self.division.keys())[i]
v2 = list(self.division.keys())[j]
new_delta_SE = Mg_operator(v1, v2)
if new_delta_SE < delta_SE:
delta_SE = new_delta_SE
vm1 = v1
vm2 = v2
if delta_SE < 0:
# Merge v2 into v1, and update the encoding tree accordingly
for node in self.division[vm2]:
self.graph.nodes[node]['comm'] = vm1
self.division[vm1] += self.division[vm2]
self.division.pop(vm2)
volume = self.struc_data[vm1][0] + self.struc_data[vm2][0]
cut = self.get_cut(self.division[vm1])
vmSE = - (cut / self.vol) * math.log2(volume / self.vol)
vmnodeSE = self.struc_data[vm1][3] - (self.struc_data[vm1][0]/ self.vol) * math.log2(self.struc_data[vm1][0] / volume) + \
self.struc_data[vm2][3] - (self.struc_data[vm2][0]/ self.vol) * math.log2(self.struc_data[vm2][0] / volume)
self.struc_data[vm1] = [volume, cut, vmSE, vmnodeSE]
self.struc_data.pop(vm2)
struc_data_2d_new = {}
for k in self.struc_data_2d.keys():
if k[0] == vm2 or k[1] == vm2:
continue
elif k[0] == vm1 or k[1] == vm1:
v1 = k[0]
v2 = k[1]
comm_merged = self.division[v1] + self.division[v2]
gm = self.get_cut(comm_merged)
vm = self.struc_data[v1][0] + self.struc_data[v2][0]
if self.struc_data[v1][0] == 0 or self.struc_data[v2][0] == 0:
vmSE = self.struc_data[v1][2] + self.struc_data[v2][2]
vmnodeSE = self.struc_data[v1][3] + self.struc_data[v2][3]
else:
vmSE = - (gm / self.vol) * math.log2(vm / self.vol)
vmnodeSE = self.struc_data[v1][3] - (self.struc_data[v1][0]/ self.vol) * math.log2(self.struc_data[v1][0] / vm) + \
self.struc_data[v2][3] - (self.struc_data[v2][0]/ self.vol) * math.log2(self.struc_data[v2][0] / vm)
struc_data_2d_new[k] = [vm, gm, vmSE, vmnodeSE]
else:
struc_data_2d_new[k] = self.struc_data_2d[k]
self.struc_data_2d = struc_data_2d_new
else:
break
def vanilla_2D_SE_mini(weighted_edges):
'''
vanilla (greedy) 2D SE minimization
'''
g = nx.Graph()
g.add_weighted_edges_from(weighted_edges)
seg = SE(g)
seg.init_division()
#seg.show_division()
SE1D = seg.calc_1dSE()
seg.update_struc_data()
#seg.show_struc_data()
seg.update_struc_data_2d()
#seg.show_struc_data_2d()
initial_SE2D = seg.calc_2dSE()
seg.update_division_MinSE()
communities = seg.division
minimized_SE2D = seg.calc_2dSE()
return SE1D, initial_SE2D, minimized_SE2D, communities
def test_vanilla_2D_SE_mini():
weighted_edges = [(1, 2, 2), (1, 3, 4)]
g = nx.Graph()
g.add_weighted_edges_from(weighted_edges)
A = nx.adjacency_matrix(g).todense()
print('adjacency matrix: \n', A)
print('g.nodes: ', g.nodes)
print('g.edges: ', g.edges)
print('degrees of nodes: ', list(g.degree(g.nodes, weight = 'weight')))
SE1D, initial_SE2D, minimized_SE2D, communities = vanilla_2D_SE_mini(weighted_edges)
print('\n1D SE of the graph: ', SE1D)
print('initial 2D SE of the graph: ', initial_SE2D)
print('the minimum 2D SE of the graph: ', minimized_SE2D)
print('communities detected: ', communities)
return
if __name__ == "__main__":
test_vanilla_2D_SE_mini()