Are you sure that your recursion gets so deep that you run out of stack space. How many paths are in your database? Since you seem to do depth first search and if your tree isn't degenerated into one long path then you would need gazillions of paths to exceed the stack space

But since you collect all found paths in memory (in @current), that array will get big pretty fast. So instead you should drop @current and print or save to file your found paths on success (inside your unless (defined $branches) ... and if (exists $regs... control structures. For this you need another parameter to your follow_paths subroutine that gives the momentary path to this point to the subroutine

So keep your recursive routine, it is much more readable, just save the results, if you can't keep them in memory.

But there is another possibility: Are you sure your data hasn't any loops, i.e. 0,1,0 points to 3,0,0 which points to 0,1,0 and both points are not in regs ? If there are loops you might put the points of your momentary path into a hash and call foul when you are at a point that already is in that hash.


In reply to Re: Eliminating Recursive Tree Walking by using MJD-style Infinite Streams? by jethro
in thread Eliminating Recursive Tree Walking by using MJD-style Infinite Streams? by jryan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.