forked from tangjianpku/LINE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstruct.cpp
More file actions
319 lines (272 loc) · 7.42 KB
/
Copy pathreconstruct.cpp
File metadata and controls
319 lines (272 loc) · 7.42 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#define MAX_STRING 100
const int hash_table_size = 30000000;
typedef float real; // Precision of float numbers
struct ClassVertex
{
double degree, sum_weight;
char *name;
};
struct Neighbor
{
int vid;
double weight;
friend bool operator < (Neighbor n1, Neighbor n2)
{
return n1.weight > n2.weight;
}
};
char train_file[MAX_STRING], output_file[MAX_STRING];
struct ClassVertex *vertex;
int *vertex_hash_table;
int max_num_vertices = 1000, num_vertices = 0;
long long num_edges = 0;
int max_depth = 1, max_k = 0;
std::vector<int> vertex_set;
std::vector<Neighbor> *neighbor;
Neighbor *rank_list;
std::map<int, double> vid2weight;
/* Build a hash table, mapping each vertex name to a unique vertex id */
unsigned int Hash(char *key)
{
unsigned int seed = 131;
unsigned int hash = 0;
while (*key)
{
hash = hash * seed + (*key++);
}
return hash % hash_table_size;
}
void InitHashTable()
{
vertex_hash_table = (int *)malloc(hash_table_size * sizeof(int));
for (int k = 0; k != hash_table_size; k++) vertex_hash_table[k] = -1;
}
void InsertHashTable(char *key, int value)
{
int addr = Hash(key);
while (vertex_hash_table[addr] != -1) addr = (addr + 1) % hash_table_size;
vertex_hash_table[addr] = value;
}
int SearchHashTable(char *key)
{
int addr = Hash(key);
while (1)
{
if (vertex_hash_table[addr] == -1) return -1;
if (!strcmp(key, vertex[vertex_hash_table[addr]].name)) return vertex_hash_table[addr];
addr = (addr + 1) % hash_table_size;
}
return -1;
}
/* Add a vertex to the vertex set */
int AddVertex(char *name)
{
int length = strlen(name) + 1;
if (length > MAX_STRING) length = MAX_STRING;
vertex[num_vertices].name = (char *)calloc(length, sizeof(char));
strcpy(vertex[num_vertices].name, name);
vertex[num_vertices].sum_weight = 0;
num_vertices++;
if (num_vertices + 2 >= max_num_vertices)
{
max_num_vertices += 1000;
vertex = (struct ClassVertex *)realloc(vertex, max_num_vertices * sizeof(struct ClassVertex));
}
InsertHashTable(name, num_vertices - 1);
return num_vertices - 1;
}
/* Read network from the training file */
void ReadData()
{
FILE *fin;
char name_v1[MAX_STRING], name_v2[MAX_STRING], str[2 * MAX_STRING + 10000];
int vid, u, v;
double weight;
Neighbor nb;
fin = fopen(train_file, "rb");
if (fin == NULL)
{
printf("ERROR: network file not found!\n");
exit(1);
}
num_edges = 0;
while (fgets(str, sizeof(str), fin)) num_edges++;
fclose(fin);
printf("Number of edges: %lld \n", num_edges);
fin = fopen(train_file, "rb");
num_vertices = 0;
for (int k = 0; k != num_edges; k++)
{
fscanf(fin, "%s %s %lf", name_v1, name_v2, &weight);
if (k % 10000 == 0)
{
printf("Reading edges: %.3lf%%%c", k / (double)(num_edges + 1) * 100, 13);
fflush(stdout);
}
vid = SearchHashTable(name_v1);
if (vid == -1) vid = AddVertex(name_v1);
vertex[vid].degree += weight;
vid = SearchHashTable(name_v2);
if (vid == -1) vid = AddVertex(name_v2);
vertex[vid].degree += weight;
}
fclose(fin);
printf("Number of vertices: %lld \n", num_vertices);
neighbor = new std::vector<Neighbor>[num_vertices];
rank_list = (Neighbor *)calloc(num_vertices, sizeof(Neighbor));
fin = fopen(train_file, "rb");
for (long long k = 0; k != num_edges; k++)
{
fscanf(fin, "%s %s %lf", name_v1, name_v2, &weight);
if (k % 10000 == 0)
{
printf("Reading neighbors: %.3lf%%%c", k / (double)(num_edges + 1) * 100, 13);
fflush(stdout);
}
u = SearchHashTable(name_v1);
v = SearchHashTable(name_v2);
nb.vid = v;
nb.weight = weight;
neighbor[u].push_back(nb);
}
fclose(fin);
printf("\n");
for (int k = 0; k != num_vertices; k++)
{
vertex[k].sum_weight = 0;
int len = neighbor[k].size();
for (int i = 0; i != len; i++)
vertex[k].sum_weight += neighbor[k][i].weight;
}
}
void Reconstruct()
{
FILE *fo = fopen(output_file, "wb");
int sv, cv, cd, len, pst;
long long num_edges_renet = 0;
double cw, sum;
std::queue<int> node, depth;
std::queue<double> weight;
for (sv = 0; sv != num_vertices; sv++)
{
if (sv % 10 == 0)
{
printf("%cProgress: %.3lf%%", 13, (real)sv / (real)(num_vertices + 1) * 100);
fflush(stdout);
}
while (!node.empty()) node.pop();
while (!depth.empty()) depth.pop();
while (!weight.empty()) weight.pop();
vid2weight.clear();
for (int i = 0; i != num_vertices; i++)
{
rank_list[i].vid = i;
rank_list[i].weight = 0;
}
len = neighbor[sv].size();
if (len > max_k)
{
for (int i = 0; i != len; i++)
fprintf(fo, "%s\t%s\t%lf\n", vertex[sv].name, vertex[neighbor[sv][i].vid].name, neighbor[sv][i].weight);
num_edges_renet += len;
continue;
}
vid2weight[sv] += vertex[sv].degree / 10.0; // Set weights for self-links here!
len = neighbor[sv].size();
sum = vertex[sv].sum_weight;
node.push(sv);
depth.push(0);
weight.push(sum);
while (!node.empty())
{
cv = node.front();
cd = depth.front();
cw = weight.front();
node.pop();
depth.pop();
weight.pop();
if (cd != 0) vid2weight[cv] += cw;
if (cd < max_depth)
{
len = neighbor[cv].size();
sum = vertex[cv].sum_weight;
for (int i = 0; i != len; i++)
{
node.push(neighbor[cv][i].vid);
depth.push(cd + 1);
weight.push(cw * neighbor[cv][i].weight / sum);
}
}
}
pst = 0;
std::map<int, double>::iterator iter;
for (iter = vid2weight.begin(); iter != vid2weight.end(); iter++)
{
rank_list[pst].vid = (iter->first);
rank_list[pst].weight = (iter->second);
pst++;
}
std::sort(rank_list, rank_list + pst);
for (int i = 0; i != max_k; i++)
{
if (i == pst) break;
fprintf(fo, "%s\t%s\t%.6lf\n", vertex[sv].name, vertex[rank_list[i].vid].name, rank_list[i].weight);
num_edges_renet++;
}
}
printf("\n");
fclose(fo);
printf("Number of edges in reconstructed network: %lld\n", num_edges_renet);
return;
}
void TrainLINE()
{
InitHashTable();
ReadData();
Reconstruct();
}
int ArgPos(char *str, int argc, char **argv) {
int a;
for (a = 1; a < argc; a++) if (!strcmp(str, argv[a])) {
if (a == argc - 1) {
printf("Argument missing for %s\n", str);
exit(1);
}
return a;
}
return -1;
}
int main(int argc, char **argv) {
int i;
if (argc == 1) {
printf("Reconstruct the network by using a Breadth-First-Search strategy\n\n");
printf("Options:\n");
printf("Parameters for training:\n");
printf("\t-train <file>\n");
printf("\t\tReconstruct the network from <file>\n");
printf("\t-output <file>\n");
printf("\t\tUse <file> to save the reconstructed network\n");
printf("\t-depth <int>\n");
printf("\t\tThe maximum depth in the Breadth-First-Search; default is 0\n");
printf("\t-threshold <int>\n");
printf("\t\tFor vertex whose degree is less than <int>, we will expand its neighbors until the degree reaches <iny>\n");
printf("\nExamples:\n");
printf("./reconstruct -train net.txt -output net_dense.txt -depth 2 -threshold 1000\n\n");
return 0;
}
if ((i = ArgPos((char *)"-train", argc, argv)) > 0) strcpy(train_file, argv[i + 1]);
if ((i = ArgPos((char *)"-output", argc, argv)) > 0) strcpy(output_file, argv[i + 1]);
if ((i = ArgPos((char *)"-depth", argc, argv)) > 0) max_depth = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-threshold", argc, argv)) > 0) max_k = atoi(argv[i + 1]);
vertex = (struct ClassVertex *)calloc(max_num_vertices, sizeof(struct ClassVertex));
TrainLINE();
return 0;
}