Using next_successor with Graph::Traversal

A Tech article with Comments posted 17 January 2007.
Tags: , ,

I recently needed to do a graph traversal using the excellent perl Graph library, but I needed to determine the order for visiting new nodes in the fringe myself. Graph::Traversal provides a next_successor attribute which takes a code ref so that you can specify this behavior yourself. Unfortunately, in version 0.80 that feature isn’t well documented, and if you try to use it, you get an “unknown attribute” error. I’ve submitted a patch to enable, document, and test the feature. Now, if you want (for example) to do a breadth-first traversal, but visit the nodes in reverse alphabetical order, you can do this:

Graph::Traversal::BFS->new(
    first_root => 'a',
    pre => sub { print $_[0]; }
    next_successor => sub { (reverse sort keys %{$_[1]})[0]; },
)->bfs;

You might also enjoy:

  1. My first CPAN acknowledgement

blog comments powered by Disqus