ok this node is a bit aged.. but I ended here looking for Dijkstra and Perl, so as future reference I put my discoveries

in short: use Graph::Weighted SP_Dijkstra method. There is also Graph::Dijkstra but its matrix of failing test removed it from the competition.

Paths::Graph must have some serious problem: it eat up a lot of memory and stop working even with small graphs. Here the code I used:

use strict; use warnings; use Paths::Graph; use Graph::Weighted; use Data::Dump; use Benchmark 'cmpthese'; my $max = $ARGV[0] || 4; my $dest = $max.'_'.$max; my @aoa = map{ [ map{ int(rand(4))+1 }0..$max ] } 0..$max; my %graph = build_graph(); dd %graph; foreach my $row (0..$#aoa){ foreach my $col( 0..$#{$aoa[$row]} ){ print $aoa[$row][$col]; } print "\n"; } cmpthese( -2, { 'Paths::Graph' => sub { my $obj = Paths::Graph->new(-origin=>"0_0",-destiny=>$dest,-gr +aph=>\%graph); my @paths = $obj->shortest_path(); }, 'Graph::Weighted' => sub { my $g = Graph::Weighted->new(); $g->populate(\%graph); my @pathbis = $g->SP_Dijkstra( "0_0", $dest ); dd @pathbis; }, }); sub build_graph{ my %graph; foreach my $row (0..$#aoa){ foreach my $col( 0..$#{$aoa[$row]} ){ #print $row."_".$col." is current..\n"; map{ $graph{$row."_".$col}{$_->[0].'_'.$_->[1]} = $aoa[$_-> +[0]][$_->[1]] if $_->[0] >= 0 and $_->[0] <= $#{$aoa[$row]} and $_->[1] >= 0 and $_->[1] <= $#aoa } ([$row-1,$col],[$row+1,$col],[$row,$col-1],[$row,$col+1] +); } } return %graph; }

Which with the deafault of a 5x5 grid gives:

Rate Paths::Graph Graph::Weighted Paths::Graph 2.46/s -- -97% Graph::Weighted 92.2/s 3646% --

while with a bigger grids, commenting out Paths::Graph subs who stuck the program;

perl pathsingraph.pl 5 Rate Graph::Weighted Graph::Weighted 60.8/s -- perl pathsingraph.pl 15 Rate Graph::Weighted Graph::Weighted 8.60/s -- perl pathsingraph.pl 29 Rate Graph::Weighted Graph::Weighted 2.29/s --

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Dijkstra-Algorithm by Discipulus
in thread Dijkstra-Algorithm by grexman

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.