use strict; use warnings; use Graph; my $net = Graph::Undirected->new; # links go both ways my $start = '1.2.3.4'; my $end = '1.2.3.6'; while() { chomp(my($from,$to) = (split /\s+[^\d.]+\s+/)[0,1]); if($net->has_edge($from,$to) ) { print "duplication of $from to $to\n"; # shows Graph understands undirected links }else{ $net->add_edge($from, $to); print "link from $from to $to added\n"; } } print "the nodes are: ", join(', ', sort $net->vertices), "\n"; print "the links are: ",$net, "\n"; print "a shortest path from $start to $end is: ", join ' => ', $net->SP_Dijkstra($start,$end); __DATA__ 1.2.3.4 links with 1.2.3.5 1.2.3.5 links with 1.2.3.4 1.2.3.5 links with 1.2.3.6 1.2.3.6 links with 1.2.3.5 1.2.3.6 links with 1.2.3.7 1.2.3.7 links with 1.2.3.6 1.2.3.7 links with 1.2.3.4 1.2.3.4 links with 1.2.3.7 1.2.3.5 links with 1.2.3.7 1.2.3.7 links with 1.2.3.5 #### link from 1.2.3.4 to 1.2.3.5 added duplication of 1.2.3.5 to 1.2.3.4 link from 1.2.3.5 to 1.2.3.6 added duplication of 1.2.3.6 to 1.2.3.5 link from 1.2.3.6 to 1.2.3.7 added duplication of 1.2.3.7 to 1.2.3.6 link from 1.2.3.7 to 1.2.3.4 added duplication of 1.2.3.4 to 1.2.3.7 link from 1.2.3.5 to 1.2.3.7 added duplication of 1.2.3.7 to 1.2.3.5 the nodes are: 1.2.3.4, 1.2.3.5, 1.2.3.6, 1.2.3.7 the links are: 1.2.3.4=1.2.3.5,1.2.3.4=1.2.3.7,1.2.3.5=1.2.3.6,1.2.3.5=1.2.3.7,1.2.3.6=1.2.3.7 a shortest path from 1.2.3.4 to 1.2.3.6 is: 1.2.3.4 => 1.2.3.7 => 1.2.3.6