Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

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

The usual thing to do when you have a recursive routine that needs an IUO parameter, is to use a 'helper' wrapper that supplies it.

Here's a cleaned up and simplified version that does that:

#! perl -slw #! perl -slw use strict; use List::Util qw[ shuffle ]; use Data::Dump qw[ pp ]; my %graph = map { ( chr( 64 + $_ ) => [ (shuffle 'A'...'Z')[ 0 .. 1+ int( rand 10 )] ] + ) } 1, (shuffle 2 .. 25)[ 0.. int( rand 20 )], 26; pp \%graph; sub _findPaths { my( $graph, $start, $end, $seen ) = @_; return [$end] if $start eq $end; $seen->{ $start } = 1; map { map [ $start, @$_ ], _findPaths( $graph, $_, $end, {%$seen} ); } grep !$seen->{ $_ }, @{ $graph->{ $start } }; } sub findPaths { _findPaths( @_, {} ); } my( $start, $end ) = @ARGV; print "@$_" for findPaths( \%graph, $start, $end ); __END__ c:\test>868031 A Z { A => ["M", "L", "V", "Y", "C", "T", "X", "B", "U", "G", "J"], B => ["U", "W", "E", "Q", "M", "G", "N"], H => ["K", "L", "I", "S", "B", "H", "M", "P", "Q", "N", "T"], L => ["Q", "X", "E", "D", "L", "S", "N", "K"], P => ["D", "O", "Y", "R", "I", "W", "Q", "V", "N"], Q => ["P", "A", "N", "X", "R", "M", "T", "H", "O", "V"], R => ["O", "A", "S", "V", "M"], T => ["J", "Z", "F", "T", "Q", "X", "S"], V => ["L", "E", "Z", "V"], Y => ["M", "X", "Y", "I", "K", "U", "G", "S"], Z => ["M", "F", "A", "P", "I"], } A L Q P R V Z A L Q P V Z A L Q R V Z A L Q T Z A L Q H P R V Z A L Q H P V Z A L Q H T Z A L Q V Z A V L Q T Z A V L Q H T Z A V Z A T Z A T Q P R V Z A T Q P V Z A T Q R V Z A T Q H P R V Z A T Q H P V Z A T Q V Z A B Q P R V Z A B Q P V Z A B Q R V Z A B Q T Z A B Q H P R V Z A B Q H P V Z A B Q H T Z A B Q V Z

Be warned. The random tree generator can generate some pretty big results sets.


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^3: 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

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, 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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (5)
    As of 2020-04-05 10:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The most amusing oxymoron is:
















      Results (33 votes). Check out past polls.

      Notices?