Depth First Search to find shortest path?

最后都变了- 提交于 2021-01-28 06:08:44

问题


I know this is usually done with breadth first, but we are asked to do it with both, I already accomplished breadth first....

I feel like this is a pretty typical example of using a depth first search, so I was hoping I could get some help here... I am attempting to find the shortest path through a maze using depth first search, but as of right now I cannot wrap my head around how exactly to do it. This is my code so far:

void maze::findPathRecursive(graph &g, int position, int goal) {
    if (position == goal) {
        isPath = true; //returns true if there is a path to the goal
        correctPath.push_back(position); // the path to the goal
    } else {
        g.mark(position);
        g.visit(position);

        //gets all the neighbors of the current position
        vector<int> neighbors = getNeighbors(position, g);

        while (!neighbors.empty()) {
            int n = neighbors.back();
            neighbors.pop_back();

            if (!g.isVisited(n)) {
                findPathRecursive(g, n, goal);
            } 

            if (isPath) {
                correctPath.push_back(position);
                break;
            } 
        } 
    } 
} 

Right now, what this does is find the first path it can to the goal and breaks from there. I know I'll need to get rid of the break and I tried making another vector to store the shortest path and compare the lengths between it and the previous shortest but it did not work because I feel like I need to unvisit nodes at some point but cannot figure out how exactly to do it here.

Any help is greatly appreciated!!

g is a graph by the way which contains the maze.


回答1:


While you can find a path (and if you're lucky a shortest path) using DFS, it does not guarantee a shortest path in general. BFS is your best bet.

There is however an approach called Iterative Deepening. From Wikipedia:

Iterative deepening depth-first search1 (IDDFS) is a state space search strategy in which a depth-limited search is run repeatedly, increasing the depth limit with each iteration until it reaches d, the depth of the shallowest goal state. IDDFS is equivalent to breadth-first search, but uses much less memory; on each iteration, it visits the nodes in the search tree in the same order as depth-first search, but the cumulative order in which nodes are first visited is effectively breadth-first.

Pseudocode, again from wikipedia:

> IDDFS(root, goal) {
>   depth = 0
>   for(i=1, i<10, i++)   {
>       DFS(limit=i)
>   }
> }

First implement a DFS (should be exactly like BFS, but use a stack instead of a queue), and then implement the ID algorithm. Hope this helps.




回答2:


In general, you don't want to use DFS to find shortest path (unless your graph is definitively acyclic and also undirected, in which case, there is only one path to your goal to begin with.) It's possible, but it gets very contrived. On its simplest level, DFS is more adept at determining if there IS a path then determining what the best path is.

I'd recommend taking a look at different Breadth First Search (BFS) algorithms. If your maze is on a grid, A* is probably your best bet.




回答3:


I'm not sure I understand the question completely, but usually, wouldn't you use a breadth-first search (at least, in the simplest case)? It seems to me that any DFS that tries to find the shortest path has to turn into some sort of BFS anyway, as a DFS might "miss" shortcuts.

Maybe this https://cs.stackexchange.com/questions/4914/why-cant-dfs-be-used-to-find-shortest-paths-in-unweighted-graphs and this http://en.wikipedia.org/wiki/Shortest_path_problem helps.




回答4:


Rather than "unvisit" a node, I think you need to store with each node how many edges it took to get there. If you get there again via a shorter path, you update the edge count for that node and all the nodes that are on the best path from that node to the goal.



来源:https://stackoverflow.com/questions/22715791/depth-first-search-to-find-shortest-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!