简体   繁体   中英

Creating a recursive path finding algorithm to find all paths between two nodes in a graph

I've implemented a DFS recursive algorithm to find ALL paths between two nodes on a directed graph. Currently, my algorithm is finding approximately half of the total paths to find (I checked on an online tool how many possible paths there should be). I'm not sure where my algorithm is going awry. I have checked a bunch of different posts and it seems to be what others have implemented but I think I may have overlooked something. This is my first pathfinding algorithm and I seem to be stumbling over my first hurdle. I am not allowed to use inbuilt ADTS and therefore have built my own. Any help is appreciated as I am really pulling my hair out over this one. Please let me know if you need further information/code.

PATH FINDING

private void findPaths(String current, String target, LinkedList<String> currPath)
{
    Edge edge;

    if(current == target)
    {
        System.out.println(curPath.toString());
        return;
    }
    for (Vertex vertex: current.getAdjacent()))
    {
        Edge edge = getEdge(current+vertex);
        if(!edge.getVisited())
        {
            edge.setVisited;
            curPath.insertLast(vertex);
            findPaths(vertex, target, currPath);
            curPath.removeLast()
        }
    }
    edge.clearVisited()
}

edited for readability.

I assume that the problem is in this code fragment:

for (Vertex vert1 : current.getVerts())      // say you have 4 unvisited edges
{
    label = vert1.toString();
    if(!getEdge(current+label).getVisited()) 
    {
        getEdge(current+label).setVisited(); // you will mark the 4 as visited 
        paths.addToPath(label);
        findPaths(vert1, target, paths);
        paths.removeFromPath();
    }
}
getEdge(current+label).clearVisited();      // but unmark only the last one 

You should replace it with

for (Vertex vert1 : current.getVerts())
{
    label = vert1.toString();
    if(!getEdge(current+label).getVisited())
    {
        getEdge(current+label).setVisited();   // mark this edge
        paths.addToPath(label);
        findPaths(vert1, target, paths);
        paths.removeFromPath();
        getEdge(current+label).clearVisited(); // unmark this edge
    }
}

Or even cleaner, you can refactor your code as follows:

private void findPaths(Vertex current, String target, Paths<String> paths)
    if(current.label.equals(target))
    {
        System.out.println(paths.getCurrPath());
        return;
    }
    for (Vertex vert1 : current.getVerts())
    {
        String label = vert1.toString();          // declare&initialize in loop
        Edge e = getEdge(current+label);          // lookup only once
        if( ! e.getVisited())
        {
            e.setVisited();
            paths.addToPath(label);
            findPaths(vert1, target, paths);
            paths.removeFromPath();
            e.clearVisited();
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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