简体   繁体   中英

I'm having a lot of trouble using recursion with graphs. Java

public int rec(String letterOne, String letterTwo, int steps)
    { 
        String currentLetter = "";
        if(letterOne.equals(letterTwo))
        {
            return steps;
        }
        for(int x=0; x<graph.length; x++)
        {
            for (int y=0; y<graph[x].size(); y++)
            {
                currentLetter = graph[x].get(y).toString();
                if (currentLetter.equals(letterOne)&&!checkForLet(checked,currentLetter))
                {                                  
                    checked.add(currentLetter);
                    if (y==0)
                    {
                        
                        return rec(graph[x].get(1).toString(),letterTwo,steps+1);
                    }
                    else
                    {
                        return rec(graph[x].get(0).toString(),letterTwo,steps+1);
                    }
                }
            }
        }
        return -1;
    }

And here is graph

CA XY RS YS ST TB AB BD RJ CE

I already have all the connections into an ArrayList[]. This is my recursion method, it is supposed to, for now, return the first path it finds in an inputted graph. I think I'm so close to figuring it out, and ps sorry if I'm bad at this; this is my first time doing this. Thanks.

The problem with your code is that when you are iterating through edged of the graph, you only check the first edge containing letterOne, and you return the result, so for example for a path from C to B, rec will be invoked in this order:

rec("C", "B", 0)
rec("A", "B", 1)
rec("C", "B", 2)//returns -1 and whole function returns -1

Here's working code based on similar idea to the one you had:

    private static List<String>[] graph = new List[]{
        List.of("C", "A"),
        List.of("X", "Y"),
        List.of("R", "S"),
        List.of("Y", "S"),
        List.of("S", "T"),
        List.of("T", "B"),
        List.of("A", "B"),
        List.of("B", "D"),
        List.of("R", "J"),
        List.of("C", "E")
};
int pathLength(String from, String to){
    return pathLengthRec(from, to, 0);
}

Set<String> checkedLetters=new HashSet<>();
private int pathLengthRec(String from, String to, int steps) {
    if(from.equals(to)){
        return steps;
    }
    if (checkedLetters.add(from)) {//returns true if letter was not yet visited
        for (List<String> edge : graph) {
            if (edge.contains(from)) {
                String nextLetterCandidate = edge.get(0).equals(from) ? edge.get(1) : edge.get(0);
                int pathLength = pathLengthRec(nextLetterCandidate,to,steps + 1);
                if (pathLength != -1) { //here is the important difference - if the function returns -1 for one of the edges try with other edges
                    return pathLength;
                }
            }
        }

    }
    return -1;
}

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