调试BFS树遍历算法

发布于 2024-09-05 02:00:31 字数 1046 浏览 5 评论 0原文

我正在独自完成这个项目,可以用另一双眼睛来观察这个项目,看看我做错了什么。第一个循环无限运行。

public void bfs(String start)
    {   
        //Initial Case
        add_queue.add(start);
        graph.visit(start);

        Iterator<String> neighbors;
        String neighbor;

        while(!add_queue.empty())
        {
            neighbors = graph.neighbors(start);
            neighbor = neighbors.next();
            graph.visit(neighbor);
            add_queue.add(neighbor);
            while(neighbors.hasNext())
            {
                neighbor = neighbors.next();
                if(!graph.isVisited(neighbor))  //If vertex is not visited it is new and is added to the queue
                {
                    add_queue.add(neighbor);
                    graph.visit(neighbor);
                }

            }   
            start = add_queue.remove();
            remove_queue.add(start);    //transfers vertex from add_queue to remove queue so that the order that the vertices were traversed is stored in memory    
        }
    }

I'm working alone on this project and could use another set of eyes to look at this to see what I am doing wrong. The first loop runs infinitely.

public void bfs(String start)
    {   
        //Initial Case
        add_queue.add(start);
        graph.visit(start);

        Iterator<String> neighbors;
        String neighbor;

        while(!add_queue.empty())
        {
            neighbors = graph.neighbors(start);
            neighbor = neighbors.next();
            graph.visit(neighbor);
            add_queue.add(neighbor);
            while(neighbors.hasNext())
            {
                neighbor = neighbors.next();
                if(!graph.isVisited(neighbor))  //If vertex is not visited it is new and is added to the queue
                {
                    add_queue.add(neighbor);
                    graph.visit(neighbor);
                }

            }   
            start = add_queue.remove();
            remove_queue.add(start);    //transfers vertex from add_queue to remove queue so that the order that the vertices were traversed is stored in memory    
        }
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱娆 2024-09-12 02:00:31

我认为您正在添加邻居的第一个顶点而不检查它是否已经被访问过..这里:

neighbor = neighbors.next(); <- you get first
graph.visit(neighbor); <- you visit
add_queue.add(neighbor); <- you add it without any check
while(neighbors.hasNext())
{
  neighbor = neighbors.next();
  if(!graph.isVisited(neighbor)) <- you do check for the others
  {
     add_queue.add(neighbor);
     graph.visit(neighbor);
  }
}

这意味着您永远不会清空该队列..因为它以大小1开始,然后您在每次迭代中删除1个元素,但是您添加至少 1 个元素(您永远不会添加任何元素)。

I think you are adding the first vertex of neighbours without checking if it's already visited.. here:

neighbor = neighbors.next(); <- you get first
graph.visit(neighbor); <- you visit
add_queue.add(neighbor); <- you add it without any check
while(neighbors.hasNext())
{
  neighbor = neighbors.next();
  if(!graph.isVisited(neighbor)) <- you do check for the others
  {
     add_queue.add(neighbor);
     graph.visit(neighbor);
  }
}

This means that you will never empty that queue.. since it starts with a size of 1, then you remove 1 element on each iteration but you add at least 1 element (you never add noone).

放我走吧 2024-09-12 02:00:31

add_queueempty() 的定义是什么?

这可能是一个糟糕的命名问题,但听起来好像 empty() 做了一些事情,而不仅仅是检查它是否为空(这可能被称为 isEmpty( ))。

另外,看起来您总是在每个外部循环中(就在内部 while 之前)向 add_queue 添加至少 1 项,但每次迭代只从 add_queue 中删除一项。

What's add_queue's definition of empty()?

It could be a bad naming issue, but it sounds like empty() does something, not just checks whether it is empty (which would be probably called isEmpty()).

Also, it looks like you always add at least 1 to add_queue in each outer loop (right before the inner while), but only remove one item from add_queue per iteration.

云巢 2024-09-12 02:00:31

需要调查的几个地方:

  1. 检查以确保 graph.isVisited() 确实能够识别何时通过 graph.visit() 访问了节点。
  2. graph.neighbor(start) 真的返回了 start 的邻居吗?并且不包括此列表中的开始?

A few places to investigate:

  1. Check to make sure that graph.isVisited() is actually recognizing when a node has been visited via graph.visit().
  2. Is graph.neighbor(start) truly returning start's neighbors? And not including start in this list?
執念 2024-09-12 02:00:31

你的代码有点不清楚。 graph.neighbors 到底返回什么?

一般来说,要进行 BFS,您需要将当前节点的添加到队列中,而不是它的邻居。由于所有内容都进入队列,这将确保您以正确的顺序访问树中的每个节点。假设它是一棵树而不是一般图,这也将确保您不会多次访问某个节点,从而允许您删除对 isVisited 的检查。

因此,从队列中取出下一个节点,将其所有子节点添加到队列中,访问该节点,然后重复,直到队列为空。

Your code is a little unclear. What exactly does graph.neighbors return?

In general to do a BFS you want to add the children of the current node to the queue, not the neighbors of it. Since it's all going into a queue this will ensure that you visit each node in the tree in the correct order. Assuming that it's a tree and not a general graph, this will also ensure that you don't visit a node more than once, allowing you to remove the checks to isVisited.

So, get the next node out of the queue, add all of it's children to the queue, visit the node, and repeat, until the queue is empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文