我正在尝试使用 jung 库实现 kNN 图的创建。我现在需要检查图中是否已存在 2 个节点之间的链接。
到目前为止我的代码:
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);
}
}
我不知道如何执行此操作,因为 Link
只有一个没有任何参数的构造函数。
最佳答案
isNeighbor() 适用于无向图,或者如果您不关心两个节点如何连接(即现有边(如果有)的走向)。对于有向图,您可能需要 isPredecessor()。
关于java - 如何检查 DirectedSparsedGraph 是否包含具有这 2 个节点的节点之间的边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788850/