I need an algorithm to explore every node in a graph (starting at any given node). Each node can have multiple parent nodes and multiple child nodes.
The following algorithm (in psuedocode) appears to gather every node but results in duplicate nodes (I think because the recursive 'paths' crossover at some points):
rFindAll(Node ID, Array currentpath) //current path passed by value
{
Array connectedNodes = getConnectedNodes(ID);
for_each_node (Node n : connectedNodes)
{
if(find(currentpath, n) == NOT_FOUND) // check to ensure id not in current recursive path
{
// every n is a node which is within the same graph as the original node
currentpath.add(n);
rFindAll(n, currentpath);
}
}
}
I was just wondering whether anyone has a better suggestion to do this. I've looked on google/wikipedia and there was nothing which would obviously help.
Thanks