in reply to calculating shortest path using Floyd-Warshall with perl
#!/usr/bin/perl use warnings; use strict; use Graph; use Graph::Directed; 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->path_length($u, $v); if ($w) { my @p = $APSP->path_vertices($u, $v); printf "(%-5s)=%d ", "@p", $w; } else { printf "%-9s ", "-" } } print "\n" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: calculating shortest path using Floyd-Warshall with perl
by filipo (Novice) on Aug 15, 2013 at 13:54 UTC |