Java 如何将此递归深度优先搜索转换为深度受限搜索?
我一直在尝试将深度优先搜索改为深度受限搜索,但到目前为止我还没有找到一种方法。有什么想法吗Java 如何将此递归深度优先搜索转换为深度受限搜索?,java,graph,depth-first-search,Java,Graph,Depth First Search,我一直在尝试将深度优先搜索改为深度受限搜索,但到目前为止我还没有找到一种方法。有什么想法吗 // A function used by DFS void DFSUtil(int v,boolean visited[]) { // Mark the current node as visited and print it visited[v] = true; System.out.print(v+" "); // Recur for a
// A function used by DFS
void DFSUtil(int v,boolean visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v+" ");
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
// The function to do DFS traversal. It uses recursive DFSUtil()
void DFS(int v)
{
// Mark all the vertices as not visited(set as
// false by default in java)
boolean visited[] = new boolean[V];
// Call the recursive helper function to print DFS traversal
DFSUtil(v, visited);
}
//DFS使用的函数
void DFSUtil(int v,布尔值[])
{
//将当前节点标记为已访问并打印
访问[v]=正确;
系统输出打印(v+“”);
//对与该顶点相邻的所有顶点重复
迭代器i=adj[v].listIterator();
while(i.hasNext())
{
int n=i.next();
如果(!已访问[n])
DFSUtil(n,已访问);
}
}
//执行DFS遍历的函数。它使用递归DFSUtil()
无效DFS(整数v)
{
//将所有顶点标记为未访问(设置为
//在java中默认为false)
布尔值[]=新布尔值[V];
//调用递归助手函数以打印DFS遍历
DFSUtil(v,访问);
}
如果我没有弄错,您希望搜索到限制,那么您可以使用计数器,并使用限制值与DFSUtil
函数中的限制进行比较 请定义“深度限制搜索”。请张贴包含测试数据的最终预期结果“搜索到极限”什么极限?