@@ -51,41 +51,40 @@ import java.util.ArrayList;
5151import java.util.HashMap ;
5252import java.util.Random ;
5353
54-
5554public class NNDescentExample {
5655
5756 public static void main (String [] args ) {
5857 Random r = new Random ();
5958 int count = 1000 ;
6059 int k = 10 ;
61-
62- // 1. Create the nodes
63- ArrayList<Node > nodes = new ArrayList<Node > (count);
60+
61+ // Create the nodes
62+ ArrayList<Integer > nodes = new ArrayList<Integer > (count);
6463 for (int i = 0 ; i < count; i++ ) {
6564 // The value of our nodes will be an int
66- nodes. add(new Node< Integer > ( String . valueOf(i), r. nextInt(10 * count) ));
65+ nodes. add(r. nextInt(10 * count));
6766 }
68-
69- // 2. Instantiate and configure the build algorithm
67+
68+ // Instantiate and configure the build algorithm
7069 NNDescent builder = new NNDescent ();
7170 builder. setK(k);
72-
71+
7372 // early termination coefficient
7473 builder. setDelta(0.1 );
75-
74+
7675 // sampling coefficient
7776 builder. setRho(0.2 );
78-
77+
7978 builder. setMaxIterations(10 );
80-
79+
8180 builder. setSimilarity(new SimilarityInterface<Integer > () {
8281
8382 @Override
8483 public double similarity (Integer v1 , Integer v2 ) {
8584 return 1.0 / (1.0 + Math . abs(v1 - v2));
8685 }
8786 });
88-
87+
8988 // Optionnallly, define a callback to get some feedback...
9089 builder. setCallback(new CallbackInterface () {
9190
@@ -94,58 +93,56 @@ public class NNDescentExample {
9493 System . out. println(data);
9594 }
9695 });
97-
98- // 3. Run the algorithm and get computed graph
96+
97+ // Run the algorithm and get computed graph
9998 Graph<Integer > graph = builder. computeGraph(nodes);
100-
99+
101100 // Display neighborlists
102- for (Node n : nodes) {
103- NeighborList nl = graph. get (n);
101+ for (Integer n : nodes) {
102+ NeighborList nl = graph. getNeighbors (n);
104103 System . out. print(n);
105104 System . out. println(nl);
106105 }
107-
106+
108107 // Optionnally, we can test the builder
109108 // This will compute the approximate graph, and then the exact graph
110109 // and compare results...
111110 builder. test(nodes);
112-
113- // 4. Analyze the graph:
111+
112+ // Analyze the graph:
114113 // Count number of connected components
115114 System . out. println(graph. connectedComponents(). size());
116-
115+
117116 // Search a query (fast approximative algorithm)
118- System . out. println(graph. search (r. nextInt(10 * count), 1 ));
119-
117+ System . out. println(graph. fastSearch (r. nextInt(10 * count), 1 ));
118+
120119 // Count number of strongly connected components
121120 System . out. println(graph. stronglyConnectedComponents(). size());
122-
123- // Convert the graph to an online graph (to which we can add new nodes)
124- OnlineGraph<Integer > online_graph = new OnlineGraph<Integer > (graph);
125-
121+
126122 // Now we can add a node to the graph (using a fast approximate algorithm)
127- online_graph. addNode(
128- new Node<Integer > (" my new node 1" , r. nextInt(10 * count)));
123+ graph. fastAdd(r. nextInt(10 * count));
129124 }
130125}
131126```
132127
133128This will produce something like:
134129
135130```
136- {computed_similarities=64361, c=4542, iterations=6, computed_similarities_ratio=0.12885085085085085}
137- {computed_similarities=75008, c=4031, iterations=7, computed_similarities_ratio=0.15016616616616615}
138- {computed_similarities=86254, c=3201, iterations=8, computed_similarities_ratio=0.17268068068068068}
139- {computed_similarities=97291, c=2302, iterations=9, computed_similarities_ratio=0.19477677677677677}
140- {computed_similarities=108458, c=1634, iterations=10, computed_similarities_ratio=0.21713313313313312}
131+ ...
132+ {computed_similarities=58141, computed_similarities_ratio=0.1163983983983984, c=4426, iterations=5}
133+ {computed_similarities=69126, computed_similarities_ratio=0.1383903903903904, c=3962, iterations=6}
134+ {computed_similarities=80369, computed_similarities_ratio=0.1608988988988989, c=3575, iterations=7}
135+ {computed_similarities=91560, computed_similarities_ratio=0.1833033033033033, c=2777, iterations=8}
136+ {computed_similarities=102698, computed_similarities_ratio=0.2056016016016016, c=2074, iterations=9}
137+ {computed_similarities=114014, computed_similarities_ratio=0.22825625625625626, c=1317, iterations=10}
141138Theoretical speedup: 1.0
142- Computed similarities: 108458
143- Speedup ratio: 4.605469398292427
144- Correct edges: 8180 (81.8 %)
145- Quality-equivalent speedup: 3.767273967803205
146- 6
147- [(523,9520,0.08333333333333333 )]
148- 12
139+ Computed similarities: 114014
140+ Speedup ratio: 4.381040924798709
141+ Correct edges: 8220 (82.19999999999999 %)
142+ Quality-equivalent speedup: 3.6012156401845385
143+ 14
144+ [(6181,0.06666666666666667 )]
145+ 26
149146
150147```
151148
0 commit comments