I was inspired to revisit this by a discussion in the chatterbox with gohaku, tye and Enlil.
Looks like your second guess was right, Anonymous Monk.
I can't for the life of me figure out what's going on in Graph::Traversal, which is the base class for Graph::BFS. The POD reads %param documentation to be written, which is a little disappointing.
However, I was able to address the problem you're trying to solve using SSSP_Dijkstra(), which also is a bit short on the POD:
#!/usr/bin/perl -w
use strict;
use Graph::Directed;
# Construct the graph
# This is a diagram of the graph we'll construct
# note there are two possible paths from D to J
# we want D->M->J since it's shorter than D->M->P->J
# D -> M -> P
# | |
# \ v
# -> J
my $G = new Graph::Directed;
$G = $G->add_edge("dave","mark");
$G = $G->add_edge("mark","paul");
$G = $G->add_edge("mark","john");
$G = $G->add_edge("paul","john");
# use the built-in SSSP_Dijkstra method
# creates a new graph, rooted at "dave"
my $SSP = $G->SSSP_Dijkstra("dave");
# this returns $SSP, which is another graph with every vertex
# reachable from "dave".
# Note that the "path" attribute for each vertex is a listref
# indicating the shortest way to reach that path (using Dijkstra's
# algorithm) from 'dave'. This is not documented in the
# Graph::Base documentation.
# find path to "john"
my @path2John = @{$SSP->get_attribute('path', "john")};
for (@path2John) {
print;
print "\n";
}
__END__
dave
mark
john
This issues a bunch of warnings about Use of uninitialized value in addition (+), which is because of some sloppy code in Graph::Base, but it gets the right answer. (they need the Perl 6 // operator!).
Hope that helps!
|