filipo has asked for the wisdom of the Perl Monks concerning the following question:
Hi can you help please? I am new to perl and i'm trying to run the code below to calculate the shortest path using Floyd-Warshall algorithm. The code is directly copied from "Mastering algorithm with perl" from O'Reilliy media. The problem is when i run the code i don't get any results and i get the following message "get_attribute: not a compat02 graph ". Am i missing a module? Can anyone help please? thanks in advance. filipo.
#!/usr/bin/perl -w use strict; use Graph; use Graph::Directed; #how to use the Floyd-Warshall code my $g=Graph::Directed->new; $g->add_weighted_path(qw(a 1 b 4 c 1 d)); $g->add_weighted_path(qw(a 3 f 1 e 2 d)); $g->add_weighted_edges(qw(a c 2 a d 4 b e 2 f d 2 )); my $APSP = $g->APSP_Floyd_Warshall; print " "; foreach my $v ($APSP->vertices ) { printf "%-9s ", "$v" } print "\n"; foreach my $u ($APSP-> vertices ) { print "$u: "; foreach my $v ( $APSP->vertices ) { my $w = $APSP->get_attribute("weight", $u, $v); if (defined $w) { my $p = $APSP->get_attribute("path", $u, $v); printf "(%-5s)=%d ", "@$p", $w } else { printf "%-9s ", "-" } } print "\n" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: calculating shortest path using Floyd-Warshall with perl
by choroba (Cardinal) on Aug 15, 2013 at 11:13 UTC | |
by filipo (Novice) on Aug 15, 2013 at 13:54 UTC |