Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

If you can do what you need to do with the paths by getting them one at a time, rather than all at once, then this version of findPaths() runs much faster and uses far less memory. (It runs faster because it uses much less memory.)

For example, on a randomly generated graph that has 10,000 paths, it takes 1.09 seconds instead of 38 seconds for my previous version. In the process, this uses less than 7MB where the previous version used 240MB.

And on a graph that has just over 5 million paths, this still uses just 7MB and completes in 14 1/2 minutes.

I project that the previous version would take 5 1/2 hours to complete, except that it would require 12GB to do so, and I only have 4GB.

It's still the same order of complexity--its essentially the same algorithm--but the simple expedient of avoiding allocating and reallocating zillions of small chunks of ram, mean in the real world it is much faster. Which confirms once again my feelings about big O.

Anyway, if your algorithm can be adapted to operate this way, you might find it useful.

sub _findPaths2 { my( $code, $graph, $start, $end, $path, $seen ) = @_; return $code->( @$path, $end ) if $start eq $end; $seen->{ $start } = 1; for ( grep !$seen->{ $_ }, @{ $graph->{ $start } } ) { _findPaths2( $code, $graph, $_, $end, [ @$path, $start ], { %$ +seen } ), } } sub findPaths2(&@) { _findPaths2( @_, [], {} ); } findPaths2{ print join ' ', @_; } \%graph, $start, $end;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^5: Finding All Paths From a Graph From a Given Source and End Node by BrowserUk
in thread Finding All Paths From a Graph From a Given Source and End Node by neversaint

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (2)
    As of 2020-12-04 02:33 GMT
    Find Nodes?
      Voting Booth?
      How often do you use taint mode?

      Results (58 votes). Check out past polls.