-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle_alg.cpp
More file actions
363 lines (307 loc) · 13.4 KB
/
triangle_alg.cpp
File metadata and controls
363 lines (307 loc) · 13.4 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
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
#include "option.h"
#include "rwgraph.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include "getMem.hpp"
#include "sampler.h"
#include "IMSampler.h"
using namespace std;
using namespace std::placeholders;
HyperGraph hg;
std::vector<int> selectedSeed;
std::vector<bool> seedMark;
double z, zmax,zmin,Tmax;
int zcover;
unsigned int numNodes;
unsigned int numSeeds;
double eager_thres;
double lambda;
bool do_check = false;
void init_seeds(Sampler &sample)
{
vector <uint64_t> s_init;
for (int i = 0; i < 16; ++i)
s_init.push_back(rand());
sample.init_seed(rand()%16,s_init);
}
void initialize(unsigned int n, unsigned int numS, double epsilon, double delta, bool c, double normalized)
{
seedMark = vector<bool>(n+1,false);
numNodes = n;
zcover = 0;
numSeeds = numS;
double lognk = lgamma(numNodes+1) - lgamma(numSeeds+1) - lgamma(numNodes-numSeeds-1);
double epsilon2 = sqrt((1-1/exp(1))*log(lognk*7/delta))/((1-1/exp(1))*sqrt(log(7/delta))+sqrt((1-1/exp(1))*log(lognk*7/delta)))*epsilon;
lambda = (2+2*epsilon2/3)*(lognk + log(7/delta))/(epsilon2*epsilon2);
zmax = (1+epsilon)/(1-1/exp(1))*lambda;
//cout << "Lambda threshold: " << lambda << " " << zmax << endl;
zmin = log(1/delta)/(epsilon*epsilon)/numSeeds;
double f = (log(6/delta)+lgamma(numNodes+1)-lgamma(numSeeds+1)-lgamma(numNodes-numSeeds+1))*normalized/(numSeeds*log(6*log2(numNodes)/delta));
Tmax = (2+2/3)*log(3*log2(f)/delta);
}
void reinitialize()
{
selectedSeed = vector<int>();
seedMark = vector<bool>(numNodes+1,false);
zcover = 0;
hg.reinitialize(numSeeds,ceil(zmax));
}
double lowerBound(double u, double i, double n, double a){
double ci = u/i;
double c = ci*n/i;
double lbound = a-(a+1)*ci/(3+ci);
double lb = (-(-2*a+2*ci/3+2*a*ci/3-2*c)-sqrt((-2*a+2*ci/3+2*a*ci/3-2*c)*(-2*a+2*ci/3+2*a*ci/3-2*c)-4*(a*a-2*a*ci/3)*(1-2*ci/3+2*c)))/(2*(1-2*ci/3+2*c));
if (lbound > lb){
lbound = lb;
}
return lbound;
}
double upperBound(double u, double i, double n, double a){
double ci = u/i;
double c = ci*n/i;
double ubound = a + (a+1)*ci/(3-ci);
//cout << "Computing upper bound: " << ci << " " << c << " " << ubound << endl;
double ub = (-(-2*a-2*ci/3-2*a*ci/3-2*c)+sqrt((-2*a-2*ci/3-2*a*ci/3-2*c)*(-2*a-2*ci/3-2*a*ci/3-2*c) - 4*(a*a+2*a*ci/3)*(1+2*ci/3+2*c)))/(2*(1+2*ci/3+2*c));
if (ubound < ub){
ubound = ub;
}
return ubound;
}
bool MSz(std::function< std::vector<int32_t>(int32_t &,int &, std::vector<bool> &, std::vector<bool>&) > sampler, std::vector<int> & seeds, int t, double & deg, float epsilon, float delta, long long int & numSuccessSamples, long long int & numTotalSamples, double normalized, double c, double thresholdz, std::vector<bool> &cseedmark, double ubound)
{
z = thresholdz;
//cout << "z = " << z << endl;
eager_thres = z/numSeeds;
double checkpoint = 1;
long long countSuccess = 0;
long long int countTotal = 0;
int k = numSeeds;
double epsilon0 = epsilon;
if (epsilon < 1.0/3){
epsilon0 = 1.0/3;
}
double f = (log(7/delta)+lgamma(numNodes+1)-lgamma(k+1)-lgamma(numNodes-k+1))/(log(7*log2(numNodes)/delta));
double lambda1 = 1+(1+epsilon0)*(2+2*epsilon0/3)*log(3*log2(f)/delta)/(epsilon0*epsilon0);
int32_t n_fail = 0;
int degree = 0;
int cc = 0;
while (selectedSeed.size() < k){
vector<int> tmp;
while(countTotal < checkpoint && selectedSeed.size() < k){
tmp = sampler(n_fail,cc,seedMark,cseedmark);
if (tmp.size() == 0){
break;
}
//cout << cc << endl;
if ((cc & 2) == 0){
hg.addHyperedge(tmp);
} else {
zcover++;
}
if (cc & 1){
degree++;
}
countTotal += n_fail+1;
countSuccess += 1;
// Add hyperedge into the graph since it's not covered
eager_thres = (z-zcover)*1.0/numSeeds;
while (hg.getMaxDegree() >= eager_thres && selectedSeed.size() < numSeeds){
int newSeed = hg.getMaxDegreeNode();
//cout << "Select " << newSeed << endl;
zcover += hg.getMaxDegree();
eager_thres = (z-zcover)*1.0/numSeeds;
selectedSeed.push_back(newSeed);
seedMark[newSeed] = true;
hg.selectNode(newSeed);
}
}
//cout << "Print degree and total samples: " << zcover << " " << degree << " " << countSuccess << " " << countTotal << " " << deg << " " << numTotalSamples << endl;
double u = log(numNodes*(log(Tmax)/log(1+c)*log(zmax/zmin)));
double lbound = lowerBound(u,countTotal,countTotal,degree*1.0/countTotal);
//cout << "Lower bound: " << lbound << endl;
//cout << "Assessment bound: " << lbound/ubound << " " << deg*2.0/z << endl;
if (lbound/ubound >= 1-1/exp(1)-epsilon){
// cout << "Estimated value: " << (degree*normalized/countTotal) << endl;
numSuccessSamples = countSuccess;
numTotalSamples = countTotal;
deg = degree;
return true;
}
/* if (degree >= lambda1){
double epsilon_1 = (deg*1.0/numTotalSamples)/(degree*1.0/countTotal) - 1;
cout << "Epsilon_1 = " << epsilon_1 << " " << deg << " " << numTotalSamples << " " << degree << " " << countTotal << endl;
double epsilon_2 = sqrt((1+epsilon0)*log(3*log2(zmax/zmin)*log(Tmax)/log(1+c))/(degree));
cout << "Epsilon_2 = " << epsilon_2 << endl;
double epsilon_3 = sqrt((1+epsilon0)*(1-1/exp(1)-epsilon)*countTotal*log(3*log2(zmax/zmin)*Tmax)/((1+epsilon/3)*degree*numTotalSamples));
cout << "Epsilon_3 = " << epsilon_3 << endl;
cout << "Epsilon_t = " << (epsilon_1 + epsilon_2 + epsilon_1*epsilon_2)*(1-1/exp(1)-epsilon) + epsilon_3*(1-1/exp(1)) << endl;
if ((epsilon_1 + epsilon_2 + epsilon_1*epsilon_2)*(1-1/exp(1)-epsilon) + epsilon_3*(1-1/exp(1)) <= epsilon){
cout << "Estimated value: " << (degree*normalized/countTotal) << endl;
numSuccessSamples = countSuccess;
numTotalSamples = countTotal;
deg = degree;
return true;
}
}*/
while (checkpoint <= countTotal)
checkpoint = checkpoint*(1+c);
}
seeds = selectedSeed;
deg = 0;
if (selectedSeed.size() < k){
vector<double> degreek(k+1,0);
hg.buildSeedSet(seeds,k-selectedSeed.size(),degreek);
deg += zcover + degreek[k-selectedSeed.size()];
} else {
if (zcover + hg.getSumK() < z){
vector<int> tmp;
while(zcover + hg.getSumK() < z){
tmp = sampler(n_fail,cc,seedMark,cseedmark);
if (tmp.size() == 0){
break;
}
if (cc <= 1){
hg.addHyperedge(tmp);
} else {
zcover++;
}
countSuccess += 1;
countTotal += n_fail+1;
}
}
deg += zcover;
}
numSuccessSamples = countSuccess;
numTotalSamples = countTotal;
//cout << "Print degree and total samples: " << zcover << " " << degree << " " << countSuccess << " " << countTotal << " " << deg << " " << numTotalSamples << endl;
return false;
}
void DTA(std::function< std::vector<int32_t>(int32_t &,int &, std::vector<bool> &, std::vector<bool> &) > sampler, std::vector<int> & seeds, int t, float epsilon, float delta, double normalized)
{
double deg = zmin;
long long int numSuccessSamples = 1;
long long int numTotalSamples = 1;
long long int samples = 0;
double c = 0.1;
clock_t start = clock();
//cout << "Try z = " << zmin << endl;
vector<bool> cseedmark(numNodes,false);
vector<int> seed_t;
double ubound = numNodes;
double u = log(numNodes*(log(Tmax)/log(1+c)*log(zmax/zmin)));
MSz(sampler,seeds,t,deg,epsilon,delta,numSuccessSamples,numTotalSamples,normalized,c,zmin,cseedmark, ubound);
int numIter = ceil(log2(zmax/zmin));
double zc = zmin;
for (int i = 1; i < numIter; ++i){
//cout << "DEG and Z: " << deg << " " << z << endl;
samples += numTotalSamples;
reinitialize();
for (int j = 0; j < seed_t.size(); ++j){
cseedmark[seed_t[j]] = false;
}
for (int j = 0; j < seeds.size(); ++j){
cseedmark[seeds[j]] = true;
}
seed_t = seeds;
double N = ceil(pow(1+c,ceil(log(numTotalSamples)/log(1+c))));
ubound = upperBound(u,numTotalSamples,N,zc/numTotalSamples);
//cout << "Upper bound: " << ubound << endl;
zc *= 2;
//cout << "Try z = " << zc << endl;
if (MSz(sampler,seeds,t,deg,epsilon,delta,numSuccessSamples,numTotalSamples,normalized,c,zc,cseedmark,ubound)){
break;
}
}
cout << "Seed Nodes: ";
for (unsigned int i = 0; i < seeds.size(); ++i){
cout << seeds[i] << " ";
}
cout << endl;
printf("Influence: %0.2lf\n",deg*normalized/numTotalSamples);
cout << "Time: " << (float)(clock()-start)/CLOCKS_PER_SEC << endl;
cout << "Memory: " << getMemValue()/1024.0 << endl;
cout << "Samples: " << samples + numTotalSamples << endl;
}
void SRA(std::function< std::vector<int32_t>(int32_t &,int &, std::vector<bool> &, std::vector<bool> &) > sampler, std::vector<int> & seeds, int t, float epsilon, float delta, double normalized)
{
double deg = 1;
long long int numSuccessSamples = 1;
long long int numTotalSamples = 1;
long long int samples = 0;
clock_t start = clock();
vector<bool> cseedmark(numNodes,false);
double c = 0.1;
double ubound = numNodes;
MSz(sampler,seeds,t,deg,epsilon,delta,numSuccessSamples,numTotalSamples,normalized,c,zmax,cseedmark,ubound);
cout << "Seed Nodes: ";
for (unsigned int i = 0; i < seeds.size(); ++i){
cout << seeds[i] << " ";
}
cout << endl;
printf("Influence: %0.2lf\n",deg*normalized/numTotalSamples);
cout << "Time: " << (float)(clock()-start)/CLOCKS_PER_SEC << endl;
cout << "Memory: " << getMemValue()/1024.0 << endl;
cout << "Samples: " << numTotalSamples << endl;
}
int main(int argc, char ** argv)
{
srand(time(NULL));
OptionParser op(argc, argv);
if (!op.validCheck()){
printf("Parameters error, please check the readme.txt file for correct format!\n");
return -1;
}
char * inFile = op.getPara("-i");
if (inFile == NULL){
inFile = (char*)"network.bin";
}
char * outFile = op.getPara("-o");
if (outFile == NULL){
outFile = (char*)"network.seeds";
}
char * tmp = op.getPara("-epsilon");
double epsilon = 0.1;
if (tmp != NULL){
epsilon = atof(tmp);
}
double delta = 0;
tmp = op.getPara("-delta");
if (tmp != NULL){
delta = atof(tmp);
}
int k = 0;
tmp = op.getPara("-k");
if (tmp != NULL){
k = atoi(tmp);
}
int t = 1;
tmp = op.getPara("-t");
if (tmp != NULL){
t = atoi(tmp);
}
char * alg = op.getPara("-alg");
if (alg == NULL){
cout << "Select an algorithm and run again!" << endl;
return 1;
}
bool do_check = false;
char * checked = op.getPara("-check");
if (checked != NULL) do_check = true;
vector<int> seeds(k,0);
graph g;
g.read_graph(inFile,false);
g.calculate_probability();
Sampler sam(g);
init_seeds(sam);
int n = g.get_n_vertices();
if (delta == 0) delta = 1.0/n;
initialize(n,k,epsilon,delta,false,n*1.0);
hg = HyperGraph(n,do_check,k,ceil(zmax));
if (strcmp(alg, "SRA") == 0){
SRA(std::bind(&Sampler::triangle_polling, &sam, _1, _2, _3, _4), seeds, t, epsilon,delta,n*1.0);
} else if (strcmp(alg, "DTA") == 0){
DTA(std::bind(&Sampler::triangle_polling, &sam, _1, _2, _3, _4),seeds,t,epsilon,delta,n*1.0);
}
}