info.debatty.java.graphs.build.Brute Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-graphs Show documentation
Show all versions of java-graphs Show documentation
Algorithms that build k-nearest neighbors graph (k-nn graph): Brute-force, NN-Descent,...
The newest version!
package info.debatty.java.graphs.build;
import info.debatty.java.graphs.Graph;
import info.debatty.java.graphs.Neighbor;
import info.debatty.java.graphs.NeighborList;
import info.debatty.java.graphs.SimilarityInterface;
import java.util.List;
/**
*
* @author Thibault Debatty
* @param
*/
public class Brute extends GraphBuilder {
@Override
protected final Graph computeGraph(
final List nodes,
final int k,
final SimilarityInterface similarity) {
int n = nodes.size();
// Initialize all NeighborLists
Graph graph = new Graph();
for (T node : nodes) {
graph.put(node, new NeighborList(k));
}
for (int i = 0; i < n; i++) {
T n1 = nodes.get(i);
for (int j = 0; j < i; j++) {
T n2 = nodes.get(j);
double sim = similarity.similarity(n1, n2);
graph.getNeighbors(n1).add(new Neighbor(n2, sim));
graph.getNeighbors(n2).add(new Neighbor(n1, sim));
}
}
return graph;
}
}