Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've recently been playing with Graph and it seems this may be a shortest path (specifically, All-Pairs Shortest Paths (APSP)) problem? The module uses the Floyd Warshall algorithm to calculate. The following creates the graph object and then calculates shortest path and prints it. Note, it only shows 1 path - have a look at the Graph perldoc on how to get all paths. (Note: I also used Graph::Convert and Graph::Easy to get the visual; it's not required to make this work.)

UPDATE: Updated code to be more efficient

#!perl use strict; use warnings; my $MAX = 5; # how big my $TO = '3-1'; # from 0-0, where to? use Graph; my $g = Graph->new(); my @from; for my $y ( 0 .. $MAX ) { my @to; for my $x ( 0 .. $y ) { push @to, "$y-$x"; } for my $f ( 0 .. $#from ) { for my $t ( $f .. $f + 1 ) { $g->add_edge( $from[$f], $to[$t] ); } } @from = @to; } # Comment next 3 if no install Graph::Convert and Graph::Easy use Graph::Convert; my $ge = Graph::Convert->as_graph_easy($g); print $ge->as_ascii . "\n"; my $apsp = $g->APSP_Floyd_Warshall(); my @v = $apsp->path_vertices('0-0', $TO); print join ( " ", @v ) . "\n";

OUTPUT:

+-----+     +-----+     +-----+     +-----+     +-----+     +-----+
| 0-0 | --> | 1-0 | --> | 2-0 | --> | 3-0 | --> | 4-0 | --> | 5-0 |
+-----+     +-----+     +-----+     +-----+     +-----+     +-----+
  |           |           |           |           |
  |           |           |           |           |
  v           v           v           v           v
+-----+     +-----+     +-----+     +-----+     +-----+
| 1-1 | --> | 2-1 | --> | 3-1 | --> | 4-1 | --> | 5-1 |
+-----+     +-----+     +-----+     +-----+     +-----+
  |           |           |           |
  |           |           |           |
  v           v           v           v
+-----+     +-----+     +-----+     +-----+
| 2-2 | --> | 3-2 | --> | 4-2 | --> | 5-2 |
+-----+     +-----+     +-----+     +-----+
  |           |           |
  |           |           |
  v           v           v
+-----+     +-----+     +-----+
| 3-3 | --> | 4-3 | --> | 5-3 |
+-----+     +-----+     +-----+
  |           |
  |           |
  v           v
+-----+     +-----+
| 4-4 | --> | 5-4 |
+-----+     +-----+
  |
  |
  v
+-----+
| 5-5 |
+-----+

0-0 1-0 2-0 3-1

In reply to Re: compute paths in Pascal's triangle (aka Tartaglia's one) by VinsWorldcom
in thread compute paths in Pascal's triangle (aka Tartaglia's one) by Discipulus

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-20 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found