- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试根据我的研究在 JAVA 中实现 BFS 算法,但我有点困惑,我不确定我是在检查节点是否是目标,还是在将节点添加到探索列表中适当的地方。代码如下:
frontier.add(nodes.getFirst());//node.isGoal is only true if the node is desired node
if(frontier.getFirst().isGoal)//Checking if the root node is goal
{
explored.add(frontier.getFirst());
prev.put(frontier.getFirst(), null);
goalNode = explored.getFirst();
frontier.poll();
}
while (!frontier.isEmpty())
{
currentNode = frontier.poll();
explored.add(currentNode);
for (Node node : currentNode.children) {//adding node children to fronier and checking if they are goal
if (!frontier.contains(node) || !explored.contains(node)) {
frontier.add(node);
prev.put(node, currentNode);//mapping nodes to parent node to return sequence of nodes
if (node.isGoal) {
goalNode = node;
break;
}
}
}
}
提前致谢。
最佳答案
根据我对您代码的理解,您似乎做错了:您检查初始集中的第一个节点(例如树级别),如果它不是目标节点,则添加尚未存在的任何子节点frontier
并且尚未访问过。没关系,但可能没有必要。错误在于您在检查 parent 的任何 sibling 之前先检查 child ,因此您的搜索并不完全是广度优先。
那么你可以/应该做什么?
假设您的数据代表一棵树(广度优先表明这一点)并且您从某个级别(例如根节点)开始。由于数据是广度优先方法中的一棵树,任何子级都不能被访问过,并且也可能不在您的 frontier
列表中,因此无需检查(如果您有更多)一般图可能并非如此)。
因此算法可能如下所示:
LinkedList<Node> frontier = new LinkedList<>();
//assuming you always have a root node just add it
frontier.add( root );
Node goal = null;
//loop until the list is empty, if we found a goal node we'll break the loop from the inside
while( !frontier.isEmpty() ) {
//get the node at the start of the list and remove it
//in the first iteration this will be the root node
Node node = frontier.pop();
//found a goal so we're done
if( node.isGoal ) {
goal = node ;
break; //this breaks the while loop
}
//didn't find a goal yet so add the children at the end
if( node.children != null ) {
frontier.addAll( node.children );
}
}
这样做意味着您在末尾添加下一级(子级)的节点,然后将节点从前面弹出到树的更高位置,直到找到您要搜索的内容。这意味着列表中应该始终只有一两个级别的树,即当前级别以及任何已处理的当前级别节点的直接子级。
正如您所看到的,由于我们在树上进行操作,因此无需保留 explored
集。
此外,您可能需要考虑是否真的需要在迭代期间构建 prev
map ,因为您可能需要再次删除元素,并且 map 并不真正适合从您的目标获取路径节点到根。您可能希望在每个节点中保留指向父节点的链接,因此一旦找到目标节点,您只需迭代直到到达根节点。
关于java - BFS 中的目标检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44903149/
我有一个无向图,我从中重新绘制了相同的图,但采用树状结构(具有层次)。我知道广度优先搜索 (BFS) 算法的工作原理,但我不确定如何从图 --> 树进行转换。 在这里,在 this Wikipedia
只要我没记错,UCS 和 BFS 是一样的,唯一的区别是它不是扩展最浅的节点,而是扩展路径成本最低的节点。 (也为此使用 PriorityQueue 而不是 Queue)所以我复制了我的 BFS 代码
我知道 Dijkstra 的算法、Floyd-Warshall 算法和 Bellman-Ford 算法,用于查找图中 2 个顶点之间的最便宜路径。 但是当所有边的成本都相同时,最便宜的路径是边数最少的
我正在寻找一个代码来找到有向图中的最短路径 a 。 有什么地方可以找到吗? (可以基于BFS) 最佳答案 使用Erlang Digraph Library和函数 get_short_path/3,它采
这个问题已经有答案了: What is the point of the diamond operator (<>) in Java? (7 个回答) 已关闭 6 年前。 我是java初学者,我有BF
我想修改下面的代码以动态从文件中获取数据并运行 BFS。我尝试过循环,但我坚持如何使用匿名对象动态连接节点。 Node nA=new Node("101"); Node nB=new
我正在尝试实现 BFS 来查找学习某门类(class)之前所需的所有先决条件。我的public List computeAllPrereqs(String courseName)方法是我的代码困惑的地
我正在尝试编写一个程序来查找节点 B 是否属于从节点 A 开始的子树。我用 C 编写了代码,并自行实现了队列机制,因为我使用 BFS 来遍历树。问题是我的代码遇到无限循环,说我的队列已满,甚至没有。
我已经制作了 BFS 算法的并行版本,现在我正在尝试序列化相同的算法以了解加速情况。我的代码是这样的: #include #include #include struct Node {
我尝试根据我的研究在 JAVA 中实现 BFS 算法,但我有点困惑,我不确定我是在检查节点是否是目标,还是在将节点添加到探索列表中适当的地方。代码如下: frontier.add(nodes.getF
请帮助我理解我的代码做错了什么。我试图使用 BFS 获得最短路径来解决问题,但它要么给我 -1 要么 2。它应该给我 6 作为答案。我究竟做错了什么?这就是问题所在: 给定一个棋盘,找到马从给定来源到
我最近在解决一个 bfs 问题,其中每个节点都是数组元素的不同排列。但是我无法想出一个合适的数据结构来跟踪扩展树中的访问节点。通常,节点是不同的字符串,因此我们可以只使用映射将节点标记为已访问,但在上
我有一个文件夹结构中的元素列表: /文件夹/myfile.pdf /folder/subfolder1/myfile.pdf /文件夹/子文件夹2/myfile.pdf /folder/subfold
我已经实现了一个 BFS 算法来检测图中的循环,这是以下代码: void hasCycle(node *root,string start){ if(
真的很难弄清楚如何修复我的代码。我知道显然存在错误,因为它没有运行,但我不确定错误到底是什么,也不确定如何修复它们。任何帮助/见解将不胜感激。谢谢!! struct vertices { in
我在图中有代表城镇的顶点。我试图找到从 A 点到 B 点的最短路径。 我创建了一个图形类。 struct Edge{ string name; vector v; Edge(
所以我正在构建这棵树,它有 1..* 个节点,其中每个节点都有一个列表,该列表本身可以有 1..* 个节点。 树的前三层我可以构建得很好,但如果我不编写所有愚蠢的级别,它就不会发挥更多作用。解决方案当
我正在研究 Perfect Squares - LeetCode Perfect Squares Given a positive integer n, find the least number o
我是图论新手,目前只学过图论中的BFS和Disjoint sets。如果在给定的无向连通图中存在一个循环,我可以使用 BFS 找到它吗?我的意图是打印循环中的所有顶点。提前致谢。 最佳答案 是的,如果
我在矩阵上实现 bfs 时遇到问题。似乎我的代码只检查起始节点的子节点。 我的目标是找到从“B”到“H”的最短路径。我还认为我的代码需要大量修改。先感谢您! #include #include #
我是一名优秀的程序员,十分优秀!