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*
In reply to Re: Dijkstra-Algorithm
by Discipulus
in thread Dijkstra-Algorithm
by grexman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |