forked from mjiang89/CatchSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatchSync.java
executable file
·2059 lines (2035 loc) · 68.8 KB
/
CatchSync.java
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Given a directed graph with millions of nodes, how can we automatically spot anomalies?
* Suspicious graph patterns show up in many applications,
* from Twitter users who buy fake followers,
* manipulating the social network,
* to botnet members performing distributed denial of service attacks,
* disturbing the network traffic graph.
*
* Input: node pairs (edge list) of a directed graph;
* Output:
* 1. Feature space plots: in-degree vs. authority for target nodes
* (out-degree vs. hubness for source nodes);
* 2. Synchronicity (coherence) vs. normality: for source nodes.
*
* Edited and Copyright by Meng Jiang
* Last update: Oct 29, 2014
*
* 1st: Feb 22, 2014
* 2nd: Sep 27, 2014
* 3rd: Oct 8, 2014
* 4th: Oct 20, 2014
*
* CatchSync: Catch Synchronized Behavior in Large Directed Graphs (KDD 2014 Best Paper Final List)
* Meng Jiang, Peng Cui, Alex Beutel, Christos Faloutsos and Shiqiang Yang
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Collections;
import java.util.Map.Entry;
import java.util.Random;
import java.lang.Math;
public class CatchSync {
// The maximum length of elements (string) per record in adjacency list is 1000.
// You may set the length 1024 when you import the files into sql.
final static int LEN_ELEMS = 1000;
/** Return the seperator of line. */
static String sep_of_file(String file_graph) {
String sep = null, line = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_graph)));
while ((line = br.readLine()) != null) {
if (line.charAt(0) == '#' || line.charAt(0) == '%') continue;
if (line.contains(" ")) sep = " ";
if (line.contains(",")) sep = ",";
if (line.contains("\t")) sep = "\t";
break;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return sep;
}
/** Return number of nodes from [file_nodemap] */
static int number_of_nodes(String file_nodemap) {
int N = 0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_nodemap)));
String line = br.readLine().substring(1);
String[] arr = line.split(",");
N = Integer.valueOf(arr[0]);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return N;
}
/** Solver for ax^3+bx^2+cx+d=0 */
static double solve(double a, double b, double c, double d) {
double elem1 = b*c/6/a/a-b*b*b/27/a/a/a-d/2/a;
double elem2 = c/3/a-b*b/9/a/a;
double delta2 = elem1*elem1+elem2*elem2*elem2;
if (delta2 < 0) return 0.0;
delta2 = Math.sqrt(delta2);
double delta0 = Math.pow(elem1+delta2, 1.0/3);
double delta1 = Math.pow(elem1-delta2, 1.0/3);
return -b/3/a+delta0+delta1;
}
/**
* Generate a random power law graph: alpha = 1.5.
*/
static void generate_rplg(String file_edgelist, String file_nodemap, int N, double alpha) {
double val = Math.pow(0.5, -alpha);
double D = solve(val+2,-2.0,0.0,-1.0*N);
if (D == 0) return;
D = D*D;
double C = Math.pow(D, alpha);
HashMap<Integer, Integer> d2c = new HashMap<Integer, Integer>();
int deg = 1;
do {
int c = (int)(Math.pow(deg, -alpha)*C);
if (c <= 0) break;
d2c.put(deg, c);
deg += 1;
} while (true);
d2c.put(0, (int)(d2c.get(1)*val));
int Nc = 0;
for (int d : d2c.keySet()) Nc += d2c.get(d);
double p = 1.0*N/Nc;
HashMap<Integer, Integer> d2cp = new HashMap<Integer, Integer>();
for (int d : d2c.keySet()) {
int c = (int)(Math.round(d2c.get(d)*p));
if (c > 0) d2cp.put(d, c);
}
Nc = 0;
for (int d : d2cp.keySet()) Nc += d2cp.get(d);
if (Nc <= N) {
int c = d2cp.get(0);
d2cp.remove(0);
d2cp.put(0, c+N-Nc);
} else {
int c = d2cp.get(0);
d2cp.remove(0);
d2cp.put(0, c+Nc-N);
/**
ArrayList<Integer> ds = new ArrayList<Integer>();
for (int d : d2cp.keySet()) ds.add(d);
Collections.sort(ds);
for (int i = ds.size()-1;i > 0;i--) {
int d = ds.get(i);
int c = d2cp.get(d);
if (Nc-c >= N) {
Nc -= c;
d2cp.remove(d);
} else {
break;
}
}
*/
}
int E = 0;
for (int d : d2cp.keySet()) E += d*d2cp.get(d);
ArrayList<Integer> outdseq = new ArrayList<Integer>();
ArrayList<Integer> indseq = new ArrayList<Integer>();
for (int d : d2cp.keySet()) {
for (int c = 0;c < d2cp.get(d);c++) {
outdseq.add(d);
}
}
int[] seq = new int[N];
for (int i = 0;i < N;i++) seq[i] = i;
int w;
Random rand = new Random();
for (int i = N-1;i > 0;i--) {
w = rand.nextInt(i);
int t = seq[i];
seq[i] = seq[w];
seq[w] = t;
}
for (int i = 0;i < N;i++) indseq.add(outdseq.get(i));
HashSet<Integer> ers = new HashSet<Integer>();
HashSet<Integer> ees = new HashSet<Integer>();
for (int i = 0;i < N;i++) {
int outd = outdseq.get(i);
int ind = indseq.get(i);
if (outd > 0) ers.add(i);
if (ind > 0) ees.add(i);
}
double Ec = 0;
for (int er : ers) {
int outd = outdseq.get(er);
for (int ee : ees) {
int ind = indseq.get(ee);
Ec += 1.0*outd*ind/E;
}
}
E = 0;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file_edgelist));
for (int er : ers) {
int outd = outdseq.get(er);
for (int ee : ees) {
int ind = indseq.get(ee);
if (rand.nextDouble() < 1.0*outd*ind/Ec) {
E += 1;
bw.write(er+","+ee+"\n");
}
}
}
bw.close();
bw = new BufferedWriter(new FileWriter(file_nodemap));
bw.write("#"+N+","+E+"\n");
for (int i = 0;i < N;i++) {
bw.write(i+","+i+"\n");
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Index the node list in [file_graph].
* Translate it into edge list [file_edgelist].
* Store the index in [file_nodemap].
*/
static void graph_to_edgelist(String file_graph, String file_edgelist, String file_nodemap) {
String sep = sep_of_file(file_graph);
if (sep == null) {
System.out.println("The seperator of line in file [" + file_graph + "] should be space/comma/tab!");
return;
}
String line = null;
int nume = 0;
HashMap<String, Integer> nodemap = new HashMap<String, Integer>();
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file_edgelist));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_graph)));
while ((line = br.readLine()) != null) {
if (line.charAt(0) == '#' || line.charAt(0) == '%') continue;
String[] arr = line.split(sep);
String node0 = arr[0], node1 = arr[1];
if (!nodemap.containsKey(node0)) nodemap.put(node0, nodemap.size());
if (!nodemap.containsKey(node1)) nodemap.put(node1, nodemap.size());
int elem0 = nodemap.get(node0), elem1 = nodemap.get(node1);
nume += 1;
bw.write(elem0+","+elem1+"\n");
}
br.close();
bw.close();
bw = new BufferedWriter(new FileWriter(file_nodemap));
bw.write("#"+nodemap.size()+","+nume+"\n");
for (String node : nodemap.keySet()) bw.write(nodemap.get(node)+","+node+"\n");
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Inject groups ([injections]) of suspicious fans and idols on edge list of [file_edgelist].
* Write down the injected edge list into [file_edgelist_injected].
* Store the injected fans into [file_injectedu].
* Store the injected idols into [file_injectedv].
* For injection, there are two types:
* spike: NUM_FANS, NUM_IDOLS, DEGREE_SPIKE, CAMOUFLAGE_OF_DEGREE
* gap: NUM_FANS, NUM_IDOLS, DEGREE_0 (start), DEGREE_GAP, CAMOUFLAGE_OF_DEGREE
*/
static void injected_on_edgelist(String file_edgelist, String file_nodemap,
String file_edgelist_injected, String file_nodemap_injected,
String file_injectedu, String file_injectedv, ArrayList<String> injections) {
Random rand = new Random();
String line = null;
try {
int N0 = number_of_nodes(file_nodemap);
int nume = 0;
BufferedWriter bw = new BufferedWriter(new FileWriter(file_edgelist_injected));
BufferedWriter bwu = new BufferedWriter(new FileWriter(file_injectedu));
BufferedWriter bwv = new BufferedWriter(new FileWriter(file_injectedv));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_edgelist)));
while ((line = br.readLine()) != null) {
if (line.charAt(0) == '#' || line.charAt(0) == '%') continue;
nume += 1;
bw.write(line+"\n");
}
int Nall = N0;
for (String injection : injections) {
String[] arr = injection.split(",");
if (arr[0].equals("spike")) {
int M = Integer.valueOf(arr[1]), N = Integer.valueOf(arr[2]);
int Dspike = Integer.valueOf(arr[3]), K = Dspike, Nstart = Nall, i = Nstart;
double C = Double.valueOf(arr[4]);
for (int m = 0;m < M;m++) {
int outd = -1;
// standard normal distribution
double v1, v2, w;
do {
v1 = rand.nextDouble() * 2.0 - 1.0;
v2 = rand.nextDouble() * 2.0 - 1.0;
w = v1 * v1 + v2 * v2;
} while (w > 1.0);
outd = (int)(K+v1*Math.sqrt(-2.0*Math.log(w)/w));
if (outd < 0) outd = K;
int outdC = (int)(outd*C), outdN = outd - outdC;
HashSet<Integer> Ns = new HashSet<Integer>(), Cs = new HashSet<Integer>();
for (int d = 0;d < outdN;d++) Ns.add(Nstart+M+rand.nextInt(N));
for (int d = 0;d < outdC;d++) Cs.add(rand.nextInt(N0));
for (int j : Ns) {
nume += 1;
bw.write(i+","+j+"\n");
}
for (int j : Cs) {
nume += 1;
bw.write(i+","+j+"\n");
}
i += 1;
}
for (i = Nall;i < Nall+M;i++) bwu.write(i+"\n");
Nall += M;
for (i = Nall;i < Nall+N;i++) bwv.write(i+"\n");
Nall += N;
}
if (arr[0].equals("gap")) {
int M = Integer.valueOf(arr[1]), N = Integer.valueOf(arr[2]);
int D0 = Integer.valueOf(arr[3]), Dgap = Integer.valueOf(arr[4]);
double C = Double.valueOf(arr[5]);
int Md = (int)(1.0*M/(Dgap-D0+1)), Nstart = Nall, i = Nstart;
for (int outd = D0;outd <= Dgap;outd++) {
for (int m = 0;m < Md;m++) {
int outdC = (int)(outd*C), outdN = outd - outdC;
HashSet<Integer> Ns = new HashSet<Integer>(), Cs = new HashSet<Integer>();
for (int d = 0;d < outdN;d++) Ns.add(Nstart+M+rand.nextInt(N));
for (int d = 0;d < outdC;d++) Cs.add(rand.nextInt(N0));
for (int j : Ns) {
nume += 1;
nume += 1;
bw.write(i+","+j+"\n");
}
for (int j : Cs) bw.write(i+","+j+"\n");
i += 1;
}
}
for (i = Nall;i < Nall+M;i++) bwu.write(i+"\n");
Nall += M;
for (i = Nall;i < Nall+N;i++) bwv.write(i+"\n");
Nall += N;
}
}
br.close();
bwv.close();
bwu.close();
bw.close();
bw = new BufferedWriter(new FileWriter(file_nodemap_injected));
bw.write("#"+Nall+","+nume+"\n");
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_nodemap)));
while ((line = br.readLine()) != null) {
if (line.charAt(0) == '#' || line.charAt(0) == '%') continue;
bw.write(line+"\n");
}
br.close();
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_injectedu)));
while ((line = br.readLine()) != null) {
bw.write(line+",suspfan"+line+"\n");
}
br.close();
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_injectedv)));
while ((line = br.readLine()) != null) {
bw.write(line+",suspidol"+line+"\n");
}
br.close();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* For every node [file_nodemap] in edge list [file_edgelist], store its in-degree and out-degree in [file_xindoutd].
* Give the out-degree distribution [file_outd2c] and in-degree distribution [file_ind2c].
*/
static void edgelist_to_dc(String file_edgelist, String file_nodemap, String file_xindoutd, String file_outd2c, String file_ind2c) {
String line = null;
try {
int N = number_of_nodes(file_nodemap);
BufferedWriter bw = new BufferedWriter(new FileWriter(file_xindoutd));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_edgelist)));
int[][] indoutd = new int[N][2];
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]), elem1 = Integer.valueOf(arr[1]);
indoutd[elem0][1] += 1;
indoutd[elem1][0] += 1;
}
br.close();
bw.close();
bw = new BufferedWriter(new FileWriter(file_xindoutd));
for (int i = 0;i < N;i++) bw.write(i+","+indoutd[i][0]+","+indoutd[i][1]+"\n");
bw.close();
HashMap<Integer, Integer> ind2c = new HashMap<Integer, Integer>(), outd2c = new HashMap<Integer, Integer>();
for (int i = 0;i < N;i++) {
int ind = indoutd[i][0], outd = indoutd[i][1];
if (!ind2c.containsKey(ind)) ind2c.put(ind, 0);
if (!outd2c.containsKey(outd)) outd2c.put(outd, 0);
ind2c.put(ind, ind2c.get(ind)+1);
outd2c.put(outd, outd2c.get(outd)+1);
}
ArrayList<Integer> ds = new ArrayList<Integer>();
for (int d : ind2c.keySet()) ds.add(d);
Collections.sort(ds);
bw = new BufferedWriter(new FileWriter(file_ind2c));
for (int d : ds) bw.write(d+","+ind2c.get(d)+"\n");
bw.close();
ds.clear();
for (int d : outd2c.keySet()) ds.add(d);
Collections.sort(ds);
bw = new BufferedWriter(new FileWriter(file_outd2c));
for (int d : ds) bw.write(d+","+outd2c.get(d)+"\n");
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Translate edge list [file_edgelist] of nodes [file_nodemap] into adjacency list:
* source node to list of destination nodes [file_adjacency_u2vs],
* destination nodes to list of source node [file_adjacency_v2us].
* Bucket sort - 1 Gigabytes for a piece of data.
*/
static void edgelist_to_adjacencylist(String file_edgelist, String file_adjacency_u2vs, String file_adjacency_v2us) {
String line = null;
String temphead = "_temp";
int MAX_SIZE_SLICE = 1000000000; // 1G
File file = new File(file_edgelist);
if (!file.exists()) return;
int rmod = (int)(1.0+file.length()/MAX_SIZE_SLICE);
HashSet<String> files_temp = new HashSet<String>();
if (rmod > 1) {
try {
HashMap<Integer, BufferedWriter> r2bw = new HashMap<Integer, BufferedWriter>();
for (int r = 0;r < rmod;r++) {
String file_temp = temphead+r;
files_temp.add(file_temp);
BufferedWriter bw = new BufferedWriter(new FileWriter(file_temp));
r2bw.put(r, bw);
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_edgelist)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]);
int r = elem0 % rmod;
r2bw.get(r).write(line+"\n");
}
br.close();
for (Integer r : r2bw.keySet()) r2bw.get(r).close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
files_temp.add(file_edgelist);
}
HashMap<Integer, String> adjacencylist = new HashMap<Integer, String>();
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file_adjacency_u2vs));
for (String file_temp : files_temp) {
adjacencylist.clear();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_temp)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]), elem1 = Integer.valueOf(arr[1]);
if (adjacencylist.containsKey(elem0)) {
String elems = adjacencylist.get(elem0)+";"+elem1;
if (elems.length() > LEN_ELEMS) {
bw.write(elem0+","+elems+"\n");
adjacencylist.remove(elem0);
} else adjacencylist.put(elem0, elems);
} else adjacencylist.put(elem0, String.valueOf(elem1));
}
br.close();
for (int key : adjacencylist.keySet()) bw.write(key+","+adjacencylist.get(key)+"\n");
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
if (rmod > 1) {
for (String file_temp : files_temp) {
if (file_temp.contains(temphead)) {
file = new File(file_temp);
if (file.exists()) file.delete();
}
}
}
files_temp.clear();
if (rmod > 1) {
try {
HashMap<Integer, BufferedWriter> r2bw = new HashMap<Integer, BufferedWriter>();
for (int r = 0;r < rmod;r++) {
String file_temp = temphead+r;
files_temp.add(file_temp);
BufferedWriter bw = new BufferedWriter(new FileWriter(file_temp));
r2bw.put(r, bw);
}
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_edgelist)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem1 = Integer.valueOf(arr[1]);
int r = elem1 % rmod;
r2bw.get(r).write(line+"\n");
}
br.close();
for (Integer r : r2bw.keySet()) r2bw.get(r).close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
files_temp.add(file_edgelist);
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file_adjacency_v2us));
for (String file_temp : files_temp) {
adjacencylist.clear();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_temp)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]), elem1 = Integer.valueOf(arr[1]);
if (adjacencylist.containsKey(elem1)) {
String elems = adjacencylist.get(elem1)+";"+elem0;
if (elems.length() > LEN_ELEMS) {
bw.write(elem1+","+elems+"\n");
adjacencylist.remove(elem1);
} else adjacencylist.put(elem1, elems);
} else adjacencylist.put(elem1, String.valueOf(elem0));
}
br.close();
for (int key : adjacencylist.keySet()) bw.write(key+","+adjacencylist.get(key)+"\n");
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
if (rmod > 1) {
for (String file_temp : files_temp) {
if (file_temp.contains(temphead)) {
file = new File(file_temp);
if (file.exists()) file.delete();
}
}
}
}
/**
* With adjacency list [file_adjacency_u2vs] and [file_adjacency_v2us],
* via HITS algorithm - calculate Hub score u1 and Authority score v1,
* with T iterations, store Hub vs. out-degree and Authority vs. in-degree.
*/
static void hits_degree_adjacency(String file_adjacency_u2vs, String file_adjacency_v2us, String file_nodemap,
String file_xindoutd, String file_xu1outd, String file_xv1ind, int T) {
String line = null;
double[] u1, v1;
double sum;
try {
int N = number_of_nodes(file_nodemap);
u1 = new double[N];
v1 = new double[N];
for (int i = 0;i < N;i++) v1[i] = 1.0;
for (int t = 0;t < T;t++) { // each iteration of HITS algorithm
sum = 0.0;
for (int i = 0;i < N;i++) sum += v1[i] * v1[i];
sum = Math.sqrt(sum);
for (int i = 0;i < N;i++) v1[i] /= sum;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_adjacency_u2vs)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]);
arr = arr[1].split(";");
for (String selem : arr) {
int elem1 = Integer.valueOf(selem);
u1[elem0] += v1[elem1];
}
}
br.close();
sum = 0.0;
for (int i = 0;i < N;i++) sum += u1[i] * u1[i];
sum = Math.sqrt(sum);
for (int i = 0;i < N;i++) u1[i] /= sum;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_adjacency_v2us)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]);
arr = arr[1].split(";");
for (String selem : arr) {
int elem1 = Integer.valueOf(selem);
v1[elem0] += u1[elem1];
}
}
br.close();
}
sum = 0.0;
for (int i = 0;i < N;i++) sum += v1[i] * v1[i];
sum = Math.sqrt(sum);
for (int i = 0;i < N;i++) v1[i] /= sum;
BufferedWriter bwu = new BufferedWriter(new FileWriter(file_xu1outd));
BufferedWriter bwv = new BufferedWriter(new FileWriter(file_xv1ind));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_xindoutd)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int i = Integer.valueOf(arr[0]), ind = Integer.valueOf(arr[1]), outd = Integer.valueOf(arr[2]);
bwu.write(i+","+u1[i]+","+outd+"\n");
bwv.write(i+","+v1[i]+","+ind+"\n");
}
br.close();
bwv.close();
bwu.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* With edge list [file_edgelist],
* via HITS algorithm - calculate Hub score u1 and Authority score v1,
* with T iterations, store Hub vs. out-degree and Authority vs. in-degree.
*/
static void hits_degree(String file_edgelist, String file_nodemap,
String file_xindoutd, String file_xu1outd, String file_xv1ind, int T) {
String line = null;
double[] u1, v1;
double sum;
try {
int N = number_of_nodes(file_nodemap);
u1 = new double[N];
v1 = new double[N];
for (int i = 0;i < N;i++) v1[i] = 1.0;
for (int t = 0;t < T;t++) { // each iteration of HITS algorithm
sum = 0.0;
for (int i = 0;i < N;i++) sum += v1[i] * v1[i];
sum = Math.sqrt(sum);
for (int i = 0;i < N;i++) v1[i] /= sum;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_edgelist)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]);
int elem1 = Integer.valueOf(arr[1]);
u1[elem0] += v1[elem1];
}
br.close();
sum = 0.0;
for (int i = 0;i < N;i++) sum += u1[i] * u1[i];
sum = Math.sqrt(sum);
for (int i = 0;i < N;i++) u1[i] /= sum;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_edgelist)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]);
int elem1 = Integer.valueOf(arr[1]);
v1[elem1] += u1[elem0];
}
br.close();
}
sum = 0.0;
for (int i = 0;i < N;i++) sum += v1[i] * v1[i];
sum = Math.sqrt(sum);
for (int i = 0;i < N;i++) v1[i] /= sum;
BufferedWriter bwu = new BufferedWriter(new FileWriter(file_xu1outd));
BufferedWriter bwv = new BufferedWriter(new FileWriter(file_xv1ind));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_xindoutd)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
int i = Integer.valueOf(arr[0]), ind = Integer.valueOf(arr[1]), outd = Integer.valueOf(arr[2]);
bwu.write(i+","+u1[i]+","+outd+"\n");
bwv.write(i+","+v1[i]+","+ind+"\n");
}
br.close();
bwv.close();
bwu.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* With Power-[P] method, divide scatter plot [file_scatter] into N1xN2 cells.
* See the position (cell) of each point in [file_xcell].
* See the cell plot in [file_cell]: first two lines are numbers of cells on axis
* and then we have N1xN2 numbers - frequency of points in each cell.
*/
static void scatter_to_cell(String file_scatter, String file_xcell, String file_cell, double P) {
String line = null;
double logP = Math.log(P);
int minc1 = 1000, maxc1 = -1000;
int minc2 = 1000, maxc2 = -1000;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_scatter)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
double d1 = Double.valueOf(arr[1]);
double d2 = Double.valueOf(arr[2]);
if (d1 == 0 || d2 == 0) continue;
int c1 = (int)(Math.log(d1)/logP);
int c2 = (int)(Math.log(d2)/logP);
minc1 = Math.min(minc1, c1);
maxc1 = Math.max(maxc1, c1);
minc2 = Math.min(minc2, c2);
maxc2 = Math.max(maxc2, c2);
}
br.close();
int Nc1 = maxc1-minc1+1, Nc2 = maxc2-minc2+1;
int[][] cells = new int[Nc1][Nc2];
BufferedWriter bw = new BufferedWriter(new FileWriter(file_xcell));
bw.write("#"+minc1+","+maxc1+","+minc2+","+maxc2+"\n");
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_scatter)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
double d1 = Double.valueOf(arr[1]);
double d2 = Double.valueOf(arr[2]);
if (d1 == 0 || d2 == 0) continue;
int c1 = (int)(Math.log(d1)/logP);
int c2 = (int)(Math.log(d2)/logP);
int nc1 = c1-minc1, nc2 = c2-minc2;
bw.write(arr[0]+","+nc1+","+nc2+"\n");
cells[nc1][nc2] += 1;
}
br.close();
bw.close();
bw = new BufferedWriter(new FileWriter(file_cell));
String s = "";
for (int c = minc1;c < maxc1;c++) {
s += Math.pow(P, (double)c)+",";
}
s += Math.pow(P, (double)maxc1)+"\n";
for (int c = minc2;c < maxc2;c++) {
s += Math.pow(P, (double)c)+",";
}
s += Math.pow(P, (double)maxc2)+"\n";
bw.write(s);
for (int i = 0;i < Nc1;i++) {
s = "";
for (int j = 0;j < Nc2-1;j++) {
s += cells[i][j]+",";
}
s += cells[i][Nc2-1]+"\n";
bw.write(s);
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Divide scatter plot [file_scatter] into [NxN] cells.
* See the position (cell) of each point in [file_xcell].
* See the cell plot in [file_cell]: first two lines are numbers of cells on axis
* and then we have [NxN] numbers - frequency of points in each cell.
* [mindeg] for lower bound of the 4th column: minimum out/in-degree
*/
static void scatter_to_cell_N(String file_scatter, String file_xcell, String file_cell,
String axis_log, int N, int mindeg) {
String AXIS_LOG_LOG = "log";
String line = null;
double mind1 = 1000000.0, maxd1 = -1000000.0;
double mind2 = 1000000.0, maxd2 = -1000000.0;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_scatter)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
double d1 = Double.valueOf(arr[1]);
double d2 = Double.valueOf(arr[2]);
if (mindeg > 0 && Integer.valueOf(arr[3]) < mindeg) continue;
if (axis_log.equals(AXIS_LOG_LOG)) {
if (d1 == 0 || d2 == 0) continue;
d1 = Math.log(d1);
d2 = Math.log(d2);
}
mind1 = Math.min(mind1, d1);
maxd1 = Math.max(maxd1, d1);
mind2 = Math.min(mind2, d2);
maxd2 = Math.max(maxd2, d2);
}
br.close();
int[][] cells = new int[N][N];
BufferedWriter bw = new BufferedWriter(new FileWriter(file_xcell));
bw.write("#"+N+"\n");
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_scatter)));
while ((line = br.readLine()) != null) {
String[] arr = line.split(",");
double d1 = Double.valueOf(arr[1]);
double d2 = Double.valueOf(arr[2]);
if (mindeg > 0 && Integer.valueOf(arr[3]) < mindeg) continue;
if (axis_log.equals(AXIS_LOG_LOG)) {
if (d1 == 0 || d2 == 0) continue;
d1 = Math.log(d1);
d2 = Math.log(d2);
}
int c1 = Math.min(N-1, (int)((d1-mind1)/(maxd1-mind1)*N));
int c2 = Math.min(N-1, (int)((d2-mind2)/(maxd2-mind2)*N));
bw.write(arr[0]+","+c1+","+c2+"\n");
cells[c1][c2] += 1;
}
br.close();
bw.close();
bw = new BufferedWriter(new FileWriter(file_cell));
String s = "";
for (int c = 0;c < N-1;c++) {
double num = mind1+(maxd1-mind1)*c/N;
if (axis_log.equals(AXIS_LOG_LOG)) num = Math.exp(num);
s += num+",";
}
if (axis_log.equals(AXIS_LOG_LOG)) maxd1 = Math.exp(maxd1);
s += maxd1+"\n";
for (int c = 0;c < N-1;c++) {
double num = mind2+(maxd2-mind2)*c/N;
if (axis_log.equals(AXIS_LOG_LOG)) num = Math.exp(num);
s += num+",";
}
if (axis_log.equals(AXIS_LOG_LOG)) maxd2 = Math.exp(maxd2);
s += maxd2+"\n";
bw.write(s);
for (int i = 0;i < N;i++) {
s = "";
for (int j = 0;j < N-1;j++) {
s += cells[i][j]+",";
}
s += cells[i][N-1]+"\n";
bw.write(s);
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* With [file_xcell]: node -> cellid (x-feature HITS, y-feature Degree),
* [file_cell]: cell -> frequency,
* [file_adjacency]: adjacency list, node's neighbors,
* calculate 'normalcy' and 'coherence' of neighbors.
*/
static void normalcy_coherence_old(String file_nodemap, String file_adjacency, String file_xcell, String file_cell, String file_norcohdeg) {
String temphead = "_temp";
int MAX_SIZE_SLICE = 100000000; // 100M
File file = new File(file_adjacency);
if (!file.exists()) return;
int rmod = (int)(1.0+file.length()/MAX_SIZE_SLICE);
// prepare temporal files - x->cid->foreground
HashSet<String> files_temp = new HashSet<String>();
HashMap<Integer, BufferedWriter> r2bw = new HashMap<Integer, BufferedWriter>();
try {
for (int r = 0;r < rmod;r++) {
String file_temp = temphead+r;
files_temp.add(file_temp);
BufferedWriter bw = new BufferedWriter(new FileWriter(file_temp));
r2bw.put(r, bw);
}
} catch (Exception e) {
e.printStackTrace();
}
String line = null;
String[] arr = null;
HashMap<String, Integer> cell2cid = null;
int[] x2cid = null;
int[] cid2bg = null;
try {
// cell of nodes (as the neighbors of the objective)
cell2cid = new HashMap<String, Integer>();
int N = number_of_nodes(file_nodemap);
x2cid = new int[N];
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_xcell)));
while ((line = br.readLine()) != null) {
if (line.charAt(0) == '#' || line.charAt(0) == '%') continue;
arr = line.split(",");
int x = Integer.valueOf(arr[0]);
String cell = arr[1]+","+arr[2];
if (!cell2cid.containsKey(cell)) cell2cid.put(cell, cell2cid.size());
x2cid[x] = cell2cid.get(cell);
}
br.close();
System.out.println("Already load cell->cid and x->cid!");
// frequency of background points of every cell
int C = cell2cid.size();
cid2bg = new int[C];
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_cell)));
arr = br.readLine().split(",");
int C1 = arr.length;
arr = br.readLine().split(",");
int C2 = arr.length;
for (int c1 = 0;c1 < C1;c1++) {
line = br.readLine();
arr = line.split(",");
for (int c2 = 0;c2 < C2;c2++) {
int freq = Integer.valueOf(arr[c2]);
if (freq > 0) {
String cell = c1+","+c2;
int cid = cell2cid.get(cell);
cid2bg[cid] = freq;
}
}
}
br.close();
System.out.println("Already load cid->background!");
// cell to frequency of foreground points of every node
HashMap<Integer, Integer> cid2fg = new HashMap<Integer, Integer>();
br = new BufferedReader(new InputStreamReader(new FileInputStream(file_adjacency)));
int linenum = 0;
while ((line = br.readLine()) != null) {
linenum += 1;
if (linenum % 100000 == 0) {
System.out.println("Loading x->cid->foreground: line "+linenum);
}
cid2fg.clear();
arr = line.split(",");
int elem0 = Integer.valueOf(arr[0]);
arr = arr[1].split(";");
for (String selem : arr) {
int elem1 = Integer.valueOf(selem);
int cid = x2cid[elem1];
int fg = 0;
if (cid2fg.containsKey(cid)) fg = cid2fg.get(cid);
fg += 1;
cid2fg.remove(cid);
cid2fg.put(cid, fg);
}
int r = elem0 % rmod;
String s = elem0+",";
for (int cid : cid2fg.keySet()) {
s += cid+"_"+cid2fg.get(cid)+";";
}
s = s.substring(0,s.length()-1);
r2bw.get(r).write(s+"\n");
}
br.close();
System.out.println("Already load x->cid->foreground!");
} catch (Exception e) {
e.printStackTrace();
}
try {
for (Integer r : r2bw.keySet()) r2bw.get(r).close();
} catch (Exception e) {
e.printStackTrace();
}
if (cid2bg == null) return;
try {
// normalcy and coherence
String[] arrcidfg = null;
BufferedWriter bw = new BufferedWriter(new FileWriter(file_norcohdeg));
for (String file_temp : files_temp) {
HashMap<Integer,HashMap<Integer, Integer>> x2cid2fg = new HashMap<Integer,HashMap<Integer, Integer>>();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file_temp)));
while ((line = br.readLine()) != null) {
arr = line.split(",");
int x = Integer.valueOf(arr[0]);
if (!x2cid2fg.containsKey(x)) x2cid2fg.put(x,new HashMap<Integer, Integer>());
arr = arr[1].split(";");
for (String cidfg : arr) {
arrcidfg = cidfg.split("_");
int cid = Integer.valueOf(arrcidfg[0]);
int fg = Integer.valueOf(arrcidfg[1]);
if (x2cid2fg.get(x).containsKey(cid)) {
fg += x2cid2fg.get(x).get(cid);
x2cid2fg.get(x).remove(cid);
}
x2cid2fg.get(x).put(cid, fg);
}
}
br.close();
for (int x : x2cid2fg.keySet()) {
double bgfg = 0.0, fgfg = 0.0, bg, fg;
int B = 0, F = 0;
for (int cid : x2cid2fg.get(x).keySet()) {
fg = x2cid2fg.get(x).get(cid);
bg = cid2bg[cid];
bgfg += bg*fg;
fgfg += fg*fg;
B += bg;
F += fg;
}
if (F > 0) {
double normalcy = bgfg/B/F, coherence = fgfg/F/F;
bw.write(x+","+normalcy+","+coherence+","+F+"\n");
}
}
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
for (String file_temp : files_temp) {
file = new File(file_temp);
if (file.exists()) file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void normalcy_coherence(String file_nodemap, String file_adjacency, String file_xcell, String file_cell, String file_norcohdeg) {
String temphead = "_temp";
int MAX_SIZE_SLICE = 100000000; // 100M
File file = new File(file_adjacency);
if (!file.exists()) return;
int rmod = (int)(1.0+file.length()/MAX_SIZE_SLICE);
// prepare temporal files - x->cid->foreground
HashSet<String> files_temp = new HashSet<String>();
HashMap<Integer, BufferedWriter> r2bw = new HashMap<Integer, BufferedWriter>();
try {
for (int r = 0;r < rmod;r++) {
String file_temp = temphead+r;
files_temp.add(file_temp);
BufferedWriter bw = new BufferedWriter(new FileWriter(file_temp));
r2bw.put(r, bw);
}
} catch (Exception e) {
e.printStackTrace();
}
String line = null;
String[] arr = null;
HashMap<String, Integer> cell2cid = null;
int[] x2cid = null;
int[] cid2bg = null;