gpt4 book ai didi

java - 如何检查 DirectedSparsedGraph 是否包含具有这两个节点的节点之间的边?

转载 作者:行者123 更新时间:2023-11-30 09:08:04 25 4
gpt4 key购买 nike

我正在尝试使用 jung 库创建 kNN 图。我到了需要检查图中是否已存在 2 个 Node 之间的链接的时刻。

到目前为止我的代码:

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 是否包含具有这两个节点的节点之间的边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com