StackOverflowerr-java

StackOverflowerr-java,java,graph,depth-first-search,stack-overflow,Java,Graph,Depth First Search,Stack Overflow,我正在写一个代码来在一个图上运行DFS,但是这些图是巨大的。我在DFS访问时遇到堆栈溢出错误,但是其他人可以毫无问题地运行DFS访问。有什么原因会导致StackOverFlow错误吗?我尝试分配更多内存(我有16gb的RAM),但它限制了我1gb的内存 public void DFSVisit(Graph <String, String> graph, String current, int [] data, StackObject [] finish, boolean compli

我正在写一个代码来在一个图上运行DFS,但是这些图是巨大的。我在DFS访问时遇到堆栈溢出错误,但是其他人可以毫无问题地运行DFS访问。有什么原因会导致StackOverFlow错误吗?我尝试分配更多内存(我有16gb的RAM),但它限制了我1gb的内存

public void DFSVisit(Graph <String, String> graph, String current, int [] data, StackObject [] finish, boolean complicated){
    //data stores the time
    data[9]++;
    graph.setAnnotation(current, "time", data[9]);
    graph.setAnnotation(current, "color", "gray");
    Iterator <ArrayList<String>> itr = graph.outAdjacentVertices(current);
    ArrayList <String> currentlist = null;
    String adjacent;
    while(itr.hasNext()){
        currentlist = itr.next();
        adjacent = currentlist.get(1);
            if(graph.getAnnotation(adjacent, "color").equals("white")){
                graph.setAnnotation(adjacent, "parent", current);
                DFSVisit(graph, adjacent, data, finish, complicated);
            }
    }
    graph.setAnnotation(current, "color", "black");
    data[9]++;
    graph.setAnnotation(current, "done", data[9]);
    finish[0].done.push(current);
    //System.out.println(current);
}
public void DFSVisit(图形、字符串当前、int[]数据、StackObject[]完成、布尔复杂){
//数据存储时间
数据[9]++;
graph.setAnnotation(当前“时间”,数据[9]);
图形。设置注释(当前“颜色”、“灰色”);
迭代器itr=图形。外邻接顶点(当前);
ArrayList currentlist=null;
相邻的字符串;
while(itr.hasNext()){
currentlist=itr.next();
相邻=currentlist.get(1);
if(graph.getAnnotation(相邻,“颜色”)等于(“白色”){
graph.setAnnotation(相邻的“父项”,当前);
DFS访问(图形、相邻、数据、完成、复杂);
}
}
图形。设置注释(当前“颜色”、“黑色”);
数据[9]++;
graph.setAnnotation(当前,“完成”,数据[9]);
完成[0]。完成。推送(当前);
//系统输出打印项次(当前);
}

分配内存没有帮助。
您在同一方法内调用DfsVisite,并且没有为该方法定义返回条件,因此它将抛出stackoverflow错误

请注意,DFS的空间复杂度是O(b.m)其中m是搜索空间的深度,b是最大分支因子,因此如果m是无限的,您可能永远无法用不适当的实现解决问题


要找到一个更好的例子,请看下面的问题。

您必须在某一点上停止递归调用。。在某些条件下。。否则将是无休止的调用,并将抛出堆栈溢出错误。

我切换到笔记本电脑上工作,它立即工作。显然,问题是我使用32位Eclipse而不是64位Eclipse。我甚至没有意识到我在桌面上使用的是32位。

如果搜索空间是无限的,则运行代码的每个系统都会出现此异常。您必须将当前节点标记为已访问,这样您就不会从相邻的顶点列表返回到它。想象一个简单的图,有两个顶点和一条连接它们的边。相邻列表中的两个顶点都将包含彼此,并且您的算法将反复访问它们。