#!/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" }