如何更改我的算法以获取每次迭代的最远节点?

发布于 2025-02-11 04:47:19 字数 1580 浏览 1 评论 0 原文

因此,我试图为每次迭代选择最远的节点。我首先随机选择第一个节点,然后继续选择第一个节点最远的节点。

从那里,我有两个节点,我想在距离(我使用欧几里得度量)中与其他节点进行比较。然后,我添加一个已经选择的节点并继续此过程,直到我选择k节点。

Node maxDistNode = null;
        k = k - pickedNodes.size(); //in this case pickedNodes has 2 entries already
        while(k > 0) {
            for(Node pickedN: pickedNodes) {
                for(Node notpickedN: allNodes) { 
                    if(notpickedN.distance < distanceBetween(notpickedN, pickedN)) { //distanceBetween(Node a, Node b) is method that returns Math.hypot(a.x - b.x, a.x - b.x)
                        notpickedN.distance = distanceBetween(notpickedN, pickedN);
                    }
                }
                maxDistNode = Collections.max(allNodes, new Comparator<Node>() {
                    @Override
                    public int compare(Node o1, Node o2) {
                        if(o1.distance > o2.distance) {
                            return 1; 
                        }
                        if(o1.distance == o2.distance) {
                            return 0;
                        }
                        return -1;
                    }
                });
            }
            if(maxDistNode != null) {
                maxDistNode.setPicked(true);
                pickedNodes.add(maxDistNode);
                allNodes.remove(maxDistNode);
                --k;
            }
            
        }

节点最初具有integer.min_value的距离。现在的问题是,当k很小时,这可以很好地工作,尽管每当我想搜索更多节点时,这都会为我提供相同的k-times。因此,我留下了许多重复的节点。 如何更改此操作,以便可以在所有新添加和旧的picknodes上获得最远的节点?

So I am trying to pick out the farthest node for each iteration. I start by randomly picking the first node, then continueing by picking the farthest node from the first one.

From there I have two nodes which I want to compare in distance (I am using the euclidean metric) with the rest of the nodes. Then I add the one that is farthest away over both already picked nodes and continue this process, until I have chosen k nodes.

Node maxDistNode = null;
        k = k - pickedNodes.size(); //in this case pickedNodes has 2 entries already
        while(k > 0) {
            for(Node pickedN: pickedNodes) {
                for(Node notpickedN: allNodes) { 
                    if(notpickedN.distance < distanceBetween(notpickedN, pickedN)) { //distanceBetween(Node a, Node b) is method that returns Math.hypot(a.x - b.x, a.x - b.x)
                        notpickedN.distance = distanceBetween(notpickedN, pickedN);
                    }
                }
                maxDistNode = Collections.max(allNodes, new Comparator<Node>() {
                    @Override
                    public int compare(Node o1, Node o2) {
                        if(o1.distance > o2.distance) {
                            return 1; 
                        }
                        if(o1.distance == o2.distance) {
                            return 0;
                        }
                        return -1;
                    }
                });
            }
            if(maxDistNode != null) {
                maxDistNode.setPicked(true);
                pickedNodes.add(maxDistNode);
                allNodes.remove(maxDistNode);
                --k;
            }
            
        }

Nodes have initially a distance of Integer.MIN_VALUE given. Now the problem is, that this works fine for when k is very small, though when ever I want to search for more nodes this will give me the same one for k-times. Therefore I am left with many duplicate nodes.
How can I change this so I can get the farthest node over all newly added and old pickNodes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文