Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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).

share|improve this question
1  
A particular example of what you want to do might be helpful to potential answerers. –  J. M. Sep 28 '12 at 16:29
    
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? –  my account_ram Sep 29 '12 at 16:13
    
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. –  Daniel Lichtblau Oct 3 '12 at 14:49
add comment

1 Answer

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}
share|improve this answer
    
Thanks Vincent. Like I mentioned, I am relying on events associated with the DFS. –  my account_ram Oct 4 '12 at 13:50
    
@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. –  jVincent Oct 4 '12 at 19:50
    
Thank you all - I am implementing a modified version of a DFS myself! –  my account_ram Oct 17 '12 at 17:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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