/
partitioner.py
1266 lines (1050 loc) · 37.2 KB
/
partitioner.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ----------------------------------------------------------------------------
# This is an implementation of some Partitioner classes. There is a basic
# Partitioner which just divides nodes randomly into groups of size k. Then
# there is a partitioner based on the algorithm described in
#
# Resisting structural re-identification in anonymized social networks
# Michael Hay, Gerome Miklau, David Jensen, Don Towsley, and Philipp Weis
# Proceedings of the VLDB, 2008
#
# That algorithm uses local search to find a good partition. The search is
# implemented in search.py and the code below implements the search state
# space and associated details (methods for evaluating current search state
# and proposing new states).
# ----------------------------------------------------------------------------
import math
import networkx
import random
import partition
import search
import utils_lite as utils
class Partitioner(object):
def __init__(self, g, k):
self.g = g
self.k = k
self.partitioner_type = 'r'
def name(self):
return self.g.name + "." + str(self.k) + "." + self.partitioner_type + ".partition"
def partition(self):
nodes = self.g.nodes()
random.shuffle(nodes)
part = []
while len(nodes) >= 2*self.k:
part.append( nodes[:self.k] )
del nodes[:self.k]
part.append(nodes)
self.partition = partition.Partition(part)
return self.partition
def save(self, directory):
self.partition.save(directory + self.name())
class MinNumWorldsPartitioner(Partitioner):
def __init__(self, g, k, working_dir, max_steps=None):
Partitioner.__init__(self, g, k)
self.working_dir = working_dir
self.max_steps = max_steps
def name(self):
return self.g.name + "." + str(self.k) + "." + 'p' + ".partition"
def partition(self):
problem = Problem(self.g, self.k, max_steps=self.max_steps)
state = search.annealing(problem, working_dir=self.working_dir)
final_partition = []
for gid in state.get_partitions():
final_partition.append(state.get_nodes(gid))
p = partition.Partition(groups=final_partition)
self.partition = p
return p
debug_sampling = False
debug = False
debug_edge_count = debug
doing_flips = True
two_hop_only = True
doing_mergesplits = True
random_split = False # it really slows down runtime
random_splitmerge = True # it really slows down runtime
init_struct_equivalence = True
class StateChange(object):
def __init__(self):
self.score_change = 0
def get_score_change(self):
return self.score_change
def set_score_change(self, score):
self.score_change = score
class Merge(StateChange):
def __init__(self, i, j):
StateChange.__init__(self)
self.i = i
self.j = j
class MergeSplit(StateChange):
def __init__(self, i, j, g1, g2):
StateChange.__init__(self)
self.i = i
self.j = j
self.g1 = g1
self.g2 = g2
class Split(StateChange):
def __init__(self, partition_idx, sub_partition_1, sub_partition_2):
StateChange.__init__(self)
self.partition_idx = partition_idx
self.sub_partition_1 = sub_partition_1
self.sub_partition_2 = sub_partition_2
class Flip(StateChange):
def __init__(self, node, i, j):
StateChange.__init__(self)
self.node = node
self.i = i
self.j = j
class NoOp(StateChange):
def __init__(self):
StateChange.__init__(self)
class State(object):
# state consists:
# - G, input graph
# - a mapping of partition key to set of nodes in partition
# - a mapping from node to partition
# - a partition graph where edge between i,j reveals number
# of edges in G between nodes in i to nodes in j
def __init__(self, g, partition=None, min_size=None):
self.g = g
if partition != None:
self.partition = {}
self.nodes = {}
self.next_key = 0
for group in partition:
self.partition[self.next_key] = group[:]
for member in group:
self.nodes[member] = self.next_key
self.next_key += 1
elif not init_struct_equivalence:
self.partition = { 0: g.nodes() }
self.nodes = {}
for node in g.nodes():
self.nodes[node] = 0
self.next_key = 1
else:
assert min_size != None and type(min_size) == int
eq_classes = utils.structural_equivalence(self.g)
eq_classes = [ (len(i), i) for i in eq_classes ]
eq_classes.sort()
eq_classes.reverse()
self.partition = {}
self.nodes = {}
curr_key = 0
self.partition[curr_key] = []
remaining_nodes = g.number_of_nodes()
for (length, eq_class) in eq_classes:
# only create a new partition if:
# - current partition is big enough
# - current structural eq. class is larger than one
# - you have enough nodes to fill another partition
if len(self.partition[curr_key]) >= min_size and length > 1 and remaining_nodes >= min_size:
curr_key += 1
self.partition[curr_key] = []
self.partition[curr_key] += eq_class[:]
for u in eq_class:
self.nodes[u] = curr_key
self.next_key = curr_key + 1
assert min(map(len, self.partition.values())) >= min_size
self.free_keys = []
self.likelihood_cache = {}
def __add_edge(self, i, j, count):
if count > 0:
self.partition_graph.add_edge(i,j,key=0,count=count)
elif self.partition_graph.has_edge(i,j):
self.partition_graph.remove_edge(i,j)
assert not self.partition_graph.has_edge(i,j)
def __add_node(self, i):
self.partition_graph.add_node(i)
def __remove_edges(self, i):
self.partition_graph.remove_edges_from(self.partition_graph.edges(i))
def __delete_node(self, i):
self.partition_graph.delete_node(i)
def __get_edge(self, i, j):
count = self.partition_graph.get_edge_data(i,j,key=0)["count"]
assert count > 0
return count
def __has_edge(self, i, j):
has_edge = self.partition_graph.has_edge(i,j)
if has_edge:
assert self.partition_graph.get_edge_data(i,j,key=0)["count"] > 0
return has_edge
def __make_partition(self, nodes):
if self.free_keys:
key = self.free_keys.pop()
else:
key = self.next_key
self.next_key += 1
self.partition[key] = nodes[:]
return key
def apply_change(self, state_change):
if isinstance(state_change, Split):
idx = state_change.partition_idx
part = self.partition[idx]
self.partition[idx] = state_change.sub_partition_1
new_idx = self.__make_partition(state_change.sub_partition_2)
for node in state_change.sub_partition_2:
self.nodes[node] = new_idx
nbrs = self.neighbors(idx)
self.__remove_edges(idx)
self.__add_node(new_idx)
# update likelihood cache
for nbr in nbrs + [idx]:
if nbr in self.likelihood_cache:
del self.likelihood_cache[nbr]
edge_count = self.edge_count(state_change.sub_partition_1, state_change.sub_partition_2)
if edge_count > 0:
self.__add_edge(idx, new_idx, edge_count)
for (idx, group) in [ (idx, state_change.sub_partition_1), \
(new_idx, state_change.sub_partition_2)]:
edge_count = self.edge_count(group)
self.__add_edge(idx, idx, edge_count)
for j in nbrs:
edge_count = self.edge_count(group, self.get_nodes(j))
self.__add_edge(idx, j, edge_count)
elif isinstance(state_change, Merge):
i = state_change.i
j = state_change.j
# compute ll of new partition of ij merged
edges_ii = self.edge_count(i)
edges_jj = self.edge_count(j)
edges_ij = self.edge_count(i,j)
edges_ii = edges_ii + edges_jj + edges_ij
self.__add_edge(i, i, edges_ii)
# compute ll of new partition with all neighbors of i and j
nbrs = self.neighbors([i,j])
for k in nbrs:
edges_ik = self.edge_count(i,k) + self.edge_count(j,k)
self.__add_edge(i, k, edges_ik)
# update likelihood cache
for nbr in nbrs.union([i,j]):
if nbr in self.likelihood_cache:
del self.likelihood_cache[nbr]
self.partition[i] = self.partition[i] + self.partition[j]
self.__delete_node(j)
for node in self.partition[j]:
self.nodes[node] = i
del self.partition[j]
self.free_keys.append(j)
elif isinstance(state_change, MergeSplit):
i = state_change.i
j = state_change.j
g1 = state_change.g1
g2 = state_change.g2
nbrs = self.neighbors([i,j])
# update likelihood cache
for nbr in nbrs.union([i,j]):
if nbr in self.likelihood_cache:
del self.likelihood_cache[nbr]
edge_count = self.edge_count(g1, g2)
self.__add_edge(i, j, edge_count)
for (idx, grp) in [ (i, g1), (j, g2) ]:
self.__add_edge(idx, idx, self.edge_count(grp))
for k in nbrs:
edge_count = self.edge_count(grp, self.get_nodes(k))
self.__add_edge(idx, k, edge_count)
self.partition[i] = g1
self.partition[j] = g2
for node in g1:
self.nodes[node] = i
for node in g2:
self.nodes[node] = j
elif isinstance(state_change, Flip):
u = state_change.node
i = state_change.i
j = state_change.j
assert self.nodes[u] == i and u in self.partition[i], "Flipped node not in partition"
edges_ii = self.edge_count(i)
edges_jj = self.edge_count(j)
edges_ij = self.edge_count(i,j)
edges_ui = self.edge_count([u], self.get_nodes(i))
edges_uj = self.edge_count([u], self.get_nodes(j))
self.__add_edge(i, i, edges_ii - edges_ui)
self.__add_edge(j, j, edges_jj + edges_uj)
self.__add_edge(i, j, edges_ij + edges_ui - edges_uj)
nbrs = self.neighbors(i)
for k in nbrs:
if k == j:
continue # handled above
nodes_k = self.get_nodes(k)
edges_uk = self.edge_count([u], nodes_k)
edges_ik = self.edge_count(i,k) - edges_uk
self.__add_edge(i, k, edges_ik)
edges_jk = self.edge_count(j,k) + edges_uk
self.__add_edge(j, k, edges_jk)
for nbr in self.neighbors([i,j]).union([i,j]):
if nbr in self.likelihood_cache:
del self.likelihood_cache[nbr]
self.partition[i].remove(u)
self.partition[j].append(u)
self.nodes[u] = j
elif not isinstance(state_change, NoOp):
assert False, "Unrecognized change %s" % (state_change.__class__)
# SANITY CHECKS OF STATE:
# - state data structure represents a valid state:
# - 1. every node in G has been assigned to exactly one partition
# - 2. each partition has at least k nodes
# - 3. the nodes in each partition appear in G
# - 4. the partition graph matches the partitions (nodes of V equals the set of partitions)
# - 5. the partition graph matches input graph G:
# - edge count between partitions i and j == no. of edges in G between nodes of i and nodes of j
# - 6. the score of the state matches likelihood when computed from scratch
# - cached edge counts are accurate
def check_state(self, min_size=None):
# check nodes of G are all in partition assignment
V = set(self.g.nodes())
nodes = set(self.nodes.keys())
assert V == nodes
# check each nodes's partition contains the node
assigned_partitions = set([])
for u in V:
i = self.nodes[u]
assert u in set(self.partition[i])
assigned_partitions.add(i)
assert assigned_partitions == set(self.partition.keys())
# check that the set of nodes in partitioning is exactly V
tmp = reduce(lambda x,y: x+y, self.partition.values())
nodes = set(tmp)
assert len(tmp) == len(nodes)
assert V == nodes
# check min size of partitions
partition_sizes = map(len, self.partition.values())
if min_size != None:
assert min(partition_sizes) >= min_size
assert sum(partition_sizes) == len(V)
assert set(self.partition_graph.nodes()) == set(self.partition.keys())
for i in self.partition_graph:
for j in self.partition_graph:
if i < j:
continue
if i == j:
nodes_i = set(self.partition[i])
neighbors = []
for u in nodes_i:
neighbors += self.g.neighbors(u)
count = 0
for nbr in neighbors:
count += nbr in nodes_i
count /= 2 # each edge is double counted
else:
nodes_i = self.partition[i]
nodes_j = self.partition[j]
count = len(networkx.edge_boundary(self.g, nodes_i, nodes_j))
if self.partition_graph.has_edge(i,j):
stored_count = self.partition_graph.get_edge_data(i,j,key=0)["count"]
assert count == stored_count, \
"Mismatch: edges(%d,%d)=%d stored count=%d" % (i,j,count, stored_count)
else:
assert count == 0, \
"Mismatch: edges(%d,%d)=%d but no count stored" % (i,j, count)
def choose_random_node(self):
return random.choice(self.nodes.keys())
def copy(self):
return State(self.g, partition=self.partition.values())
def edge_count(self, i, j=None):
self_loop = False
assert i != j, "Should not be equal"
if j == None:
self_loop = True
j = i
if type(i) == type(0):
num_edges = 0
if self.__has_edge(i,j):
num_edges = self.__get_edge(i,j)
if debug_edge_count:
if self_loop:
num_edges_check = self.edge_count(self.get_nodes(i))
else:
num_edges_check = self.edge_count(self.get_nodes(i), self.get_nodes(j))
assert num_edges == num_edges_check, "Count mismatch (" + str(i) + "," + str(j) + "): returns " + \
str(num_edges) + " but is " + str(num_edges_check)
return num_edges
# otherwise, compute number between groups i and j
if len(i) == 0 or len(j) == 0:
return 0
if len(i) > len(j):
tmp = i
i = j
j = tmp
# SORTING SEEMS TO BE SLOWER
# neighbors = reduce(lambda x,y: x + y, map(self.adj_list.get, i))
# neighbors.sort()
# j.sort()
# count = 0
# n_idx = 0
# n = len(neighbors)
# for u in j:
# while n_idx < n and neighbors[n_idx] < u:
# n_idx += 1
# while n_idx < n and neighbors[n_idx] == u:
# n_idx += 1
# count += 1
# edge_count = count
j = set(j)
neighbors = reduce(lambda x,y: x + y, map(self.adj_list.get, i))
edge_count = reduce(lambda total, nbr: total + (nbr in j), neighbors, 0)
if self_loop:
edge_count = edge_count / 2 # every edge is double counted
assert edge_count >= 0
return edge_count
def get_likelihood_partition(self, i):
return self.likelihood_cache[i]
def get_partition_of_node(self, u):
return self.nodes[u]
def get_partitions(self):
return self.partition.keys()
def get_partition_map(self):
self.initialize()
nodes_to_group = {}
for gid in self.partition:
for node in self.partition[gid]:
nodes_to_group[node] = gid
return nodes_to_group
def get_nodes(self, i):
return self.partition[i]
def has_likelihood_partition(self, i):
return i in self.likelihood_cache
def initialize(self):
self.adj_list = {}
for u in self.g:
self.adj_list[u] = self.g.neighbors(u)[:]
self.partition_graph = networkx.MultiGraph()
# for i in self.partition:
# self.__add_node(i)
# for j in self.partition:
# if i < j:
# continue
# elif i == j:
# edge_count = self.edge_count(self.partition[i])
# else:
# edge_count = self.edge_count(self.partition[i], self.partition[j])
# self.__add_edge(i,j,edge_count)
# a faster way, initialize by iterating over edges of G
for i in self.partition:
self.__add_node(i)
for (u,v) in self.g.edges():
i = self.nodes[u]
j = self.nodes[v]
if self.partition_graph.has_edge(i,j):
count = self.partition_graph.get_edge_data(i,j,key=0)["count"] + 1
else:
count = 1
self.partition_graph.add_edge(i,j,key=0,count=count)
if debug:
self.check_state()
def neighbors(self, i):
if type(i) == type([]):
nbrs = []
for idx in i:
nbrs += self.partition_graph.neighbors(idx)
nbrs = set(nbrs)
nbrs.difference_update(i)
return nbrs
else:
assert type(i) == type(0) or type(i) == type('s')
nbrs = self.partition_graph.neighbors(i)
if i in nbrs:
nbrs.remove(i)
return nbrs
def neighbors_two_hop(self, i):
"Returns neighbors and neighbors' neighbors excluding i"
nbrs = []
for nbr in self.partition_graph.neighbors(i):
nbrs.append(nbr)
nbrs2 = []
for nbr in nbrs:
nbrs2 += self.partition_graph.neighbors(nbr)
nbrs = set(nbrs + nbrs2)
nbrs.discard(i)
return nbrs
def node_count(self, i):
return len(self.get_nodes(i))
def num_partitions(self):
return len(self.partition)
def set_likelihood_partition(self, i, l):
self.likelihood_cache[i] = l
class Problem(object):
def __init__(self, g, k=None, max_steps=None, start_state=None):
self.n = g.number_of_nodes()
# parameters
self.k = k
self.max_steps = 200 * max(self.n, 100)
self.start_temp = 1000
self.alpha = math.exp( 1./(self.max_steps) * math.log(.0001/self.start_temp ))
if max_steps != None:
self.max_steps = max_steps
self.cycle_length = 5 * self.n
self.min_changes_per_cycle = int(round(.10 * self.n)) # 10% of nodes must flip per cycle
self.split_frequency = 100
self.num_changes = 0
self.score = None
if start_state == None:
self.__start_state(g)
else:
self.state = start_state
self.state.initialize()
# logging
self.num_noops = 0
self.num_splits = 0
self.num_merges = 0
self.num_mergesplits = 0
self.num_flips = 0
self.cache_hit = 0
self.cache_tries = 0
self.nbr_merge = 0
self.nbr_nbr_merge = 0
self.nonnbr_merge = 0
def __likelihood(self):
ll = 0
for i in self.state.get_partitions():
edges_ii = self.state.edge_count(i)
nodes_i = self.state.node_count(i)
ll += self.__likelihood_partition_pair(edges_ii, nodes_i)
for j in self.state.get_partitions():
if i <= j:
continue
edges_ij = self.state.edge_count(i,j)
nodes_j = self.state.node_count(j)
ll += self.__likelihood_partition_pair(edges_ij, nodes_i, nodes_j)
return ll
def __likelihood_flip(self, u, i, j):
ll = 0
edges_ii = self.state.edge_count(i)
edges_jj = self.state.edge_count(j)
edges_ij = self.state.edge_count(i,j)
nodes_i = self.state.node_count(i)
nodes_j = self.state.node_count(j)
edges_ui = self.state.edge_count([u], self.state.get_nodes(i))
edges_uj = self.state.edge_count([u], self.state.get_nodes(j))
ll += self.__likelihood_partition_pair(edges_ii - edges_ui, nodes_i - 1)
ll += self.__likelihood_partition_pair(edges_jj + edges_uj, nodes_j + 1)
ll += self.__likelihood_partition_pair(edges_ij - edges_uj + edges_ui, nodes_i - 1, nodes_j + 1)
# recompute ll of partition i and j with all neighbors of i and j
for k in self.state.neighbors([i,j]):
nodes_k = self.state.get_nodes(k)
edges_uk = self.state.edge_count([u],nodes_k)
edges_ik = self.state.edge_count(i,k)
ll += self.__likelihood_partition_pair(edges_ik - edges_uk, nodes_i - 1, len(nodes_k))
edges_jk = self.state.edge_count(j,k)
ll += self.__likelihood_partition_pair(edges_jk + edges_uk, nodes_j + 1, len(nodes_k))
return ll
def __likelihood_merge(self, i, j):
ll = 0
# compute ll of new partition of ij merged
edges_ii = self.state.edge_count(i)
edges_jj = self.state.edge_count(j)
edges_ij = self.state.edge_count(i,j)
edges = edges_ii + edges_jj + edges_ij
nodes = self.state.node_count(i) + self.state.node_count(j)
ll += self.__likelihood_partition_pair(edges, nodes)
# compute ll of new partition with all neighbors of i and j
for k in self.state.neighbors([i,j]):
edges = self.state.edge_count(i,k) + self.state.edge_count(j,k)
nodes_k = self.state.node_count(k)
ll += self.__likelihood_partition_pair(edges, nodes, nodes_k)
return ll
def __likelihood_merge_and_split(self, i, j, g1, g2):
ll_change = 0
# compute likelihood of edges in g1, edges in g2 and edges between g1 and g2
edges_11 = self.state.edge_count(g1)
edges_12 = self.state.edge_count(g1, g2)
edges_22 = self.state.edge_count(g2)
ll_change += self.__likelihood_partition_pair(edges_11, len(g1))
ll_change += self.__likelihood_partition_pair(edges_12, len(g1), len(g2))
ll_change += self.__likelihood_partition_pair(edges_22, len(g2))
# compute likelihood with neighbors of i and j
nbrs = self.state.neighbors([i,j])
for sub_partition in [g1, g2]:
for k in nbrs:
nodes_k = self.state.get_nodes(k)
edges_sk = self.state.edge_count(sub_partition, nodes_k)
ll_change += self.__likelihood_partition_pair(edges_sk, len(sub_partition), len(nodes_k))
return ll_change
def __likelihood_partition(self, i):
self.cache_tries += 1
if self.state.has_likelihood_partition(i):
self.cache_hit += 1
return self.state.get_likelihood_partition(i)
ll = 0
nodes_i = self.state.node_count(i)
edges_ii = self.state.edge_count(i)
ll += self.__likelihood_partition_pair(edges_ii, nodes_i)
for j in self.state.neighbors(i):
edges_ij = self.state.edge_count(i,j)
nodes_j = self.state.node_count(j)
ll += self.__likelihood_partition_pair(edges_ij, nodes_i, nodes_j)
self.state.set_likelihood_partition(i, ll)
return ll
def __likelihood_partition_pair(self, edge_count, n, m=None):
if m == None:
num_slots = n * (n-1) / 2
else:
num_slots = n * m
if edge_count == 0 or edge_count == num_slots:
return 0
assert edge_count < num_slots and edge_count > 0, "edge count=%d slots=%d" % (edge_count, num_slots)
p = 1. * edge_count / num_slots
l = num_slots * (p * math.log(p) + (1-p) * math.log(1-p))
assert str(l) != 'nan'
return l
def __likelihood_split(self, split_idx, part1, part2):
ll_change = 0
# compute score between subpartitions
edges_12 = self.state.edge_count(part1, part2)
ll_change += self.__likelihood_partition_pair(edges_12, len(part1), len(part2))
assert str(ll_change) != "nan"
# for each subpartition, compute its score and its score to the other partitions
for sub_partition in [part1, part2]:
edges_ii = self.state.edge_count(sub_partition)
ll_change += self.__likelihood_partition_pair(edges_ii, len(sub_partition))
assert str(ll_change) != "nan"
for i in self.state.neighbors(split_idx):
nodes_i = self.state.get_nodes(i)
edges_ij = self.state.edge_count(sub_partition, nodes_i)
ll_change += self.__likelihood_partition_pair(edges_ij, len(sub_partition), len(nodes_i))
assert str(ll_change) != "nan"
return ll_change
def __start_state(self, graph):
self.state = State(graph, min_size=self.k)
self.state.initialize()
self.score = self.__likelihood()
def apply_noop_change(self):
self.apply_change(NoOp())
def apply_change(self, state_change):
if isinstance(state_change, Merge):
i = state_change.i
j = state_change.j
nbrs = self.state.neighbors(j)
if i in nbrs:
self.nbr_merge += 1
else:
# is i a neighbor of j's neighbor?
found = False
for nbr in nbrs:
if i in self.state.neighbors(nbr):
found = True
break
if found:
self.nbr_nbr_merge += 1
else:
self.nonnbr_merge += 1
self.state.apply_change(state_change)
if isinstance(state_change, NoOp):
self.num_noops += 1
else:
self.num_changes += 1
if isinstance(state_change, Split):
self.num_splits += 1
elif isinstance(state_change, Merge):
self.num_merges += 1
elif isinstance(state_change, MergeSplit):
self.num_mergesplits += 1
elif isinstance(state_change, Flip):
self.num_flips += 1
else:
assert False, "Unrecognized change"
self.score += state_change.get_score_change()
def __check_likelihood(self):
# check score matches actual likelihood
temp_problem = Problem(self.state.g, self.k)
temp_problem.set_state(self.state.copy())
temp_score = temp_problem.get_score()
assert abs(self.score - temp_score) < 0.0000000001, \
"Actual score (%g) does not match reported score (%g)" % (temp_score, self.score)
# check values in cache
for i in self.state.get_partitions():
if self.state.has_likelihood_partition(i):
cached_ll = self.state.get_likelihood_partition(i)
# must get appropriate partition in temp problem
u = self.state.get_nodes(i)[0]
ll = temp_problem.__likelihood_partition(temp_problem.state.get_partition_of_node(u))
assert abs(cached_ll - ll) < 0.0000000001, \
"Invalid likelihood cache for partition i=%d: %g vs. cached %g" % (i, ll, cached_ll)
def copy_state(self):
"Copy state used for saving the max state"
return self.state.copy()
def get_current_state(self):
return self.state
def get_score(self):
if self.score == None:
self.score = self.__likelihood()
return self.score
def merge_partition(self, i):
successors = []
if two_hop_only:
candidate_partitions = self.state.neighbors_two_hop(i)
else:
candidate_partitions = self.state.get_partitions()
for j in candidate_partitions:
if i == j:
continue
successors.append(Merge(i,j))
return successors
def merge_and_split_partition(self, i):
# first find best merge
successors = []
if two_hop_only:
candidate_partitions = self.state.neighbors_two_hop(i)
else:
candidate_partitions = self.state.get_partitions()
if len(candidate_partitions) == 0:
return []
for j in candidate_partitions:
if i == j:
continue
successors.append(Merge(i,j))
scores = []
for merge in successors:
score = self.score_change(merge)
assert score != None and str(score) != 'nan'
scores.append(score)
merge_op = successors[utils.get_max(scores)]
j = merge_op.j
# then find best split of a merged i,j
nodes = self.state.get_nodes(i)[:] + self.state.get_nodes(j)[:]
g1 = nodes
g2 = []
g1_edges = {}
g2_edges = {}
edges_11 = self.state.edge_count(i) + self.state.edge_count(j) + self.state.edge_count(i,j)
edges_12 = 0
edges_22 = 0
nbrs = self.state.neighbors([i,j])
for k in nbrs:
g1_edges[k] = self.state.edge_count(i, k) + self.state.edge_count(j, k)
g2_edges[k] = 0
while len(g2) < self.k:
# find best node from g1 to move to g2
max = None
argmax = None
if random_splitmerge and len(g2) == 0:
argmax = random.choice(g1)
else:
for node in g1:
ll = 0
# compute change in score between g1 and g2 if we move node to g2
edges_node_1 = self.state.edge_count([node], g1)
edges_node_2 = self.state.edge_count([node], g2)
tmp_edges_11 = edges_11 - edges_node_1
tmp_edges_12 = edges_12 - edges_node_2 + edges_node_1
tmp_edges_22 = edges_22 + edges_node_2
ll += self.__likelihood_partition_pair(tmp_edges_12, len(g1)-1, len(g2)+1)
ll += self.__likelihood_partition_pair(tmp_edges_11, len(g1)-1)
ll += self.__likelihood_partition_pair(tmp_edges_22, len(g2)+1)
# compute change in edge counts and score if we move node to g2
for k in nbrs:
nodes_k = self.state.node_count(k)
# compute change in edges
edges_node_k = self.state.edge_count([node], self.state.get_nodes(k))
edges_1k = g1_edges[k] - edges_node_k
edges_2k = g2_edges[k] + edges_node_k
ll += self.__likelihood_partition_pair(edges_1k, len(g1)-1, nodes_k)
ll += self.__likelihood_partition_pair(edges_2k, len(g2)+1, nodes_k)
if ll > max:
max = ll
argmax = node
assert argmax != None
edges_node_1 = self.state.edge_count([argmax], g1)
edges_node_2 = self.state.edge_count([argmax], g2)
edges_11 = edges_11 - edges_node_1
edges_12 = edges_12 - edges_node_2 + edges_node_1
edges_22 = edges_22 + edges_node_2
assert edges_12 >= 0 and edges_11 >= 0 and edges_22 >= 0
for k in nbrs:
edges_node_k = self.state.edge_count([argmax], self.state.get_nodes(k))
g1_edges[k] = g1_edges[k] - edges_node_k
g2_edges[k] = g2_edges[k] + edges_node_k
assert g1_edges[k] >= 0 and g2_edges[k] >= 0
g1.remove(argmax)
g2.append(argmax)
s_g1 = set(g1)
s_g2 = set(g2)
s_i = set(self.state.get_nodes(i))
s_j = set(self.state.get_nodes(j))
if s_g1 == s_i:
assert s_g2 == s_j
return [ ]
elif s_g1 == s_j:
assert s_g2 == s_i
return [ ]
else:
return [ MergeSplit(i, j, g1, g2) ]
def get_stats(self):
#s = "noops=%d splits=%d merges=%d cache=%g" % (self.num_noops, self.num_splits, self.num_merges, 1.*self.cache_hit/self.cache_tries)
if self.cache_tries == 0:
cache_rate = 0
else:
cache_rate = 1.*self.cache_hit/self.cache_tries
t = (self.num_noops, self.num_splits, self.num_merges, self.num_mergesplits, self.num_flips, cache_rate)
self.num_noops = self.num_splits = self.num_merges = self.num_mergesplits = self.num_flips = self.cache_hit = self.cache_tries = 0
return t
def sample_graph(self, enforce_min_degree=False, enforce_connected_comps=False):
g2 = networkx.Graph()
for i in self.state.partition.keys():
edges_within = self.state.edge_count(i)
nodes_i = self.state.get_nodes(i)
g2.add_nodes_from(nodes_i)
assert edges_within <= len(nodes_i) * (len(nodes_i)-1)/2 and edges_within >= 0
while edges_within > 0:
u = random.choice(nodes_i)
v = random.choice(nodes_i)
if u == v or g2.has_edge(u,v):
continue
else:
g2.add_edge(u,v)
edges_within -= 1
for j in self.state.neighbors(i):
if i <= j:
continue
edges_between = self.state.edge_count(i,j)
nodes_j = self.state.get_nodes(j)
assert edges_between <= len(nodes_i) * len(nodes_j) and edges_between >= 0
while edges_between > 0:
u = random.choice(nodes_i)
v = random.choice(nodes_j)
if g2.has_edge(u,v):
continue
else:
g2.add_edge(u,v)
edges_between -= 1
if enforce_min_degree:
self.min_degree(g2)
if enforce_connected_comps:
self.conn_comps(g2)
if debug_sampling:
#import pdb
print "Checking sampled graph"
for i in self.state.partition:
nodes_i = self.state.get_nodes(i)
assert self.state.edge_count(i) == len(networkx.subgraph(g2, nodes_i).edges())
for j in self.state.partition:
if i <= j:
continue
nodes_j = self.state.get_nodes(j)
assert self.state.edge_count(i,j) == len(networkx.edge_boundary(g2, nodes_i, nodes_j))
return g2
def min_degree(self, g2):
# originally, tried to sample uniformly by edge
# very slow when partition is large because loop through entire partition