3
$\begingroup$

Is there a way to conditionally take control/modify the flow during a depth-first scan or the breadth-first scan ?

Say, for example, if a particular node if found, consider it as a leaf-node and continue with the rest of the processing as usual (meaning do not traverse its child-nodes).

$\endgroup$
3
  • 1
    $\begingroup$ A particular example of what you want to do might be helpful to potential answerers. $\endgroup$ Sep 28, 2012 at 16:29
  • $\begingroup$ I am trying to find a path in 2D regular grid (GridGraph[{m, n}]), which satisfies some conditions. I need to explore pathways starting from a node. I think DFS would be one (quick+suitable for my purpose) way to explore all possible pathways. But as soon as I see that the current path is NOT a solution, I need the algorithm to stop there and not process the child nodes further. (I am thinking of using DFS since I find the events very handy) Can this be done? $\endgroup$ Sep 29, 2012 at 16:13
  • $\begingroup$ There are ways indicated here for doing a depth first traversal. You could modify one to terminate any given path on encountering a given condition. $\endgroup$ Oct 3, 2012 at 14:49

1 Answer 1

1
$\begingroup$

The answer by Mr. Wizard that Daniel linked to is easily hacked to include a stop condition, you simply take the recursive function, but filter the next level depending on your stopping criterion.

stopCondition[element_] := Length[element] > 1;
(Print@#; #0~Scan~Select[#, stopCondition]) &@{{1, {2, 3}, 0}, {4, 5, 2}, 2}
$\endgroup$
3
  • $\begingroup$ Thanks Vincent. Like I mentioned, I am relying on events associated with the DFS. $\endgroup$ Oct 4, 2012 at 13:50
  • $\begingroup$ @myaccount_ram I'm assuming you mean to say that you are using the build-in DepthFirstScan function for scanning graphs? If so you should really update your question to reflect this, and include an example highlight what events you are reliant on. $\endgroup$
    – jVincent
    Oct 4, 2012 at 19:50
  • $\begingroup$ Thank you all - I am implementing a modified version of a DFS myself! $\endgroup$ Oct 17, 2012 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.