Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
图JAVA stackoverflow中的递归深度优先搜索_Java_Search_Graph_Tree - Fatal编程技术网

图JAVA stackoverflow中的递归深度优先搜索

图JAVA stackoverflow中的递归深度优先搜索,java,search,graph,tree,Java,Search,Graph,Tree,我用java制作了一个图形,使用hashmap,因为它更高效。 我做了一个递归dfs算法,找到节点1和节点2之间的路径,它可以工作,但是当我有一个有很多节点(比如100.000)的图时,我得到了stackoverflow错误。 因此,我尝试制作一个迭代版本的dfs,但效果不是很好,因为它保存了所有dfs的路径,而不仅仅是节点1和节点2之间的路径,就像递归方法一样。 有人能帮我解决迭代法吗?或者找到一个更好的解决方案来获得两个节点之间的路径,因为我必须使一个更快的方法成为可能。 dfs递归 pub

我用java制作了一个图形,使用hashmap,因为它更高效。 我做了一个递归dfs算法,找到节点1和节点2之间的路径,它可以工作,但是当我有一个有很多节点(比如100.000)的图时,我得到了stackoverflow错误。 因此,我尝试制作一个迭代版本的dfs,但效果不是很好,因为它保存了所有dfs的路径,而不仅仅是节点1和节点2之间的路径,就像递归方法一样。 有人能帮我解决迭代法吗?或者找到一个更好的解决方案来获得两个节点之间的路径,因为我必须使一个更快的方法成为可能。 dfs递归

public boolean rootToNodePath(Node node, ArrayList<Integer> path, Node nodeToFind, List<Integer> visited) {
        if (node.getVal() == nodeToFind.getVal()) {
            return true;
        }

        boolean res = false;
        boolean addedInPath = false;

        if (!visited.contains(node.getVal()))
            visited.add(node.getVal());

        for (Edge e : node.getChildren()) {
            if (!visited.contains(e.getDestination())) {
                res = res || rootToNodePath(nodeMap.get(e.getDestination()), path, nodeToFind, visited);
                if (res && !addedInPath) {
                    path.add(e.getDestination());
                    addedInPath = true;
                }
            }
        }
        return res;
    }
public boolean DFS(Node nodeStart, Node nodeToFind, ArrayList<Integer> path) {
        if (nodeStart.getVal() == nodeToFind.getVal()) {
            return true;
        }
        Stack<Node> DFS_stack = new Stack<>();
        DFS_stack.add(nodeStart);
        List<Node> visited = new ArrayList<>();
        visited.add(nodeStart);
        while (!DFS_stack.isEmpty()) {
            Node nodeRemove = DFS_stack.pop();
            List<Integer> adjs = nodeRemove.getAdjacenets();
            List<Node> adj = new ArrayList<>();
            for (Integer i : adjs) {
                adj.add(nodeMap.get(i));
            }
            for (int i = 0; i < adj.size(); i++) {
                Node currentNode = adj.get(i);
                if (currentNode != null && !visited.contains(currentNode) && !path.contains(currentNode.getVal())) {
                    path.add(currentNode.getVal());
                    DFS_stack.add(currentNode);
                    visited.add(currentNode);
                }
            }
        }
        return true;
    }
public class Node {
    private int val;
    private ArrayList<Edge> children;

    // constructor,initializing the node value and
    public Node(int val) {
        this.val = val;
        this.children = new ArrayList<>();
    }
    //getter&setter
public class Edge {
    private int destination, weight;

    public Edge(int to, int w) {
        this.destination = to;
        this.weight = w;
    }
    //getter & setter