Skip to content

Commit e43280a

Browse files
committed
Simplified Pruning example
1 parent cb2485c commit e43280a

File tree

2 files changed

+25
-42
lines changed

2 files changed

+25
-42
lines changed

src/main/java/info/debatty/java/graphs/Graph.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ public final T first() throws NoSuchElementException {
176176
* @param threshold
177177
*/
178178
public final void prune(final double threshold) {
179-
for (NeighborList nl : map.values()) {
180179

181-
nl.prune(threshold);
180+
for (NeighborList nl : map.values()) {
181+
nl.prune(threshold);
182182
}
183183
}
184184

@@ -1152,3 +1152,18 @@ public final boolean equals(final Object obj) {
11521152
return this.map.equals(other.map);
11531153
}
11541154
}
1155+
1156+
class RunnablePrune implements Runnable {
1157+
1158+
private final NeighborList nl;
1159+
private final double threshold;
1160+
1161+
RunnablePrune(final NeighborList nl, final double threshold) {
1162+
this.nl = nl;
1163+
this.threshold = threshold;
1164+
}
1165+
1166+
public void run() {
1167+
nl.prune(threshold);
1168+
}
1169+
}

src/main/java/info/debatty/java/graphs/examples/PruneExample.java

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@
2525

2626
import info.debatty.java.graphs.Graph;
2727
import info.debatty.java.graphs.SimilarityInterface;
28-
import info.debatty.java.graphs.build.NNDescent;
28+
import info.debatty.java.graphs.build.ThreadedNNDescent;
2929
import java.util.ArrayList;
3030
import java.util.Random;
31+
import java.util.concurrent.ExecutionException;
3132

3233
public class PruneExample {
3334

34-
public static void main(String[] args) {
35+
public static void main(String[] args) throws InterruptedException, ExecutionException {
3536
Random r = new Random();
36-
int count = 50000;
37-
int k = 10;
37+
int count = 100000;
38+
int k = 20;
3839

3940
// Create the nodes
4041
ArrayList<Integer> nodes = new ArrayList<Integer>(count);
@@ -44,9 +45,9 @@ public static void main(String[] args) {
4445
}
4546

4647
// Instantiate and configure the build algorithm
47-
NNDescent builder = new NNDescent();
48+
ThreadedNNDescent builder = new ThreadedNNDescent();
4849
builder.setK(k);
49-
builder.setDelta(0.01);
50+
builder.setDelta(0.1);
5051
builder.setRho(0.5);
5152

5253
builder.setSimilarity(new SimilarityInterface<Integer>() {
@@ -60,44 +61,11 @@ public double similarity(final Integer v1, final Integer v2) {
6061
// Run the algorithm and get computed graph
6162
Graph<Integer> graph = builder.computeGraph(nodes);
6263

64+
System.out.println("Start prunning...");
6365
long start = System.nanoTime();
6466
graph.prune(0.5);
6567
long end = System.nanoTime();
6668
System.out.println("n = " + count);
6769
System.out.println("time: " + (end - start));
68-
69-
count = 100000;
70-
k = 10;
71-
72-
// Create the nodes
73-
nodes = new ArrayList<Integer>(count);
74-
for (int i = 0; i < count; i++) {
75-
// The value of our nodes will be an int
76-
nodes.add(r.nextInt());
77-
}
78-
79-
// Instantiate and configure the build algorithm
80-
builder = new NNDescent();
81-
builder.setK(k);
82-
builder.setDelta(0.01);
83-
builder.setRho(0.5);
84-
85-
builder.setSimilarity(new SimilarityInterface<Integer>() {
86-
87-
@Override
88-
public double similarity(final Integer v1, final Integer v2) {
89-
return 1.0 / (1.0 + Math.abs(v1 - v2));
90-
}
91-
});
92-
93-
// Run the algorithm and get computed graph
94-
graph = builder.computeGraph(nodes);
95-
96-
start = System.nanoTime();
97-
graph.prune(0.5);
98-
end = System.nanoTime();
99-
System.out.println("n = " + count);
100-
System.out.println("time: " + (end - start));
101-
10270
}
10371
}

0 commit comments

Comments
 (0)