调试BFS树遍历算法
我正在独自完成这个项目,可以用另一双眼睛来观察这个项目,看看我做错了什么。第一个循环无限运行。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您正在添加邻居的第一个顶点而不检查它是否已经被访问过..这里:
这意味着您永远不会清空该队列..因为它以大小1开始,然后您在每次迭代中删除1个元素,但是您添加至少 1 个元素(您永远不会添加任何元素)。
I think you are adding the first vertex of neighbours without checking if it's already visited.. here:
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).
add_queue
对empty()
的定义是什么?这可能是一个糟糕的命名问题,但听起来好像
empty()
做了一些事情,而不仅仅是检查它是否为空(这可能被称为isEmpty( )
)。另外,看起来您总是在每个外部循环中(就在内部 while 之前)向 add_queue 添加至少 1 项,但每次迭代只从 add_queue 中删除一项。
What's
add_queue
's definition ofempty()
?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 calledisEmpty()
).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.
需要调查的几个地方:
graph.isVisited()
确实能够识别何时通过graph.visit()
访问了节点。graph.neighbor(start)
真的返回了 start 的邻居吗?并且不包括此列表中的开始?A few places to investigate:
graph.isVisited()
is actually recognizing when a node has been visited viagraph.visit()
.graph.neighbor(start)
truly returning start's neighbors? And not including start in this list?你的代码有点不清楚。 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.