This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiGraphThreaded.java
More file actions
305 lines (259 loc) · 7.23 KB
/
DiGraphThreaded.java
File metadata and controls
305 lines (259 loc) · 7.23 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
/**
*
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.kccoder.fracture.Fracture;
import com.kccoder.fracture.IProcessor;
/**
*
* @author Andreas
*
*/
public class DiGraphThreaded<K> implements IDiGraph<K> {
private List<IVertex<K>> vertices; // Map holding vertices
private Map<K,Integer> vIndex; //
private BitSet bIntersection;
private List<IVertex<K>> intersection;
private final char[] CharacterSet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private static int addedBulk = 0;
private static int addedIter = 0;
// Aux
private HashMap<Character, BitSet> CIndex = new HashMap<Character, BitSet>();
private int E = 0; // Number of edges
private int V = 0; // Number of vertices
private int subkeysize = 4;
public DiGraphThreaded() {
init();
}
public DiGraphThreaded(int size) {
this.subkeysize = size;
init();
}
private void init() {
addedBulk = 0;
addedIter = 0;
// Initialize variables
vertices = new ArrayList<IVertex<K>>();
vIndex = new HashMap<K, Integer>();
for (char c : CharacterSet) CIndex.put(c, new BitSet());
bIntersection = new BitSet();
intersection = new ArrayList<IVertex<K>>();
}
/***
* Builds a graph consisting of the elements of type K
* @param col
* @
*/
public void buildGraph(List<K> col) {
for (K k : col) {
IVertex<K> v = new Vertex<K>(k, subkeysize);
vertices.add(v);
vIndex.put(k, V);
index(v);
V++;
}
addEdges();
//CIndex = null;
bIntersection = null;
intersection = null;
cern.colt.bitvector.BitVector qbv = new cern.colt.bitvector.BitVector(10);
}
public void buildGraph(String filename) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
// Read all lines in the input file
while(true) {
String s = r.readLine();
if (s == null) break;
s = s.toLowerCase();
IVertex<K> v = new Vertex((K) s, subkeysize); // Create a new vertex
vertices.add(v); // Vertex v has Index V
vIndex.put(v.getValue(), V);
index(v);
V++;
}
addEdgesThreaded();
CIndex = null;
bIntersection = null;
intersection = null;
for (IVertex<K> v : vertices) {
E += v.adj().size();
}
}
private void index(IVertex<K> v) {
for (Character c : v.getValueTable().keySet()) {
BitSet bs = CIndex.get(c);
if (bs == null) {
CIndex.put(c, new BitSet());
CIndex.get(c).set(V);
} else {
bs.set(V);
}
}
}
private void addEdges() {
for (IVertex<K> v : vertices) {
doIntersection(v.getSuffixTable().keySet());
intersection.remove(v);
if (!v.hasDuplicateChars()) {
addCalls(intersection.size(), true);
v.adj().addAll(intersection);
E+= intersection.size();
}
else {
for (IVertex<K> i : intersection) {
addCalls(1, false);
if (v.isNeighbour(i)) {
E++;
v.addEdge(i);
}
}
}
}
}
/***
* Returns the set of vertices which contains all characters in a given suffix
* @param v
* @return
* @
*/
private void doIntersection(Set<Character> suffix) {
bIntersection.clear();
intersection.clear();
Iterator<Character> it = suffix.iterator();
if (it.hasNext()) bIntersection = (BitSet) CIndex.get(it.next()).clone();
while (it.hasNext()) {
bIntersection.and(CIndex.get(it.next()));
if (bIntersection.cardinality() == 0) break;
}
// Get vertices from index position in the BitSet
for (int i = bIntersection.nextSetBit(0); i >= 0; i = bIntersection.nextSetBit(i+1)) {
intersection.add(vertices.get(i));
}
}
public List<IVertex<K>> getIntersection(Set<Character> suffix, List<IVertex<K>> gIntersection) {
BitSet _bIntersection = new BitSet();
Iterator<Character> it = suffix.iterator();
if (it.hasNext()) _bIntersection = (BitSet) getBitSet(it.next()).clone();
while (it.hasNext()) {
_bIntersection.and(getBitSet(it.next()));
if (_bIntersection.cardinality() == 0) break;
}
for (int i = _bIntersection.nextSetBit(0); i >= 0; i = _bIntersection.nextSetBit(i+1)) {
gIntersection.add(vertices.get(i));
}
return gIntersection;
}
public int V() {
return V;
}
public int E() {
return E;
}
public void addEdge(IVertex<K> v, IVertex<K> w) {
v.adj().add(w);
}
public void addEdge(IVertex<K> v, List<IVertex<K>> w) {
v.adj().addAll(w);
}
public List<IVertex<K>> adj(IVertex<K> v) {
return v.adj();
}
private static void addCalls(int i, boolean isBulk) {
if (isBulk) addedBulk += i;
else addedIter += i;
}
public boolean hasPath(IVertex<K> v, IVertex<K> w) {
BFS<K> bfs = new BFS<K>(this, v);
return bfs.hasPathTo(w);
}
public List<IVertex<K>> vertices() {
return vertices;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("DiGraph\nVERTICES:").append("\n");
for (IVertex<K> v : vertices) {
builder.append(v.toString()).append("\n");
}
builder.append("\nV=")
.append(V).append("\nE=").append(E).append("");
return builder.toString();
}
public IVertex<K> getByValue(K k) {
return vertices.get(vIndex.get(k));
}
private void addEdgesThreaded() {
int nThreads = Runtime.getRuntime().availableProcessors();
int step = 0;
step = V() >= 20000 ? 20000 : (int) (V() / (Math.log(V()) / Math.log(nThreads)));
//step = 20000;
int start = 0;
int end = step;
int length = V();
ArrayList<AddEdge> tasks = new ArrayList<AddEdge>();
while (end + step < length){
tasks.add(new AddEdge(start, end));
start += step;
end += step;
}
if (step % length != 0) tasks.add(new AddEdge(start, length));
Fracture.forEach(tasks, new IProcessor<AddEdge>() {
public void processElement(AddEdge arg0) {
arg0.addEdges();
}
});
tasks.clear();
tasks = null;
}
public BitSet getBitSet(char c) {
return CIndex.get(c);
}
public static long getBulkCount() {
return addedBulk;
}
public static long getIterCount() {
return addedIter;
}
class AddEdge {
int start;
int end;
public AddEdge(int start, int end) {
this.start = start;
this.end = end;
}
private void addEdges()
{
List<IVertex<K>> gIntersection = new ArrayList<IVertex<K>>();
List<IVertex<K>> _vertices = vertices();
for (IVertex<K> v : _vertices.subList(start, end)) {
gIntersection.clear();
gIntersection = getIntersection(v.getSuffixTable().keySet(), gIntersection);
gIntersection.remove(v);
if (gIntersection.size()>0) {
if (!v.hasDuplicateChars()) {
addCalls(gIntersection.size(), true);
v.addEdge(gIntersection);
}
else {
for (IVertex<K> i : gIntersection) {
if (v.isNeighbour(i)) {
addCalls(1, false);
v.addEdge(i);
}
}
}
}
}
}
}
}