- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想生成 DAG(直接无环图)的 BFS 森林。这意味着我的 Tree 类需要是普通树而不是二叉树(换句话说,当我生成森林时,我无法提前知道节点将拥有的子节点数量)。大部分代码都写在下面并显示在下面,但是我缺少一行,但我一生都没有注意到!
public Tree BFS(V start)
{
reset();
LinkedList<GraphMatrixVertex<V>> list = new LinkedList<GraphMatrixVertex<V>>();
GraphMatrixVertex<V> vert = dict.get(start);
Tree root = new Tree(vert);
list.add(vert);
do
{
vert = list.getFirst();
Iterator<V> ni = neighbors(start);
while(ni.hasNext())
{
V v = ni.next();
GraphMatrixVertex<V> vtx = dict.get(v);
if(!vtx.isVisited())
{
list.add(vtx);
vtx.visit();
root.addChild(new Tree(vtx));
}
}
//code goes here
}
while(!list.isEmpty());
return root;
}
我的 Tree 类存储一个值参数、一个父引用和一个子列表。我的问题是引用下一个树节点。一旦我将所有未访问的邻居添加为当前节点的子节点,我如何到达下一个节点?
编辑:
所以它看起来像这样?
public void bfs(Tree parent)
{
Iterator<V> ni = neighbors((V) parent.value());
if(ni.hasNext())
{
while(ni.hasNext())
{
V next = ni.next();
GraphMatrixVertex<V> vert = dict.get(next);
if(!vert.isVisited())
parent.addChild(new Tree(next));
}
}
}
递归调用去哪里了?
最佳答案
如果我正确理解你的问题,你可以使用递归来解决这个问题。基本上,您有一个函数可以创建一层节点,然后为您想要创建/访问的每个子节点再次调用自身。
编辑:
好的,我稍微编辑了你的代码。首先,我删除了 if(hasNext) ,因为它与其中的 while 循环是多余的。对于邻居列表上的每个子节点,您创建一个新的树节点,然后运行其 bfs() 方法,传递当前的 Tree 对象。该函数返回一个列表,该列表应该是穿过树的最佳路径。我也不确定你获取相邻节点的方式,它看起来有点奇怪。我也没有测试过代码,所以其中可能存在拼写错误和其他内容,但希望它能让您了解如何进行搜索。哦,当您命中叶节点(您的目标?)时,它将需要设置其权重并返回一个仅包含其自身的新列表。
int weight; // this should be you node traversal cost
public LinkedList<Tree> bfs(Tree parent){
Iterator<V> ni = neighbors((V) parent.value());
LinkedList bestPath = null;
int bestScore = 0xFFFFFFFF;
while(ni.hasNext()){
V next = ni.next();
GraphMatrixVertex<V> vert = dict.get(next);
if(!vert.isVisited()){
Tree newNode = new Tree(next);
parent.addChild(newNode);
LinkedList path = newNode.bfs(this);
if(newNode.weight < bestScore){
bestScore = weight;
bestPath = path;
}
}
}
weight = bestScore + this.weight;
bestPath.addFirst(this);
return path;
}
编辑2:
public void bfs(Tree parent){
Iterator<V> ni = neighbors((V) parent.value());
while(ni.hasNext()){
V next = ni.next();
GraphMatrixVertex<V> vert = dict.get(next);
if(!vert.isVisited()){
Tree newNode = new Tree(next);
parent.addChild(newNode);
newNode.bfs(this);
}
}
}
关于java - 生成 BFS 图森林,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3509783/
我有一个无向图,我从中重新绘制了相同的图,但采用树状结构(具有层次)。我知道广度优先搜索 (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 #
我是一名优秀的程序员,十分优秀!