3

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
my account_ram
  • 2,158
  • 11
  • 25
  • 1
    A particular example of what you want to do might be helpful to potential answerers. – J. M.'s missing motivation 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 03 '12 at 14:49

1 Answers1

1

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}
jVincent
  • 14,766
  • 1
  • 42
  • 74
  • Thanks Vincent. Like I mentioned, I am relying on events associated with the DFS. – my account_ram Oct 04 '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 04 '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