-
Notifications
You must be signed in to change notification settings - Fork 16
/
celfpp.py
65 lines (58 loc) · 2.06 KB
/
celfpp.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
import diffusion
from heapdict import heapdict
class Node(object):
def __init__(self, node):
self.node = node
self.mg1 = 0
self.prev_best = None
self.mg2 = 0
self.flag = None
self.list_index = 0
def celfpp(graph, diffuse, k):
S = set()
# Note that heapdict is min heap and hence add negative priorities for
# it to work.
Q = heapdict()
last_seed = None
cur_best = None
node_data_list = []
for node in graph.nodes:
node_data = Node(node)
node_data.mg1 = diffuse.diffuse_mc([node])
node_data.prev_best = cur_best
node_data.mg2 = diffuse.diffuse_mc([node, cur_best.node]) if cur_best else node_data.mg1
node_data.flag = 0
cur_best = cur_best if cur_best and cur_best.mg1 > node_data.mg1 else node_data
graph.nodes[node]['node_data'] = node_data
node_data_list.append(node_data)
node_data.list_index = len(node_data_list) - 1
Q[node_data.list_index] = - node_data.mg1
while len(S) < k:
node_idx, _ = Q.peekitem()
node_data = node_data_list[node_idx]
if node_data.flag == len(S):
S.add(node_data.node)
del Q[node_idx]
last_seed = node_data
continue
elif node_data.prev_best == last_seed:
node_data.mg1 = node_data.mg2
else:
before = diffuse.diffuse_mc(S)
S.add(node_data.node)
after = diffuse.diffuse_mc(S)
S.remove(node_data.node)
node_data.mg1 = after - before
node_data.prev_best = cur_best
S.add(cur_best.node)
before = diffuse.diffuse_mc(S)
S.add(node_data.node)
after = diffuse.diffuse_mc(S)
S.remove(cur_best.node)
if node_data.node != cur_best.node: S.remove(node_data.node)
node_data.mg2 = after - before
if cur_best and cur_best.mg1 < node_data.mg1:
cur_best = node_data
node_data.flag = len(S)
Q[node_idx] = - node_data.mg1
return S