Ok, background to the problem:
I’m writing a little application that looks at the graph and looks at all the paths from any two given points.
Points are A B C D E and the connections on the graph are as follows…
A->B, A->D, A->E
B->C
C->D, C->E
D->C, D->E
E->B
I need to code it so that it looks at all possible paths which are less than a certain length (say 30). The quirky bit of the spec is that it can visit the destination as part of the path, e.g:
Start at C going to C can follow:
CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC
Now my code is as follows…
private void findAllPaths(LinkedList path, Junction node, Junction end)
{
path.add(node);
if(node == end && path.size() > 1)
{
System.out.println(path);
}
else
{
for(Road r : node.getAdjacencies())
{
if(path.size() < 30) findAllPaths(path, r.getTarget(), end);
}
}
}
And this gives me the paths: [C, D, C] [C, D, C, E, B, C] [C, D, C, E, B, C, E, B, C]
My problem is that the recursion doesnt seem to happen in the way that i expect. It only ever follows the first adjaceny for each node and never recurses back to try the others.
If anyone can see where im going ridiculously wrong or see my problem please post! All help would be greatly appreciated…
Cheers,
Djoodle
You’re not removing the node again after you have tried it: