I am trying to implement creation of kNN graph using jung library. I have came to a moment where I need to check if a link between 2 Node
s already exists in the graph.
My code so far:
for (int i = 0; i < k; i++) {
for (Iterator<Node> it1 = mGraph.getVertices().iterator(); it1.hasNext();) {
Node n1 = (Node) it1.next();
double minDistance = 9999999;
Node toConnect = null;
for (Iterator<Node> it2 = mGraph.getVertices().iterator(); it2.hasNext();) {
Node n2 = (Node) it2.next();
double currDistance = this.getDistance(n1, n2);
if( currDistance < minDistance &&
mGraph.containsEdge( /* WHAT HERE */ ) ){
minDistance = currDistance;
toConnect = n2;
}
}
mGraph.addEdge(new Link(), n1, toConnect, EdgeType.DIRECTED);
}
}
I do not know how to do this since there is only one constructor for Link
without any parameters.
isNeighbor() works for undirected graphs or if you don't care how the two nodes are connected (that is, which way the existing edge, if any, goes) . For a directed Graph you probably want isPredecessor().