Repository navigation
Expand file tree
/
Copy pathsybilbelief.cpp
More file actions
510 lines (349 loc) · 12.9 KB
/
Copy pathsybilbelief.cpp
File metadata and controls
510 lines (349 loc) · 12.9 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <list>
#include <vector>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= 40300
#include <tr1/unordered_map>
using namespace std::tr1;
#define hash_map unordered_map
#else
#include <unordered_map>
#endif
using namespace std;
// random generator function
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
class Data{
public:
typedef unsigned long int vertex;
//total number of nodes
long N;
//adjacency list
//weighted graph
unordered_map<vertex,list<pair<vertex, double> > > network_map;
//1 : weighted, 0: unweighted
int weighted_graph;
//messages, each message sum to 1
unordered_map<string, double> messages_map;
unordered_map<string, double> messages_map_new;
//priors: initial probability of being benign for each node
double *prior;
//posteriors: posterior probability of being benign for each node
//during computation, post is treated as the normalized multiplication of incoming messages of each node.
double *post;
//network file
char *network_file;
//posterior file
char *post_file;
//prior file
char *prior_file;
//labeled training set file
char *train_set_file;
double theta_pos; // positive nodes
double theta_neg; // negative nodes
double theta_unl; // unlabeled nodes
//this weight is used when the input is an unweighted graph
double weight;
//number of maximum iterations
double max_iter;
Data(){
}
/* Store edges and weights in the network_map */
void add_edge(vertex node1, vertex node2, double w){
// no self loops
if(node1==node2){
return;
}
//add node2 to the adjacency list of node1
pair <vertex, double> nei (node2, w);
network_map[node1].push_back(nei);
}
/* Read the social graph */
//the format for the social graph is
//each line corresponds to an edge, e.g, 3 2 0.8
//each edge in the graph appears twice, e.g.,
//3 2 0.8
//2 3 0.8
void read_network(){
ifstream in(network_file,ifstream::in);
assert(in);
string line;
vertex node1,node2;
double w;
//read edges
while(getline(in,line)!=NULL){
node1=(vertex)atol(strtok((char *)line.c_str()," \n\t\r"));
node2=(vertex)atol(strtok(NULL," \n\t\r"));
if (weighted_graph == 1) {
w=(double)atof(strtok(NULL," \n\t\r"));
}
else{
w = weight;
}
add_edge(node1, node2, w);
}
//number of nodes in the graph
N = network_map.size();
//allocate space for final posterior scores
post = (double*) malloc(sizeof(double)*(N));
//allocate space for prior scores
prior = (double*) malloc(sizeof(double)*(N));
in.close();
}
/* Read priorfile or/and training file */
void read_prior(){
//initialize node priors as theta_unl
vertex node;
for (node = 0; node < N; node++) {
prior[node] = theta_unl;
}
//reassign node priors if there exists the priorfile
if (prior_file != "") {
ifstream in(prior_file,ifstream::in);
assert(in);
string line;
double score;
while(getline(in,line)!=NULL){
node=(vertex)atol(strtok((char *)line.c_str()," \n\t\r"));
score=(double)atof(strtok(NULL," \n\t\r"));
prior[node] = score;
}
in.close();
}
//reassign node priors for labeled benign (positive) nodes or/and Sybils (negative nodes)
if (train_set_file != ""){
ifstream in(train_set_file,ifstream::in);
assert(in);
string line;
//reading labeled benign nodes
getline(in,line);
istringstream pos_train_str(line);
vertex sub;
while (pos_train_str){
pos_train_str >> sub;
prior[sub] = theta_pos;
}
//reading labeled Sybils
getline(in,line);
istringstream neg_train_str(line);
while (neg_train_str){
neg_train_str >> sub;
prior[sub] = theta_neg;
}
in.close();
}
}
string construct_id (vertex sender, vertex receiver){
string id;
ostringstream convert;
convert << sender;
convert << " ";
convert << receiver;
id = convert.str();
return id;
}
/* Initialize all edges the messages */
void initialize_messages(){
vertex node, nei;
list<pair<vertex, double> >::iterator iter;
string id;
for (node = 0; node < N; node++) {
post[node] = 0.5;
for (iter = network_map[node].begin(); iter != network_map[node].end(); iter++) {
nei = (*iter).first;
id = construct_id (nei, node);
messages_map[id] = 0.5;
}
}
}
/* Send message from sender to receiver */
double send_message(vertex sender, vertex receiver, double w){
//calculate multiplication of messages sent to sender except the one from the receiver.
double other_message[] = {1 - post[sender], post[sender]};
double message_receiver_sender_pos;
string id_receiver_sender = construct_id(receiver, sender);
message_receiver_sender_pos = messages_map[id_receiver_sender];
other_message[0] /= 1 - message_receiver_sender_pos;
other_message[1] /= message_receiver_sender_pos;
double message[] = {0.0, 0.0};
int i, j;
double node_potential;
double edge_potential;
//receiver's state
for (i = 0; i < 2; i++) {
//sender state
for (j = 0; j < 2; j++) {
node_potential = j == 1? prior[sender] : (1 - prior[sender]);
edge_potential = i == j? w : 1 - w;
message[i] += node_potential * edge_potential * other_message[j];
}
}
double sum = message[0] + message[1];
message[1] = message[1] / sum;
if (message[1] < 1e-10) {
message[1] = 1e-10;
}
if (message[1] > 1 - 1e-10) {
message[1] = 1 - 1e-10;
}
return message[1];
}
/* Update posteriors in each iteration */
void update_post(){
double value[2], sum;
list<pair<vertex, double> >::iterator iter;
string id;
vertex nei, node;
unordered_map<vertex,list<pair<vertex, double> > >::iterator map_iter;
for (map_iter = network_map.begin(); map_iter != network_map.end(); map_iter++) {
node = map_iter->first;
value[0] = 1;
value[1] = 1;
for (iter = (map_iter->second).begin(); iter != (map_iter->second).end(); iter++) {
nei = (*iter).first;
id = construct_id (nei, node);
value[0] *= 1 - messages_map[id];
value[1] *= messages_map[id];
//normalize the values to avoid underflow or overflow
sum = value[0] + value[1];
value[0] /= sum;
value[1] /= sum;
}
post[node] = value[1] / (value[1] + value[0]);
}
}
/* Compute the final posteriors */
void posterior(){
double value[] = {0.0, 0.0};
for (vertex node = 0; node < N; node++){
value[0] = (1 - prior[node]) * (1 - post[node]);
value[1] = prior[node] * post[node];
//probability of being benign
post[node] = value[1] / (value[0] + value[1]);
}
}
/* Loopy belief propagation */
//use iterative method to propagate messages
//flooding method's performance is not as good as iterative method
void lbp(){
//initialize messages
initialize_messages();
vector<vertex> ordering;
for (vertex i = 0; i < N; i++) {
ordering.push_back(i);
}
vector<vertex>::iterator iter_order;
vertex node;
double message;
list<pair<vertex, double> >::iterator nei_iter;
string id;
double sum;
int iter = 1;
while (iter <= max_iter) {
//random ordering
random_shuffle(ordering.begin(), ordering.end(), p_myrandom);
for (iter_order = ordering.begin(); iter_order != ordering.end(); iter_order++) {
node = *iter_order;
//collect messages for node from all of its neighbors
for (nei_iter = network_map[node].begin(); nei_iter != network_map[node].end(); nei_iter++) {
message = send_message((*nei_iter).first, node, (*nei_iter).second);
//update message_map
id = construct_id((*nei_iter).first, node);
messages_map_new[id] = message;
}
}
messages_map.clear();
messages_map = messages_map_new;
//update post
update_post();
iter += 1;
}
//calculate the final posteriors
posterior();
}
/* Write final posteriors to the output file */
void write_posterior(){
ofstream out(post_file, ofstream::out);
for (vertex i = 0; i < N; i++) {
out << i << " " << setprecision(10) << post[i] << endl;
}
out.close();
}
void parse_par(int argc, char **argv){
//default setting
network_file = "";
train_set_file = "";
post_file = "";
prior_file = "";
max_iter = 5;
theta_pos = 0.9;
theta_neg = 0.1;
theta_unl = 0.5;
weight = 0.9;
//by default, unweighted graph
weighted_graph = 0;
int i = 1;
while (i < argc) {
if (strcmp(argv[i],"-graphfile") == 0){
network_file = argv[i + 1];
}
else if (strcmp(argv[i],"-trainfile") == 0){
train_set_file = argv[i + 1];
}
else if (strcmp(argv[i],"-priorfile") == 0){
prior_file = argv[i + 1];
}
else if (strcmp(argv[i], "-postfile") == 0){
post_file = argv[i + 1];
}
else if (strcmp(argv[i],"-mIter") == 0){
max_iter = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-tp") == 0){
theta_pos = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tn") == 0){
theta_neg = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tu") == 0){
theta_unl = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-wei") == 0){
weight = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-wg") == 0){
weighted_graph = atoi(argv[i + 1]);
}
else{
cout << "undefined inputs: " << argv[i] <<endl;
exit(0);
}
i += 2;
}
}
};
int main (int argc, char **argv)
{
srand ( time(NULL) );
Data data;
data.parse_par(argc, argv);
data.read_network();
data.read_prior();
data.lbp();
data.write_posterior();
return 0;
}