Java 为什么';以下代码是否返回A、AEC、AEBCD?
我有一个深度有限的搜索算法,我在一个循环中运行了几次,这样算法就可以像一个迭代深化搜索算法一样工作, 为什么代码只在显式调用方法时返回所需结果,而在循环中调用方法时不返回所需结果Java 为什么';以下代码是否返回A、AEC、AEBCD?,java,search,depth-first-search,Java,Search,Depth First Search,我有一个深度有限的搜索算法,我在一个循环中运行了几次,这样算法就可以像一个迭代深化搜索算法一样工作, 为什么代码只在显式调用方法时返回所需结果,而在循环中调用方法时不返回所需结果 public class DepthLimited { public static void main(String[] args) { Graph graph = new Graph(); graph.addNode('A'); // Since A is the first vertex s
public class DepthLimited {
public static void main(String[] args) {
Graph graph = new Graph();
graph.addNode('A'); // Since A is the first vertex so it takes the index 0
graph.addNode('E'); // B is the second one so it takes index 1
graph.addNode('B'); // index 2
graph.addNode('C'); // index 3
graph.addNode('D'); // index 4
graph.addEdge(0, 1); // This indicates the relation between the vertices A and E
graph.addEdge(1, 2); // This indicates the relation between the vertices E and B
graph.addEdge(0, 3); // This indicates the relation between the vertices A and C
graph.addEdge(3, 4); // This indicates the relation between the vertices C and D
// System.out.print("Sequence: ");
// graph.dls(2);
// System.out.println("");
graph.dls(2); // produces AEBCD when called like this, before any other similar calls i.e graph.dls(1)
for (int i = 0; i <= 2; i++) {
graph.dls(i); // when i is 2 the method only returns A instead of AEBCD
System.out.println("");
}
}
}
公开课取消限制{
公共静态void main(字符串[]args){
图形=新图形();
graph.addNode('A');//因为A是第一个顶点,所以它采用索引0
graph.addNode('E');//B是第二个,因此它采用索引1
graph.addNode('B');//索引2
graph.addNode('C');//索引3
graph.addNode('D');//索引4
graph.addEdge(0,1);//这表示顶点A和E之间的关系
graph.addEdge(1,2);//这表示顶点E和B之间的关系
graph.addEdge(0,3);//这表示顶点A和C之间的关系
graph.addEdge(3,4);//这表示顶点C和D之间的关系
//系统输出打印(“序列:”);
//图1.dls(2);
//System.out.println(“”);
graph.dls(2);//在像这样调用时,在任何其他类似调用(如graph.dls(1))之前生成AEBCD
对于(inti=0;i而言,原因是双重的
首先,调用dls(int limit)
后,每个访问的节点的checked
值设置为true
,并在随后调用该方法时保持不变。首先需要将它们设置为将checked
设置回false
其次,depth
是一个类成员变量/字段,不会每次重置为0
。该变量应该是方法的局部变量
我建议您不要将它们是否被访问存储为节点
的属性,而是引入一个本地集
(例如哈希集
),用于存储访问的节点;或者在您的情况下,是表示访问的节点
对象索引的整数值
例如,快速破解您的解决方案:
public void dls(int limit) {
Set<Integer> visited = new HashSet<>();
int start = 0;
stack.push(start);
display(start);
int depth = 1;
while (!stack.isEmpty()) {
int current = stack.peek();
visited.add(current);
int next = -1;
for (int adj = 0; adj < noOfNodes; adj++) {
if (neighbourMatrix[current][adj] == 1 && !visited.contains(adj)) {
next = adj;
}
}
if (depth <= limit) {
if (next == -1) {
stack.pop();
depth--;
} else {
stack.push(next);
display(next);
depth++;
}
} else {
stack.pop();
depth--;
}
}
}
public void dls(整数限制){
Set visted=新HashSet();
int start=0;
堆栈推送(启动);
显示(启动);
int深度=1;
而(!stack.isEmpty()){
int current=stack.peek();
已访问。添加(当前);
int next=-1;
for(int adj=0;adjdls()
应该是迭代的,并且它会在搜索顶点时打印顶点,那么如果您继续已经完成的搜索,它为什么会/应该打印任何东西呢?似乎您需要重新考虑您的逻辑和/或预期结果。
public void dls(int limit) {
Set<Integer> visited = new HashSet<>();
int start = 0;
stack.push(start);
display(start);
int depth = 1;
while (!stack.isEmpty()) {
int current = stack.peek();
visited.add(current);
int next = -1;
for (int adj = 0; adj < noOfNodes; adj++) {
if (neighbourMatrix[current][adj] == 1 && !visited.contains(adj)) {
next = adj;
}
}
if (depth <= limit) {
if (next == -1) {
stack.pop();
depth--;
} else {
stack.push(next);
display(next);
depth++;
}
} else {
stack.pop();
depth--;
}
}
}