Traverse nodes starting from one and moving to outer connections

I need to filter out nodes from a graph based on the value of links coming out of that node and the value of those nodes. And, I need to iterate in fashion where I start from one node and then go through each node going out, but, and this is very important, I need to be able to not go through some nodes if they don’t match my condition.

If I have the graph like this:
A -> B -> D -> E
A -> C
I want to start from A, then check if B and C match my link and node conditions and if they do, move forward to the either B, C or both. If none of them match my condition, I want to stop there and never visit that node again, even if I get to it form a different link.

Basically, I want to filter a graph by traversing each node/link once and checking if they match my conditions, but the thing is that my condition is related to the other sibling nodes (coming out of the node I’m analyzing). So, if I’m at node A, I want to only go through the biggest value node (be that B or C), so I need to know both B and C, compare them and only navigate forward with the biggest one.

Is there an easy way to do this? Or should I just iterate through all the nodes, get all the nodes coming out of each of them, do my comparison, remember the nodes that don’t match my condition, then when I get to them (cause I iterate through all of them) just ignore them. The problem with this approach is that I iterate through all the nodes, allthough I don’t need to, cause maybe I choose C instead of B, then I skip the nodes D and E.

You can find three different kinds of graph traversal in this sample:

Basically you want to define a recursive function that takes the “current” node and a Set of already seen nodes (so that you don’t go into infinite loops if it encounters any circular paths). The first thing the function should do is check to see if the given node is already in the Set. If it is, return; if it is not, add the node to the Set and then decide what to do next.

Ok, hmm… thanks. I’ll check out that example.

But, my understanding is that I have to create such a recursive function myself, and check wether the function already passed through that node myself, correct?

Yes, that is correct. I outlined how to implement it in my previous reply. And there are lots of examples of it, although of course for slightly different purposes than what you want.