-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfake_seeds.cpp
208 lines (177 loc) · 4.52 KB
/
fake_seeds.cpp
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
#include "option.h"
#include "graph.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <random>
//// -n 1005 -o ../../datasets/EmailCore/fake.seeds -k 10 -m top -f 0.02 -s ../../datasets/EmailCore/inf.txt
using namespace std;
void generateSeedsTop(int n, int k, float f, const char* infFile, vector<int> &seeds) {
ifstream in(infFile);
int i, v1, rand_pos;
float v2;
int num = n * f;
vector<int> candidates(num,0);
srand (time(NULL));
for (i = 0; i < num; i++) {
in >> v1 >> v2;
rand_pos = rand() % (i+1);
if (rand_pos != i) candidates[i] = candidates[rand_pos];
candidates[rand_pos] = v1;
}
in.close();
for (i = 0; i < k; i++) {
seeds[i] = candidates[i];
}
}
void generateSeedsTopAgain(int n, int k, float f, const char* infFile, char* infile2, vector<int> &seeds) {
ifstream in(infFile);
ifstream in2(infile2);
int generated_num, id;
map<int,bool>already_choose; //mark the nodes that are already generate
in2 >> generated_num;
int i, v1, rand_pos, j;
for (j = 0; j < generated_num; j++){
in2 >> id;
already_choose[id] = true;
seeds[j] = id;
}
float v2;
int num = n * f;
vector<int> candidates(num,0);
srand (time(NULL));
for (i = 0; i < num; i++) {
in >> v1 >> v2;
rand_pos = rand() % (i+1);
if (rand_pos != i) candidates[i] = candidates[rand_pos];
candidates[rand_pos] = v1;
}
in.close();
for (i = generated_num, j = 0; i < k; j++) {
if (already_choose[candidates[j]] == false){
seeds[i] = candidates[j];
i++;
}
}
}
void generateSeedsRandom(int n, int k, vector<int> &seeds) {
int i, v, rand_pos;
vector<int> candidates(n,0);
srand (time(NULL));
for (i = 0; i < n; i++) {
candidates[i] = i;
}
for (i = n-1; i > 0; i--) {
rand_pos = rand() % (i+1);
v = candidates[rand_pos];
candidates[rand_pos] = candidates[i];
candidates[i] = v;
}
for (i = 0; i < k; i++) {
seeds[i] = candidates[i];
}
}
void generateSeedsRandomAgain(int n, int k, char* infile2, vector<int> &seeds) {
ifstream in2(infile2);
int generated_num, id;
map<int,bool>already_choose; //mark the nodes that are already generate
in2 >> generated_num;
int i, v, rand_pos, j;
for (j = 0; j < generated_num; j++){
in2 >> id;
already_choose[id] = true;
seeds[j] = id;
}
float v2;
vector<int> candidates(n,0);
srand (time(NULL));
for (i = 0; i < n; i++) {
candidates[i] = i;
}
for (i = n-1; i > 0; i--) {
rand_pos = rand() % (i+1);
v = candidates[rand_pos];
candidates[rand_pos] = candidates[i];
candidates[i] = v;
}
for (i = generated_num, j = 0; i < k; j++) {
if (already_choose[candidates[j]] == false){
seeds[i] = candidates[j];
i++;
}
}
}
void generateSeedsComm(Graph &g, int n, int k, const char* commFile, vector<int> &seeds) {
return;
}
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 * outFile = op.getPara("-o");
if (outFile == NULL){
outFile = (char*)"fake.seeds";
}
int n = -1;
char * tmp = op.getPara("-n");
if (tmp != NULL){
n = atoi(tmp);
}
if (n < 0) {
printf("Must provide graph size!");
return -1;
}
int k = 1;
tmp = op.getPara("-k");
if (tmp != NULL){
k = atoi(tmp);
}
float f = 0.1;
tmp = op.getPara("-f");
if (tmp != NULL){
f = atof(tmp);
}
char * method = op.getPara("-m");
if (method == NULL){
method = (char*)"top";
}
char * infFile = op.getPara("-s");
if (infFile == NULL){
infFile = (char*)"singleton.inf";
}
char * infFile2 = op.getPara("-a");
if (infFile == NULL){
infFile = (char*)"already.seeds";
}
vector<int> seeds(k,0);
if (strcmp(method, "top") == 0) {
generateSeedsTop(n, k, f, infFile, seeds);
} else if (strcmp(method, "random") == 0) {
generateSeedsRandom(n, k, seeds);
} else if (strcmp(method, "ta") == 0) {
generateSeedsTopAgain(n, k, f, infFile, infFile2, seeds);
} else if (strcmp(method, "ra") == 0) {
generateSeedsRandomAgain(n, k, infFile2, seeds);
} else {
printf("Incorrect method option!");
return -1;
}
ofstream of(outFile);
of << k << endl;
for (int i = 0; i < k; i++){
of << seeds[i] << endl;
}
of.close();
//
// ALL DONE
//
}