-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruss_detection.cpp
473 lines (421 loc) · 10.8 KB
/
truss_detection.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
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
#include "truss_detection.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <stdlib.h>
#include <sstream>
using namespace std;
//======edge
bool edge::operator < ( const edge &eg ) const {
if ( u == eg.u ) return v < eg.v;
else return u < eg.u;
}
//======edge_s
edge_s::edge_s (){
sup = 0;
ssup = 0;
truss = 2;
}
//======k_truss_detection
k_truss_detection::k_truss_detection() {
location.clear();
ad_list.clear();
answer.clear();
com_nb.clear();
arr = NULL;
bin = NULL;
pos = NULL;
rep = NULL;
node_num = 0;
edge_num = 0;
kmax = 0;
k = 2;
}
void k_truss_detection::add_row_vector( int st, int ed ) {
while ( st < ed )
{
st ++;
ad_list.push_back( new vector< pair < int, int > > );
}
}
void k_truss_detection::init_ans() {
for ( int i = 0; i <= (kmax + 4); i ++ )
answer.push_back( new vector< edge > );
}
void k_truss_detection::load_data ( string path, string model, int w ) {
ifstream fin;
int source;
int target;
int weight;
int prenode = 0;
path = path + "3-ad_list.txt";
fin.open ( path.c_str() );
if ( fin )
{
ad_list.push_back( new vector< pair< int, int > > );
while ( !fin.eof() )
{
fin >> source >> target >> weight;
if ( source == -1 ) break;
if ( source > prenode )
{
add_row_vector( prenode, source );
prenode = source;
node_num = source;
}
if ( weight >= w )
ad_list[source] -> push_back( make_pair( target, weight ) );
edge_num ++;
source = -1;
}
} else {
cout << "Error occurred when opening: " + path << endl;
exit ( 1 );
}
edge_num = edge_num / 2;
fin.close();
return;
}
int k_truss_detection::find_com_neighbor( int w, int to, int L, int R ) {
if ( L > R )
return -1;
else
{
int mid = ( L + R ) / 2;
if ( (*ad_list[to])[mid].first == w )
return mid;
else if ( (*ad_list[to])[mid].first > w )
return find_com_neighbor( w, to, L, mid-1 );
else if ( (*ad_list[to])[mid].first < w )
return find_com_neighbor( w, to, mid+1, R );
}
}
void k_truss_detection::pre_sup_cal ( string model ) {
//======support calculation
arr = new edge_s[ edge_num + 2 ];
//memset( arr, 0, sizeof( edge_s )*( edge_num + 2) );
int u;
int v;
int len;
int len1;
int len2;
int id = 0;
int s;
int t;
int spt;
for ( int i = 0; i <= node_num; i ++ )
{
len = ad_list[i] -> size();
for ( int j = 0; j < len; j ++ )
{
v = (*ad_list[ i ])[ j ].first;
u = i;
if ( u <= v ) break; // only consider u > v
len1 = len;
len2 = ad_list[v] -> size();
if ( len1 > len2 )
{
swap(u, v);
swap(len1, len2);
}
spt = 0;
s = 0;
t = 0;
if ( (len1 + len2) <= (int)(len1 * (log10(1.0 * len2)/log10(2.0))) )
{
while ( (s < len1) && (t < len2) )
{
if ( (*ad_list[u])[s].first == (*ad_list[v])[t].first )
{
spt ++;
s ++;
t ++;
}
else if ( (*ad_list[u])[s].first > (*ad_list[v])[t].first )
t ++;
else
s ++;
}
}
else
{
while ( s < len1 )
{
t = find_com_neighbor( (*ad_list[u])[s].first, v, 0, len2 - 1 );
if ( t != -1 )
spt ++;
s ++;
}
}
arr[id].u = i;
arr[id].v = (*ad_list[i])[j].first;
arr[id].w = (*ad_list[i])[j].second;
arr[id].ssup = spt;
arr[id++].sup = spt;
if ( spt > kmax )
kmax = spt;
}
}
//================
//=======bin sort asc
bin = new int[ kmax + 2 ];
pos = new int[edge_num + 2];
rep = new int[edge_num + 2];
}
void k_truss_detection::com_nb_cpt() {
int u;
int v;
int len1;
int len2;
int s;
int t;
com_nb.push_back( new vector< int > );
for ( int i = 0;i < edge_num; i ++)
{
u = arr[i].u;
v = arr[i].v;
com_nb.push_back( new vector< int > );
len1 = ad_list[u] -> size();
len2 = ad_list[v] -> size();
if ( len1 > len2 )
{
swap(u, v);
swap(len1, len2);
}
s = 0;
t = 0;
if ( (len1 + len2) <= (int)(len1 * (log10(1.0 * len2)/log10(2.0))) )
{
while ( (s < len1) && (t < len2) )
{
if ( (*ad_list[u])[s].first == (*ad_list[v])[t].first )
{
com_nb[i] -> push_back( (*ad_list[u])[s].first );
s ++;
t ++;
}
else if ( (*ad_list[u])[s].first > (*ad_list[v])[t].first )
t ++;
else
s ++;
}
}
else{
while ( s < len1 )
{
t = find_com_neighbor( (*ad_list[u])[s].first, v, 0, len2 - 1 );
if ( t != -1 )
com_nb[i] -> push_back( (*ad_list[u])[s].first );
s ++;
}
}
}
}
void k_truss_detection::binsort( int start ) {
edge_s* dup = new edge_s[ edge_num + 2 - start ];
for ( int i = 0; i < edge_num - start; i ++)
dup[i] = arr[ i + start ];
for ( int i = 0; i <= kmax; i ++ )
bin[i] = 0;
int spt;
for ( int i = 0; i < edge_num - start; i ++ )
{
spt = dup[i].sup;
bin[ spt ] ++;
}
int st = start, num = 0;
for ( int i = 0; i <= kmax; i ++ )
{
num = bin[i];
bin[i] = st;
st += num;
}
edge e;
for ( int i = start; i < edge_num; i ++ )
{
e.u = dup[i - start].u;
e.v = dup[i - start].v;
spt = dup[i - start].sup;
location[ e ] = bin[ spt ];
arr[ bin[spt] ] = dup[ i - start ];
pos[ bin[spt] ] = bin[spt];
rep[ bin[spt] ] = bin[spt];
bin[ spt ] ++;
}
for ( int i = kmax; i > 0; i -- )
bin[i] = bin[i-1];
bin[0] = start;
delete[] dup;
com_nb_cpt();
}
void k_truss_detection::dec(int x) { //swap
int oo;
int ppos;
int op;
int y;
oo = arr[x].sup;
ppos = bin[oo];
bin[oo] ++;
arr[x].sup --;
op = rep[x];
y = pos[ppos];
pos[ppos] = x;
rep[x] = ppos;
pos[op] = y;
rep[y] = op;
}
void k_truss_detection::truss_decomposition( string path, string model, int ww ) {
cout << "Beginning loading graph..." << endl;
clock_t stat, finish;
stat = clock();
load_data( path, model, ww );
finish = clock();
cout << "time: " << (double)(finish-stat)/CLOCKS_PER_SEC << endl;
cout << "Finished loading graph!" << endl;
cout<< "Beginning computing support..." << endl;
stat = clock();
pre_sup_cal( model );
finish = clock();
cout << "time: " << (double)(finish-stat)/CLOCKS_PER_SEC << endl;
cout << "Finished computing support!" << endl;
cout << "The max number of support: kmax = " << kmax << endl;
cout << "Beginning truss decomposition!" << endl;
stat = clock();
init_ans();
int start = 0;
int spt;
int u;
int v;
int w;
int s;
int len;
int ppos1;
int ppos2;
int now;
edge_s ttemp;
edge e, re, te;
binsort( start ); //sort
while( start < edge_num )
{
now = pos[start];
spt = arr[now].sup;
if ( spt <= (k - 2) )
{
u = arr[now].u;
v = arr[now].v;
e.u = u;
e.v = v;
answer[k] -> push_back( e );
len = com_nb[now] -> size();
s = 0;
while ( s < len )
{
w = (*com_nb[now])[s];
re.u = max(u, w);
re.v = min(u, w);
te.u = max(v, w);
te.v = min(v, w);
ppos1 = location[re];
ppos2 = location[te];
if ( (arr[ppos1].sup == -1) || (arr[ppos2].sup == -1) )
{
s ++;
continue;
}
if ( arr[ppos1].sup > (k - 2) )
dec(ppos1);
if ( arr[ppos2].sup > (k - 2) )
dec(ppos2);
s ++;
}
arr[now].truss = k;
arr[now].sup = -1;
start ++;
} else
k ++;
}
finish = clock();
cout << "time: " << (double)(finish-stat)/CLOCKS_PER_SEC << endl;
cout << "Finished truss decomposition!" << endl;
cout << "Maximal truss number: " << k << endl;
cout << answer[k] -> size() << endl;
cout << "Beginning saving data!" << endl;
save_results( path );
cout << "Finished saving data!" << endl;
delete[] arr;
delete[] bin;
delete[] pos;
delete[] rep;
location.clear();
ad_list.clear();
com_nb.clear();
//out_results();
}
void k_truss_detection::save_results( string path ) {
for ( int i = 0; i <= node_num; i ++ )
{
int len = (*ad_list[i]).size();
edge e;
int pos;
int target;
for ( int j = 0; j < len; j ++ )
{
target = (*ad_list[i])[j].first;
if ( i < target )
{
e.u = target;
e.v = i;
}
else
{
e.u = i;
e.v = target;
}
pos = location[e];
stringstream ss1, ss2, ss3, ss4, ss5;
ss1 << i;
res += ss1.str();
res += " ";
ss2 << target;
res += ss2.str();
res += " ";
ss3 << arr[pos].w;
res += ss3.str();
res += " ";
ss4 << arr[pos].ssup;
res += ss4.str();
res += " ";
ss5 << arr[pos].truss;
res += ss5.str();
res += "\n";
//fout << i << " " << target << " " << arr[pos].w << " " << arr[pos].ssup << " " << arr[pos].truss << endl;
}
}
path = path + "truss.txt";
ofstream fout( path.c_str() );
fout << res ;
fout.close();
}
void k_truss_detection::out_results() {
bool flag = true;
string path = "E://k-truss/trussness.txt";
ofstream fout( path.c_str() );
for ( int o = 2; o <= k; o ++ )
{
flag = true;
for ( int j = 0; j < (int)answer[o] -> size(); j ++ )
{
if ( flag )
{
fout << -2 << " " << o << endl;
flag = false;
}
fout << (*answer[o])[j].u << " " << (*answer[o])[j].v << endl;
}
}
fout.close();
answer.clear();
}