深度优先迭代加深算法,不返回任何结果(在Java中) [英] Depth First Iterative deepening algorithm returning no results (in java)
问题描述
我有一个搜索算法,该算法应该解析整个树,找到可以与搜索查询匹配的所有结果,并将它们全部作为列表返回.我意识到这还不是算法的重点,但是我正在做此测试,首先进行广度优先和深度优先搜索,以了解对它们进行计时的最快速度.其他两个搜索按预期工作,但是当我输入与DFID搜索目标相同的搜索信息时,我得到一个空列表.所以我知道我的数据是正确的,只是算法中的某些问题是错误的,我无法弄清楚是什么.我是根据Wikipedia上的伪代码编写的.这是我所拥有的:
I have a search algorithm that is supposed to parse the entire tree, find all results that could match a search query, and return them all as a list. I realize this isn't quite the point of the algorithm, but I'm doing this as a test with breadth first and depth first searches to see what is fastest by timing them. The other two searches work as intended, but when I enter the same search information as my goal for the DFID search i get an empty list. So I know my data is right, just something in the algorithm is wrong and I can't figure out what. I wrote this based off the pseudocode on Wikipedia. Here's what I have:
boolean maxDepth = false;
List<String> results = new ArrayList<String>();
public List<String> dfid(Tree t, String goal)
{
int depth = 0;
while (!maxDepth)
{
System.out.println(results);
maxDepth = true;
depth += 1;
dls(t.root, goal, depth);
}
return results;
}
public void dls(Node node, String goal, int depth)
{
System.out.println(depth);
if (depth == 0 && node.data.contains(goal))
{
//set maxDepth to false if the node has children
if (!node.children.isEmpty())
{
maxDepth = false;
}
results.add(node.data);
}
else if (depth > 0)
{
for(Node child : node.children)
{
dls(child, goal, depth-1);
}
}
}
推荐答案
交换zim-zam建议的行,并添加另一个else(如果depth> 0,则添加到else后面)以将maxDepth翻转为false
swap the lines zim-zam suggested and add another else (after the else if depth > 0 ) to flip maxDepth to false
这篇关于深度优先迭代加深算法,不返回任何结果(在Java中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!