# Control over depth-first scan and breadth-first scan

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

-
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

stopCondition[element_] := Length[element] > 1;