-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyperGraph.h
323 lines (303 loc) · 11.1 KB
/
hyperGraph.h
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
//
// Created by sbian on 2019/9/9.
//
#ifndef BIM_HYPERGRAPH_H
#define BIM_HYPERGRAPH_H
class HyperGraph
{
private:
/// __numV: number of nodes in the graph.
uint32_t __numV;
/// __numE: number of edges in the graph.
size_t __numE;
/// __numRRsets: number of RR sets.
size_t __numRRsets = 0;
std::vector<bool> __vecVisitBool;
Nodelist __vecVisitNode;
/// Initialization
void init_hypergraph()
{
__numV = (uint32_t)_graph.size();
for (auto& nbrs : _graph) __numE += nbrs.size();
_FRsets = FRsets(__numV);
__vecVisitBool = std::vector<bool>(__numV);
__vecVisitNode = Nodelist(__numV);
}
public:
/// _graph: reverse graph
const Graph& _graph;
/// _FRsets: forward cover sets, _FRsets[i] is the node sets that node i can reach
FRsets _FRsets;
/// _RRsets: reverse cover sets, _RRsets[i] is the node set that can reach node i
RRsets _RRsets;
/// _cascadeModel: the cascade model, default is IC
CascadeModel _cascadeModel = IC;
explicit HyperGraph(const Graph& graph) : _graph(graph)
{
init_hypergraph();
}
/// Set cascade model
void set_cascade_model(const CascadeModel model)
{
_cascadeModel = model;
}
/// Returns the number of nodes in the graph.
uint32_t get_nodes() const
{
return __numV;
}
/// Returns the number of edges in the graph.
size_t get_edges() const
{
return __numE;
}
/// Returns the number of RR sets in the graph.
size_t get_RR_sets_size() const
{
return __numRRsets;
}
/// Get out degree
std::vector<size_t> get_out_degree() const
{
std::vector<size_t> outDeg(__numV);
for (auto& nbrs : _graph)
{
for (auto& nbr : nbrs)
{
outDeg[nbr.first]++;
}
}
return outDeg;
}
/// Generate a set of n RR sets
void build_n_RRsets(const size_t numSamples)
{
if (numSamples > SIZE_MAX)
{
std::cout << "Error:R too large" << std::endl;
exit(1);
}
const auto prevSize = __numRRsets;
__numRRsets = __numRRsets > numSamples ? __numRRsets : numSamples;
for (auto i = prevSize; i < numSamples; i++)
{
build_one_RRset(dsfmt_gv_genrand_uint32_range(__numV), i);
}
}
/// Generate one RR set
void build_one_RRset(const uint32_t uStart, const size_t hyperIdx)
{
size_t numVisitNode = 0, currIdx = 0;
_FRsets[uStart].push_back(hyperIdx);
__vecVisitNode[numVisitNode++] = uStart;
__vecVisitBool[uStart] = true;
while (currIdx < numVisitNode)
{
const auto expand = __vecVisitNode[currIdx++];
if (_cascadeModel == IC)
{
for (auto& nbr : _graph[expand])
{
const auto nbrId = nbr.first;
if (__vecVisitBool[nbrId])
continue;
const auto randDouble = dsfmt_gv_genrand_open_close();
if (randDouble > nbr.second)
continue;
__vecVisitNode[numVisitNode++] = nbrId;
__vecVisitBool[nbrId] = true;
_FRsets[nbrId].push_back(hyperIdx);
}
}
else if (_cascadeModel == LT)
{
if (_graph[expand].size() == 0)
continue;
const auto nextNbrIdx = gen_random_node_by_weight_LT(_graph[expand]);
if (nextNbrIdx >= _graph[expand].size()) break; // No element activated
const auto nbrId = _graph[expand][nextNbrIdx].first;
if (__vecVisitBool[nbrId]) break; // Stop, no further node activated
__vecVisitNode[numVisitNode++] = nbrId;
__vecVisitBool[nbrId] = true;
_FRsets[nbrId].push_back(hyperIdx);
}
}
for (int i = 0; i < numVisitNode; i++) __vecVisitBool[__vecVisitNode[i]] = false;
_RRsets.push_back(RRset(__vecVisitNode.begin(), __vecVisitNode.begin() + numVisitNode));
}
/// Evaluate the influence spread of a seed set on current generated RR sets
double self_inf_cal(const Nodelist& vecSeed)
{
std::vector<bool> vecBoolVst = std::vector<bool>(__numRRsets);
std::vector<bool> vecBoolSeed(__numV);
for (auto seed : vecSeed) vecBoolSeed[seed] = true;
for (auto seed : vecSeed)
{
for (auto node : _FRsets[seed])
{
vecBoolVst[node] = true;
}
}
return 1.0 * std::count(vecBoolVst.begin(), vecBoolVst.end(), true) * __numV / __numRRsets;
}
/// Efficiently estimate the influence spread with sampling error epsilon within probability 1-delta
double effic_inf_valid_algo(const Nodelist& vecSeed, const double delta = 1e-3, const double eps = 0.01)
{
const double c = 2.0 * (exp(1.0) - 2.0);
const double LambdaL = 1.0 + 2.0 * c * (1.0 + eps) * log(2.0 / delta) / (eps * eps);
size_t numHyperEdge = 0;
size_t numCoverd = 0;
std::vector<bool> vecBoolSeed(__numV);
for (auto seed : vecSeed) vecBoolSeed[seed] = true;
while (numCoverd < LambdaL)
{
numHyperEdge++;
size_t numVisitNode = 0, currIdx = 0;
const auto uStart = dsfmt_gv_genrand_uint32_range(__numV);
if (vecBoolSeed[uStart])
{
// Stop, this sample is covered
numCoverd++;
continue;
}
__vecVisitNode[numVisitNode++] = uStart;
__vecVisitBool[uStart] = true;
while (currIdx < numVisitNode)
{
const auto expand = __vecVisitNode[currIdx++];
if (_cascadeModel == IC)
{
for (auto& nbr : _graph[expand])
{
const auto nbrId = nbr.first;
if (__vecVisitBool[nbrId])
continue;
const auto randDouble = dsfmt_gv_genrand_open_close();
if (randDouble > nbr.second)
continue;
if (vecBoolSeed[nbrId])
{
// Stop, this sample is covered
numCoverd++;
goto postProcess;
}
__vecVisitNode[numVisitNode++] = nbrId;
__vecVisitBool[nbrId] = true;
}
}
else if (_cascadeModel == LT)
{
if (_graph[expand].size() == 0)
continue;
const auto nextNbrIdx = gen_random_node_by_weight_LT(_graph[expand]);
if (nextNbrIdx >= _graph[expand].size()) break; // No element activated
const auto nbrId = _graph[expand][nextNbrIdx].first;
if (__vecVisitBool[nbrId]) break; // Stop, no further node activated
if (vecBoolSeed[nbrId])
{
// Stop, this sample is covered
numCoverd++;
goto postProcess;
}
__vecVisitNode[numVisitNode++] = nbrId;
__vecVisitBool[nbrId] = true;
}
}
postProcess:
for (auto i = 0; i < numVisitNode; i++)
__vecVisitBool[__vecVisitNode[i]] = false;
}
return 1.0 * numCoverd * __numV / numHyperEdge;
}
/// Efficiently evaluate the influence spread of a seed set with a given number of RR sets to test
double effic_inf_valid_algo_with_samplesize(const std::vector<uint32_t>& vecSeed, const size_t numSamples)
{
size_t numHyperEdge = 0;
size_t numCoverd = 0;
std::vector<bool> vecBoolSeed(__numV);
for (auto seed : vecSeed) vecBoolSeed[seed] = true;
while (++numHyperEdge < numSamples)
{
size_t numVisitNode = 0, currIdx = 0;
const auto uStart = dsfmt_gv_genrand_uint32_range(__numV);
if (vecBoolSeed[uStart])
{
// Stop, this sample is covered
numCoverd++;
continue;
}
__vecVisitNode[numVisitNode++] = uStart;
__vecVisitBool[uStart] = true;
while (currIdx < numVisitNode)
{
const auto expand = __vecVisitNode[currIdx++];
if (_cascadeModel == IC)
{
for (auto& nbr : _graph[expand])
{
const auto nbrId = nbr.first;
if (__vecVisitBool[nbrId])
continue;
const auto randDouble = dsfmt_gv_genrand_open_close();
if (randDouble > nbr.second)
continue;
if (vecBoolSeed[nbrId])
{
// Stop, this sample is covered
numCoverd++;
goto postProcess;
}
__vecVisitNode[numVisitNode++] = nbrId;
__vecVisitBool[nbrId] = true;
}
}
else if (_cascadeModel == LT)
{
if (_graph[expand].size() == 0)
continue;
const auto nextNbrIdx = gen_random_node_by_weight_LT(_graph[expand]);
if (nextNbrIdx >= _graph[expand].size()) break; // No element activated
const auto nbrId = _graph[expand][nextNbrIdx].first;
if (__vecVisitBool[nbrId]) break; // Stop, no further node activated
if (vecBoolSeed[nbrId])
{
// Stop, this sample is covered
numCoverd++;
goto postProcess;
}
__vecVisitNode[numVisitNode++] = nbrId;
__vecVisitBool[nbrId] = true;
}
}
postProcess:
for (auto i = 0; i < numVisitNode; i++)
__vecVisitBool[__vecVisitNode[i]] = false;
}
return 1.0 * numCoverd * __numV / numHyperEdge;
}
/// Refresh the hypergraph
void refresh_hypergraph()
{
for (auto i = __numRRsets; i--;)
{
RRset().swap(_RRsets[i]);
}
RRsets().swap(_RRsets);
for (auto i = __numV; i--;)
{
FRset().swap(_FRsets[i]);
}
__numRRsets = 0;
}
/// Release memory
void release_memory()
{
refresh_hypergraph();
std::vector<bool>().swap(__vecVisitBool);
Nodelist().swap(__vecVisitNode);
FRsets().swap(_FRsets);
}
};
using THyperGraph = HyperGraph;
using PHyperGraph = std::shared_ptr<THyperGraph>;
#endif //BIM_HYPERGRAPH_H