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 pathWordLadderTest.java
More file actions
398 lines (339 loc) · 12.6 KB
/
WordLadderTest.java
File metadata and controls
398 lines (339 loc) · 12.6 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
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
import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
public class WordLadderTest {
private final BitSet bs1 = new BitSet(1000000);
private int iterations = 2;
private String datafile = "500000_alpha_5_output.txt";
@Test
public void Benchmark() throws IOException {
TestDiGraphThreaded();
//TestDiGraphAnt();
TestDiGraphCarrot();
}
@Test
public void TestDiGraphThreaded() throws IOException {
int suffix = 4;
String dir = System.getProperty("user.dir");
String filename = datafile;
Stopwatch w = new Stopwatch();
double elapsed = w.elapsedTime();
System.out.println("Building Threaded");
DiGraphThreaded<String> dgtAnt = null;
for (int i = 0; i < iterations; i++) {
dgtAnt = new DiGraphThreaded<String>(suffix);
dgtAnt.buildGraph(dir + "\\trunk\\wordladder\\ressources\\" + filename);
System.out.println("Iteration : " + (i+1) + ": " + (w.elapsedTime() - elapsed));
elapsed = w.elapsedTime();
}
System.out.println("Mean buildtime Iter: " + w.elapsedTime()/iterations);
System.out.println("Vertices: " + dgtAnt.V());
System.out.println("Edges: " + dgtAnt.E());
System.out.println("Added bulk: " + dgtAnt.getBulkCount());
System.out.println("Added iter: " + dgtAnt.getIterCount());
}
@Test
public void TestDiGraphAnt() throws IOException {
int suffix = 4;
String dir = System.getProperty("user.dir");
String filename = datafile;
Stopwatch w = new Stopwatch();
double elapsed = w.elapsedTime();
System.out.println("Building Ant ");
DiGraphThreadedAnt<String> dgt = null;
for (int i = 0; i < iterations; i++) {
dgt = new DiGraphThreadedAnt<String>(suffix);
dgt.buildGraph(dir + "\\trunk\\wordladder\\ressources\\" + filename);
System.out.println("Iteration : " + (i+1) + ": " + (w.elapsedTime() - elapsed));
elapsed = w.elapsedTime();
}
System.out.println("Mean buildtime Iter: " + w.elapsedTime()/iterations);
System.out.println("Vertices: " + dgt.V());
System.out.println("Edges: " + dgt.E());
System.out.println("Added bulk: " + dgt.getBulkCount());
System.out.println("Added iter: " + dgt.getIterCount());
}
@Test
public void TestDiGraphCarrot() throws IOException {
int suffix = 4;
String dir = System.getProperty("user.dir");
String filename = datafile;
Stopwatch w = new Stopwatch();
double elapsed = w.elapsedTime();
System.out.println("Building Carrot ");
DiGraphThreadedCarrot<String> dgt = null;
for (int i = 0; i < iterations; i++) {
dgt= new DiGraphThreadedCarrot<String>(suffix);
dgt.buildGraph(dir + "\\trunk\\wordladder\\ressources\\" + filename);
System.out.println("Iteration : " + (i+1) + ": " + (w.elapsedTime() - elapsed));
elapsed = w.elapsedTime();
}
System.out.println("Mean buildtime Iter: " + w.elapsedTime()/iterations);
System.out.println("Vertices: " + dgt.V());
System.out.println("Edges: " + dgt.E());
System.out.println("Added bulk: " + dgt.getBulkCount());
System.out.println("Added iter: " + dgt.getIterCount());
}
@Test
public void TestDiGraphProgram() throws IOException {
HashMap<String, String> cmdargs = new HashMap<String, String>();
cmdargs.put("df", "words-15046-data.txt");
cmdargs.put("tf", "words-5757-test.txt");
cmdargs.put("mc", "1");
Stopwatch w = new Stopwatch();
for (int i = 0; i < 10; i++ ) {
//DiGraphProgram dgp = new DiGraphProgram(cmdargs);
}
System.out.println(w.elapsedTime()/10);
}
//@Test
public void TestBuildGraph() throws IOException {
String dir = System.getProperty("user.dir");
Stopwatch w = new Stopwatch();
System.out.println("Building Digraph");
DiGraph<String> dg = new DiGraph<String>(1);
dg.buildGraph(dir + "\\trunk\\wordladder\\ressources\\5757_alpha_10_output.txt");
System.out.println("Buildtime: " + w.elapsedTime());
System.out.println("Vertices: " + dg.V());
System.out.println("Edges: " + dg.E());
System.out.println("Added bulk: " + dg.getBulkCount());
System.out.println("Added iter: " + dg.getIterCount());
int count = 0;
for (IVertex<String> iv : dg.vertices()) {
count += iv.adj().size();
}
System.out.println(count);
// IVertex<String> v1 = dg.getByValue("other");
// System.out.println("V1: " + v1.getValue() + ","+ v1.adj().size());
// IVertex<String> v2 = dg.getByValue("there");
// System.out.println("V2: " + v2.getValue() + ","+ v2.adj().size());
// System.out.printf("BFS starting from '%s'", v1.getValue());
//
// w = new Stopwatch();
// BFS<String> bfs = new BFS<String>(dg, v1);
// System.out.println("..." + w.elapsedTime());
// Assert.assertTrue(bfs.hasPathTo(v2));
//
// System.out.printf("Dist from %s to %s : %s\n", v1.getValue(), v2.getValue(), bfs.distTo(v2));
// Iterator<IVertex<String>> i = bfs.pathTo(v2).iterator();
// StringBuilder sb = new StringBuilder();
// while(i.hasNext()) sb.append(i.next().getValue()+ "-->");
// int idx = sb.lastIndexOf("-->");
// sb.replace(idx, sb.length(), "");
// System.out.printf("Shortest path from %s to %s : %s\n", v1.getValue(), v2.getValue(), sb.toString());
}
@Test
public void TestVertex() {
Vertex<String> v1 = new Vertex<String>("there");
Vertex<String> v2 = new Vertex<String>("heres");
Vertex<String> v3 = new Vertex<String>("other");
Vertex<String> v4 = new Vertex<String>("their");
Assert.assertTrue(v1.isNeighbour(v2));
Assert.assertFalse(v2.isNeighbour(v1));
System.out.println("v3-->v4");
Assert.assertTrue(v3.isNeighbour(v4));
System.out.println("v4-->v3");
Assert.assertFalse(v4.isNeighbour(v3));
}
@Test
public void TestBigFiles() throws IOException {
System.out.print("Testing big files");
String dir = System.getProperty("user.dir");
for (int i = 1; i< 6; i++) {
Stopwatch w = new Stopwatch();
DiGraph<String> dg = null;
dg = new DiGraph<String>();
dg.buildGraph(dir + "\\trunk\\" + "alpha_" + i * 20000 + "_output.txt");
System.out.print("Built Digraph using Iteration");
System.out.println("Build time " + w.elapsedTime());
System.out.println("Vertices: " + dg.V());
System.out.println("Edges: " + dg.E());
System.out.println("Calls during build: " + dg.getIterCount());
}
}
@Test
public void TestLongSuffixes() throws IOException {
System.out.print("Testing long suffixes");
String dir = System.getProperty("user.dir");
Stopwatch w = new Stopwatch();
DiGraph<String> dg = null;
dg = new DiGraph<String>(19);
dg.buildGraph(dir + "\\trunk\\" + "5757_alpha_20_output.txt");
System.out.println("Built Digraph using Iteration");
System.out.println("Build time " + w.elapsedTime());
System.out.println("Vertices: " + dg.V());
System.out.println("Edges: " + dg.E());
System.out.println("Calls during build: " + dg.getIterCount());
}
@Test
public void TesStaticKeyLength() throws IOException {
String dir = System.getProperty("user.dir");
for (int i = 10; i<= 10; i++) {
Stopwatch w = new Stopwatch();
DiGraph<String> dg = null;
dg = new DiGraph<String>(7);
dg.buildGraph(dir + "\\trunk\\wordladder\\ressources\\" + i*10000 + "_random_alpha_10_output.txt");
System.out.print("Built Digraph using Iteration");
System.out.println("Build time " + w.elapsedTime());
System.out.println("Vertices: " + dg.V());
System.out.println("Edges: " + dg.E());
System.out.println("Calls during build: " + dg.getIterCount());
}
}
@Test
public void TestDynamicKeyLength() throws IOException {
String dir = System.getProperty("user.dir");
for (int i = 1; i<= 10; i++) {
Stopwatch w = new Stopwatch();
DiGraph<String> dg = null;
dg = new DiGraph<String>(i);
dg.buildGraph(dir + "\\trunk\\wordladder\\ressources\\" + i*10000 + "_random_alpha_10_output.txt");
System.out.println("Built Digraph using Iteration");
System.out.println("Key length");
System.out.println("Suffix length " + i);
System.out.println("Build time " + w.elapsedTime());
System.out.println("Vertices: " + dg.V());
System.out.println("Edges: " + dg.E());
System.out.println("Calls during build: " + dg.getIterCount());
}
}
@Test
public void TestHashTable() {
Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
ht.put("AN", 2);
ht.put("AN", 2);
ht.put("AN", 2);
System.out.println(ht);
ht.remove("AN");
System.out.println(ht);
}
@Test
public void TestRefs() {
String[] c = {"a","b", "c"};
System.out.println(c);
String[] b = c;
System.out.println(b);
b = null;
System.out.println(c);
System.out.println(b);
Set<Character> set = new HashSet<Character>();
String str = "abc";
for (char t : str.toCharArray())
set.add(t);
System.out.println("a".charAt(0));
String string = "abcdefghijkl";
System.out.println(string.substring(0, 3));
System.out.println(string.substring(3, 3));
}
@Test
public void TestLogicalAnd() {
BitSet bs1 = new BitSet(1000000);
BitSet bs2 = new BitSet(4000);
BitSet bs3 = new BitSet(300);
BitSet bs4 = new BitSet(100000);
for (int i = 0; i < 1000; i++) {
bs1.set((int)StdRandom.uniform(bs1.size()));
bs2.set((int)StdRandom.uniform(bs2.size()));
bs3.set((int)StdRandom.uniform(bs3.size()));
bs4.set((int)StdRandom.uniform(bs4.size()));
}
ArrayList<BitSet> bitsets = new ArrayList<BitSet>();
bitsets.add(bs1);
bitsets.add(bs4);
bitsets.add(bs2);
bitsets.add(bs3);
for (BitSet bs : bitsets)
System.out.print(bs.size() + ";");
BitSet tmp2 = bitsets.get(0);
System.out.println(tmp2.equals(bitsets.get(0)));
Stopwatch w = new Stopwatch();
for (int i = 0; i < 50000000; i++) {
BitSet intersection = (BitSet) bitsets.get(0);
for (int j = 1; j < bitsets.size(); j++)
intersection.and(bitsets.get(j));
}
System.out.println(w.elapsedTime());
System.out.println(tmp2.equals(bitsets.get(0)));
bitsets.clear();
bitsets.add(bs3);
bitsets.add(bs2);
bitsets.add(bs4);
bitsets.add(bs1);
w = new Stopwatch();
for (int i = 0; i < 50000000; i++) {
BitSet tmp = (BitSet) bitsets.get(0);
BitSet intersection = (BitSet) bitsets.get(0);
for (int j = 1; j < bitsets.size(); j++)
intersection.and(bitsets.get(j));
bitsets.remove(0);
bitsets.add(0, tmp);
}
System.out.println(w.elapsedTime());
System.out.println("");
for (BitSet bs : bitsets)
System.out.print(bs.size() + ";");
//Collections.sort(bitsets, DiGraphThreaded.bsComp);
System.out.println("");
for (BitSet bs : bitsets)
System.out.print(bs.size() + ";");
}
@Test
public void testClone() {
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(20000000);
int iters = 2;
System.out.println("iter: " + iters);
for (ArrayList<Integer> al : list) {
for (int i = 0; i < list.size();i++) {
al.add(i);
}
list.add(al);
}
StringBuilder sb = new StringBuilder();
Stopwatch w ;
double elapsed = 0.0;
for (int i = 0 ; i<iters; i++) {
Collections.shuffle(list);
w = new Stopwatch();
ArrayList<ArrayList<Integer>> sublist1 = new ArrayList<ArrayList<Integer>>();
sublist1.addAll(list);
elapsed += w.elapsedTime();
}
sb.append("addAll: " + elapsed+ "\n");
elapsed = 0.0;
for (int i = 0 ; i<iters; i++) {
Collections.shuffle(list);
w = new Stopwatch();
ArrayList<ArrayList<Integer>> sublist2 = new ArrayList<ArrayList<Integer>>();
Collections.copy(sublist2, list);
elapsed += w.elapsedTime();
}
sb.append("copy: " + elapsed+ "\n");
elapsed = 0.0;
for (int i = 0 ; i<iters; i++) {
Collections.shuffle(list);
w = new Stopwatch();
ArrayList<ArrayList<Integer>> tmp = new ArrayList<ArrayList<Integer>>();
for (ArrayList v : list) tmp.add(v);
ArrayList<ArrayList<Integer>> sublist2 = new ArrayList<ArrayList<Integer>>(tmp);
elapsed += w.elapsedTime();
}
sb.append("tmp new: " + elapsed+ "\n");
System.out.println(sb.toString());
// EWAHCompressedBitmap ewahBitmap1 = new EWAHCompressedBitmap();
// EWAHCompressedBitmap ewahBitmap2 = new EWAHCompressedBitmap();
// sb.append("ewah: " + elapsed+ "\n");
// for (int i = 0; i<80000;i++) {
// ewahBitmap1.set(StdRandom.uniform(1000000));
// ewahBitmap2.set(StdRandom.uniform(1000000));
// }
// EWAHCompressedBitmap xorbitmap = ewahBitmap1.xor(ewahBitmap2);
sb.append("ewah: " + elapsed+ "\n");
}
}