简体   繁体   English

使用广度优先搜索:如何到达终点?

[英]Using breadth first search: how do I get to the end vertex?

I am not sure why my code is not returning the correct path vertices. 我不确定为什么我的代码没有返回正确的路径顶点。 It is returning [abc] instead of [acf],and I don't know why. 它返回[abc]而不是[acf],我不知道为什么。 Is there something I'm missing here or doing wrong in my algorithm? 我的算法中是否有我遗漏或做错的事情? Note: getNeighbors(String vertex) returns the connecting edges for the vertex in its parameter. 注意:getNeighbors(String vertex)返回其参数中顶点的连接边。

This is the test: My code stops at "assertEquals("c", route.next())" because it is returning "b" instead of a "c". 这是测试:我的代码停在“assertEquals(”c“,route.next())”因为它返回“b”而不是“c”。 my code' current output is [abc] and the expected is [acf] 我的代码'当前输出是[abc],预期是[acf]

public class PathingTest {

    @Test
    public void testPathing(){
        Graph cycle = new Graph("graphs/cycle.json");
        Iterator<String> route = cycle.getRoute("d", "b").iterator();
        assertEquals("d",route.next());
        assertEquals("b",route.next());
        assertFalse(route.hasNext());

        Graph tree = new Graph("graphs/tree.json");
        route = tree.getRoute("a", "f").iterator();
        assertEquals("a",route.next());
        assertEquals("c", route.next());
        assertEquals("f", route.next());
        assertFalse(route.hasNext());

        Graph disconnected = new Graph("graphs/disconnected.json");
        assertEquals(null, disconnected.getRoute("a", "f"));
    }
}

The queue variable and the visited variable have different purposes, but in your case, they get updated in the same way, which is not correct. queue变量和visited变量有不同的用途,但在您的情况下,它们会以相同的方式更新,这是不正确的。

Shortly, you add a node to the queue while processing its parent (which means that at some point in the future, that node is expecting to be processed too). 不久,你将节点添加到所述queue在处理其父(这意味着在未来某一时刻,该节点期望被处理过)。 Meanwhile, you only add a node to visited only after you have processed it (added its children to the queue). 同时,只有在处理完节点后才将其添加到visited的节点(将其子节点添加到队列中)。

Your while loop should look like this (please notice the position of the insertion into visited ). 你的while循环应该是这样的(请注意插入到visited的位置)。

while (!queue.isEmpty()) {
    String current = queue.remove();
    path.add(current);

    visited.add(current);

    if (current == end) {
        return path;
    }

    Iterator<String> neighbors = getNeighbors(start).iterator();
    while (neighbors.hasNext()) {
        String n = neighbors.next();

        if (!visited.contains(n)) {
            queue.add(n);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HashMap进行广度优先搜索 <Vertex, List<Vertex> &gt; - Breadth First Search using HashMap<Vertex, List<Vertex>> 如何在 Java 的广度优先搜索中将字符串顶点转换为 Integer 顶点 - How can i convert String Vertex to Integer vertex in Breadth First Search in Java 广度优先搜索:需要多少个顶点状态? - Breadth First Search: How many states of a vertex are needed? 如何在邻接矩阵的广度优先搜索中跟踪每个顶点的深度? (爪哇) - How can I track the depth of each vertex in a breadth first search of an adjacency matrix? (Java) 如何使用广度优先搜索在树中找到从一个顶点到另一个顶点的路径? - How to find a path from one vertex to another in a tree using breadth first search? 如何使用 Java 实现图的广度优先搜索? - How can I implement a Breadth first search of a graph using Java? 如何进行广度优先搜索 - How to do breadth-first search 如何使用广度优先搜索获得两个节点之间的最短路径? - How to get shortest path between two nodes with Breadth First Search? 如何使用广度优先搜索在迷宫中找到最短路径? - How to find the shortest path in a maze using breadth first search? 使用队列广度搜索第一个迷宫求解器 - breadth search first maze solver using queues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM