简体   繁体   English

深度优先搜索随机选择节点/顶点

[英]Depth First Search Random Select Node/Vertex

I want to find a path to goal from start node using iterative depth first search using this maze represented in graph. 我想使用图形表示的迷宫,通过迭代深度优先搜索找到从起始节点到目标的路径。 It is a text file containing only pair of numbers like a pairwise connection aka edges/arcs. 这是一个仅包含数字对的文本文件,例如成对连接,也称为边/弧。 Like this: 像这样:

11 3
2 3
0 3
1 4
5 4
5 7
6 7
7 8
8 9
9 10
0 5

Then my code is like this: 然后我的代码是这样的:

private void performIterativeDFS(MazeGraph G, int node, int goal) {
        ArrayBasedStack arrayStack = new ArrayBasedStack();
        ArrayBasedStack pathStack = new ArrayBasedStack();
        arrayStack.push(node);
        visited[node] = true;
        while (!arrayStack.isEmpty()) {
            int newNode = arrayStack.pop();
            if (newNode == 0) {
                out.print("Starting at " + newNode + " ");
            }
            pathStack.push(newNode);
            if (newNode == goal) {
                out.println("Path if goal found: " + pathStack.toString());
            }
            for (int arc : G.getAdjacencyList(newNode)) {
                if (!visited[arc]) {
                    visited[arc] = true;
                    arrayStack.push(arc);
                }
            }
        }
    }

I have input 0 as a starting node and goal node is 1. Then the path that output is 0,5,7,8,9,10,6,4,1. 我有输入0作为起始节点,目标节点是1。然后输出的路径是0,5,7,8,9,10,6,4,1。 Unfortunately, that's not like a proper solution where you can go 0,5,4,1 instead. 不幸的是,那不是一个可以解决0,5,4,1的正确解决方案。 Does iterative depth first search randomly selects which nodes to go next before reaching the goal? 迭代深度优先搜索是否在达到目标之前随机选择了接下来要去的节点?

I tried modifying my code to do that but I can't make the path to print like 0,5,4,1. 我试图修改我的代码来做到这一点,但是我无法使打印路径像0,5,4,1一样。 I want to keep it simple as possible so it is for everyone to understand. 我想让它尽可能简单,以便每个人都可以理解。 Any suggestions or advice? 有什么建议或建议吗?

You won't get a different answer from your search without changing your algorithm(which would not make it a dfs) or (map which would be a waste of time if you were trying to make it for anything besides this specific data set). 如果不更改算法(不会使其成为dfs)或(如果尝试针对除此特定数据集以外的任何内容进行映射,这将浪费时间),您将不会从搜索中获得不同的答案。 you could try implementing a sort of backtrace after the code has found a path to reduces the number of nodes traversed, but that wouldn't be the simple answer you're looking for. 您可以在代码找到减少所遍历的节点数量的路径后尝试实施某种回溯,但这并不是您要找的简单答案。

Short answer: no, that's not really how DFS works. 简短的回答:不,这不是DFS的真正工作原理。

EDIT: missed a bit of your question, there's nothing in your code that makes it random. 编辑:错过了您的问题的一点,您的代码中没有什么使其随机。 if, rather than 如果,而不是

for (int arc : G.getAdjacencyList(newNode)) {
            if (!visited[arc]) {
                visited[arc] = true;
                arrayStack.push(arc);
            }

you randomly sampled G, then you would have a chance of getting the a different outcome, but as it is there is no random element to it. 您对G进行了随机抽样,那么您将有机会获得不同的结果,但是因为它没有随机元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM