-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIMIC.cpp
163 lines (145 loc) · 3.29 KB
/
IMIC.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
#include <cstdio>
#include <queue>
#include <cstring>
#include <time.h>
#include <stdlib.h>
#include <iostream>
//#include <windows.h>
using namespace std;
#define NODE 100000
#define EDGE 600000
#define MAXK 200
#define SNAP 100
FILE* fp;
FILE* fout;
struct edge
{
int v, next;
int edgesample[SNAP];
};
edge E[EDGE];
int firstedge[NODE] = {0},
deg[NODE] = {0},
degin[NODE] = {0},
choose[NODE] = {0},
seed[MAXK] = {0};
bool visit[NODE] = {0};
int n, m, K,
TOPK = 20,
DIR = 0;
float marg[NODE] = {0};
struct myless
{
bool operator()(int x,int y){
return marg[x] < marg[y];
}
};
bool cur[NODE];
priority_queue <int, vector<int>, myless> PQ;
void GenerateSNAP()
{
for (int x = 0; x < n; x++){
for (int i = firstedge[x]; i != 0; i = E[i].next){
int y = E[i].v;
for (int k = 0; k < SNAP; k++){
float prob = (float)rand()/RAND_MAX;
if (prob < 1.0/degin[y])
E[i].edgesample[k] = 1;
else
E[i].edgesample[k] = 0;
}
}
}
}
int Simulate(int snapno,int topk)
{
queue <int> Q;
int x,y,i,
tot = 0;
float thrs;
memset(visit,0,sizeof(visit));
for (i = 0; i < topk; i++){
Q.push(seed[i]);
visit[seed[i]] = 1;
tot++;
}
while (!Q.empty()){
x = Q.front();
Q.pop();
for (i = firstedge[x]; i != 0; i = E[i].next){
y = E[i].v;
if (E[i].edgesample[snapno] == 1 && !visit[y]){
Q.push(y);
visit[y] = 1;
tot++;
}
}
}
return tot;
}
int main(int argc, char* argv[])
{
int x,y,maxj,
tot=0;
float maxd,
totnum=0;
fp = fopen(argv[1], "r");
fout = fopen(argv[2], "w");
srand(time(NULL));
if (argc >= 4){
TOPK = atoi(argv[3]);
}
if (argc >= 5 && argv[4][0] == 'D'){
DIR = 1;
}
fscanf(fp, "%d %d", &n, &m);
for (int i = 0; i < m; i++){
fscanf(fp, "%d %d", &x, &y);
tot++;
E[tot].v = y;
E[tot].next = firstedge[x];
firstedge[x] = tot;
deg[x]++;
degin[y]++;
if (DIR == 0){
tot++;
E[tot].v = x;
E[tot].next = firstedge[y];
firstedge[y] = tot;
deg[y]++;
degin[x]++;
}
}
fclose(fp);
GenerateSNAP(); //for Snapshot
memset(choose,0,sizeof(choose));
for (int i = 0; i < n; i++){
marg[i] = NODE+1;
PQ.push(i);
}
for (int i = 1; i <= TOPK; i++){
memset(cur,0,sizeof(cur));
while (1){
maxj = PQ.top();
PQ.pop();
maxd = marg[maxj];
if (cur[maxj]){
seed[i-1] = maxj;
fprintf(fout, "%d %f\n", maxj, maxd);
break;
}
else {
seed[i-1] = maxj;
totnum = 0;
for (int u = 1; u <= SNAP; u++){
totnum += Simulate(u, i)-Simulate(u, i-1);
}
marg[maxj] = totnum/SNAP;
cur[maxj] = 1;
PQ.push(maxj);
}
}
}
fclose(fout);
return 0;
}