forked from YingtongDou/Centrality-Influence-Maximization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runIAC.py
315 lines (289 loc) · 9.9 KB
/
runIAC.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
'''
Independent Arbitrary Cascade (IAC) is a independent cascade model with arbitrary
propagation probabilities.
'''
from __future__ import division
from copy import deepcopy
import random, multiprocessing, os, math, json
import networkx as nx
import matplotlib as plt
def uniformEp(G, p = .01):
'''
Every edge has the same probability p.
'''
if type(G) == type(nx.DiGraph()):
Ep = dict(zip(G.edges(), [p]*len(G.edges())))
elif type(G) == type(nx.Graph()):
Ep = dict()
for (u, v) in G.edges():
Ep[(u, v)] = p
Ep[(u, v)] = p
else:
raise ValueError, "Provide either nx.Graph or nx.DiGraph object"
return Ep
def randomEp(G, maxp):
'''
Every edge has random propagation probability <= maxp <= 1
'''
assert maxp <= 1, "Maximum probability cannot exceed 1."
Ep = dict()
if type(G) == type(nx.DiGraph()):
for v1,v2 in G.edges():
p = random.uniform(0, maxp)
Ep[(v1,v2)] = p
elif type(G) == type(nx.Graph()):
for v1,v2 in G.edges():
p = random.uniform(0, maxp)
Ep[(v1,v2)] = p
Ep[(v2,v1)] = p
else:
raise ValueError, "Provide either nx.Graph or nx.DiGraph object"
return Ep
def random_from_range (G, prange):
'''
Every edge has propagation probability chosen from prange uniformly at random.
'''
for p in prange:
if p > 1:
raise ValueError, "Propagation probability inside range should be <= 1"
Ep = dict()
if type(G) == type(nx.DiGraph()):
for v1,v2 in G.edges():
p = random.choice(prange)
Ep[(v1,v2)] = p
elif type(G) == type(nx.DiGraph()):
for v1,v2 in G.edges():
p = random.choice(prange)
Ep[(v1,v2)] = p
Ep[(v2,v1)] = p
return Ep
# http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
def chunks(lst, n):
size = int(math.ceil(float(len(lst))/n))
return [lst[i:i + size] for i in range(0, len(lst), size)]
def degree_categories(G, prange):
'''
Every edge has propagation probability chosen from prange based on degree of a node.
'''
for p in prange:
if p > 1:
raise ValueError, "Propagation probability inside range should be <= 1"
Ep = dict()
d = {v: sum([G[v][u]["weight"] for u in G[v]]) for v in G}
sorted_d = chunks(sorted(d.iteritems(), key = lambda (_, degree): degree), len(prange))
sorted_p = sorted(prange)
categories = zip(sorted_p, sorted_d)
dp = dict()
for c in categories:
p, nodes = c
for (v, _) in nodes:
dp[v] = p
if type(G) == type(nx.DiGraph()):
for v1,v2 in G.edges():
Ep[(v1,v2)] = dp[v1]
elif type(G) == type(nx.DiGraph()):
for v1,v2 in G.edges():
Ep[(v1,v2)] = dp[v2]
Ep[(v2,v1)] = dp[v1]
return Ep
def weightedEp(G):
'''
Every incoming edge of v has propagation probability equals to 1/deg(v)
'''
Ep = dict()
for v in G:
in_edges = G.in_edges([v])
degree = sum([G[u][v]["weight"] for (u, _) in in_edges])
for edge in in_edges:
Ep[edge] = 1.0/degree
return Ep
def runIAC (G, S, Ep):
''' Runs independent arbitrary cascade model.
Input: G -- networkx graph object
S -- initial set of vertices
Ep -- propagation probabilities
Output: T -- resulted influenced set of vertices (including S)
NOTE:
Ep is a dictionary for each edge it has associated probability
If graph is undirected for each edge (v1,v2) with probability p,
we have Ep[(v1,v2)] = p, Ep[(v2,v1)] = p.
'''
T = deepcopy(S) # copy already selected nodes
# ugly C++ version
i = 0
while i < len(T):
for v in G[T[i]]: # for neighbors of a selected node
if v not in T: # if it wasn't selected yet
w = G[T[i]][v]['weight'] # count the number of edges between two nodes
p = Ep[(T[i],v)] # propagation probability
if random.random() <= 1 - (1-p)**w: # if at least one of edges propagate influence
# print T[i], 'influences', v
T.append(v)
i += 1
return T
def avgIAC (G, S, Ep, I):
'''
Input:
G -- undirected graph
S -- seed set
Ep -- propagation probabilities
I -- number of iterations
Output:
avg -- average size of coverage
'''
avg = 0
for i in range(I):
avg += float(len(runIAC(G,S,Ep)))/I
return avg
def findCC(G, Ep):
# remove blocked edges from graph G
E = deepcopy(G)
edge_rem = [e for e in E.edges() if random.random() < (1-Ep[e])**(E[e[0]][e[1]]['weight'])]
E.remove_edges_from(edge_rem)
# initialize CC
CC = dict() # number of a component to its members
explored = dict(zip(E.nodes(), [False]*len(E)))
c = 0
# perform BFS to discover CC
for node in E:
if not explored[node]:
c += 1
explored[node] = True
CC[c] = [node]
component = E[node].keys()
for neighbor in component:
if not explored[neighbor]:
explored[neighbor] = True
CC[c].append(neighbor)
component.extend(E[neighbor].keys())
return CC
def findL(CCs, T):
# find top components that can reach T activated nodes
sortedCCs = sorted([(len(dv), dk) for (dk, dv) in CCs.iteritems()], reverse=True)
cumsum = 0 # sum of top components
L = 0 # current number of CC that achieve T
# find L first
for length, numberCC in sortedCCs:
L += 1
cumsum += length
if cumsum >= T:
break
return L, sortedCCs
def findCCs_size_distribution(G, Ep, T):
CCs = findCC(G, Ep)
L, sortedCCs = findL(CCs, T)
from itertools import groupby
histogram = [(s, len(list(group))) for (s, group) in groupby(sortedCCs, key = lambda (size, _): size)]
bluedots = 1
acc_size = 0
for (size, number) in histogram:
acc_size += size
if acc_size < T:
bluedots += 1
else:
break
return histogram, bluedots, L, len(CCs)
def findLrangeforTrange (G, Ep, Trange):
Lrange = []
CCs = findCC(G, Ep)
for T in Trange:
L, _ = findL(CCs, T)
Lrange.append(L)
return Lrange, len(CCs)
if __name__ == '__main__':
import time
start = time.time()
# read in graph
# G = nx.DiGraph()
# with open('../../graphdata/hep.txt') as f:
# n, m = f.readline().split()
# for line in f:
# try:
# u, v = map(int, line.split())
# except ValueError:
# continue
# try:
# G[u][v]["weight"] += 1
# G[v][u]["weight"] += 1
# except:
# G.add_edge(u, v, weight=1)
# G.add_edge(v, u, weight=1)
# print 'Built graph G'
# print time.time() - start
#
#
# nx.write_gpickle(G, "../../graphs/hep.gpickle")
# print 'Wrote graph G'
# print time.time() - start
G = nx.read_gpickle("../../graphs/hep.gpickle")
print 'Read graph G'
print time.time() - start
DROPBOX = "/home/sergey/Dropbox/Influence Maximization/"
random.seed(1)
# time2probability = time.time()
# prange = [.01, .02, .04, .08]
# Ep = random_from_range(G, prange)
# print 'Built probabilities Ep'
# print time.time() - time2probability
# model = "MultiValency"
#
# write CCs sizes distrbution to file
# T = 500
# histogram, bluedots, L, TotalCCs = findCCs_size_distribution(G, Ep, T)
# with open("plotdata/CCs_sizes_Multivalency1.txt", "w+") as fp:
# print >>fp, bluedots
# print >>fp, T
# print >>fp, L
# print >>fp, TotalCCs
# print >>fp, json.dumps(histogram)
#
# write TvsL to file
# Trange = range(1, 3001, 50)
# Lrange, TotalCCs = findLrangeforTrange(G, Ep, Trange)
# with open("plotdata/LvsT_%s.txt" %model, "w+") as fp:
# print >>fp, json.dumps(Trange)
# print >>fp, json.dumps(Lrange)
# print >>fp, TotalCCs
# with open(DROPBOX + "plotdata/LvsT_%s.txt" %model, "w+") as fp:
# print >>fp, json.dumps(Trange)
# print >>fp, json.dumps(Lrange)
# print >>fp, TotalCCs
# with open("Ep_hep_range1.txt", "w+") as f:
# for key, value in Ep.iteritems():
# f.write(str(key[0]) + " " + str(key[1]) + " " + str(value) + os.linesep)
#
# time2probability = time.time()
# prange = [.01, .02, .04, .08]
# Ep = degree_categories(G, prange)
# print 'Built probabilities Ep'
# print time.time() - time2probability
# model = "Categories"
#
# time2probability = time.time()
# Ep = randomEp(G, .1)
# print 'Built probabilities Ep'
# print time.time() - time2probability
# model = "Random"
#
# time2probability = time.time()
# Ep = uniformEp(G, .01)
# print 'Built probabilities Ep'
# print time.time() - time2probability
#
# time2probability = time.time()
# Ep = weightedEp(G)
# print 'Built probabilities Ep'
# print time.time() - time2probability
#
# import json
# coverage2length = [[0,0]]
# with open("plotdata/rawCCWPforDirect2.txt") as f:
# for line in f:
# [(cov, S)] = json.loads(line).items()
# coverage2length.append([len(S), int(cov)])
#
# coverage2length.sort(key=lambda (l,_): l)
#
# with open("plotdata/plotReverseCCWPforReverse2_v2.txt", "w+") as f:
# json.dump(coverage2length, f)
console = []