forked from stamps/FAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBergerShorFAS.java
More file actions
206 lines (164 loc) · 5.03 KB
/
BergerShorFAS.java
File metadata and controls
206 lines (164 loc) · 5.03 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
/*
FAS via DFS back edges
Copyright (C) 2016 by
Michael Simpson <simpsonm@uvic.ca>
All rights reserved.
BSD license.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.BitSet;
import it.unimi.dsi.webgraph.ImmutableGraph;
import it.unimi.dsi.webgraph.NodeIterator;
public class BergerShorFAS {
String basename; // input graph basename
ImmutableGraph G, I; // graph G
int n; // number of vertices in G
long e; // number of edges in G
int[] A; // array of nodes to be sorted
Random rand = new Random();
BitSet deleted;
// load graph and initialize class variables
public BergerShorFAS(String basename) throws Exception {
this.basename = basename;
System.out.println("Loading graph...");
G = ImmutableGraph.load(basename); //We need random access
System.out.println("Graph loaded");
System.out.println("Loading transpose graph...");
I = ImmutableGraph.load(basename+"-t"); //We need random access
System.out.println("Graph loaded");
n = G.numNodes();
e = G.numArcs();
System.out.println("n="+n);
System.out.println("e="+e);
A = new int[n];
for(int i = 0; i < A.length; i++) {
A[i] = i;
}
deleted = new BitSet(n);
}
public void shuffle(int[] array) {
int count = array.length;
for (int i = count; i > 1; i--) {
swap(array, i - 1, rand.nextInt(i));
}
}
public void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public long computeFAS() throws Exception {
// String outfile = basename + "_skews.txt";
// PrintWriter writer = new PrintWriter(outfile, "UTF-8");
String outfile_removed = basename + "_removed_edges_BS";
PrintWriter writer_removed = new PrintWriter(outfile_removed, "UTF-8");
shuffle(A);
long mag = 0;
int in;
int out;
int w;
for (int v = 0; v < A.length; v++) {
in = 0;
out = 0;
int[] in_neighbors = I.successorArray(A[v]);
int in_deg = I.outdegree(A[v]);
for(int x = 0; x < in_deg; x++) {
w = in_neighbors[x];
if (!deleted.get(w)) {
in++;
}
}
int[] out_neighbors = G.successorArray(A[v]);
int out_deg = G.outdegree(A[v]);
for(int x = 0; x < out_deg; x++) {
w = out_neighbors[x];
if (!deleted.get(w)) {
out++;
}
}
if (in+out > 0) {
if (in > out) {
mag += in;
// writer.println((double)(out) / (in+out));
// write out the removed edges: all outgoing edges
for (int j =0; j < in_deg; j++){
writer_removed.printf("%d\t%d\n", in_neighbors[j], A[v]);
}
} else {
mag += out;
// writer.println((double)(in) / (in+out));
// write out the removed edges: all in-coming edges
for (int j =0; j < out_deg; j++){
writer_removed.printf("%d\t%d\n", A[v], out_neighbors[j]);
}
}
}
deleted.set(A[v]);
}
// writer.close();
writer_removed.close();
long fas = e - mag;
System.out.println("fas size is " + fas);
return fas;
}
/*
METHOD UNNECESSARILY USES HASH SETS
public int computeFAS() throws Exception {
shuffle(A);
int mag = 0;
int in;
int out;
int w;
for (int v = 0; v < A.length; v++) {
in = 0;
out = 0;
int[] in_neighbors = I.successorArray(v);
int in_deg = G.indegree(v);
for(int x = 0; x < in_deg; x++) {
w = in_neighbors[x];
if (!deleted.contains(new Pair(w,v))) {
in++;
}
}
int[] out_neighbors = G.successorArray(v);
int out_deg = G.outdegree(v);
for(int x = 0; x < out_deg; x++) {
w = out_neighbors[x];
if (!deleted.contains(new Pair(v,w))) {
out++;
}
}
if (in > out) {
mag += in;
} else {
mag += out;
}
for(int x = 0; x < out_deg; x++) {
w = out_neighbors[x];
deleted.add(new Pair(v,w));
}
for(int x = 0; x < in_deg; x++) {
w = in_neighbors[x];
deleted.add(new Pair(w,v));
}
}
return e - mag;
} */
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
//args = new String[] {"cnr-2000"};
// args = new String[] {"wordassociation-2011"};
if(args.length != 1) {
System.out.println("Usage: java dfsFAS basename");
System.out.println("Output: FAS statistics");
return;
}
System.out.println("Starting " + args[0]);
BergerShorFAS fas = new BergerShorFAS(args[0]);
fas.computeFAS();
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println(args[0] + ": Time elapsed = " + estimatedTime/1000.0);
}
}