#!/usr/bin/perl use strict; use Graph::Undirected; use Graph::Traversal::DFS; my $g = Graph::Undirected->new; $g->add_edge('a', 'b'); $g->add_edge('b', 'c'); $g->add_edge('c', 'd'); $g->add_edge('c', 'z'); $g->add_edge('d', 'e'); $g->add_edge('c', 'q'); $g->add_edge('q', 'r'); $g->add_edge('r', 's'); # what I'd like this to return is # a, b, c, d, e # c, q, r, s # c, z # see, since c is a branch point, I want c to be part of the set returned # by the program. # this current code starts at 'a' and cruises along the depth first tree # but it doesnt exhaustively find all the branches and produce the above # output. my $old_node; my $count = 0; my @pre; #this returns all paths #the call to print in post is only made at terminal nodes for each possible path from a branch #at this point @pre is a complete path from start to the terminal node #you can replace "print join()" with any desired action you want to perform on @pre, including making a copy my $startnode = 'a'; my $t = Graph::Traversal::DFS->new($g, pre => sub {push @pre,$_[0]}, post => sub { print join(',',@pre),"\n" if($g->degree($_[0]) == 1 && $_[0] ne $startnode); pop @pre;}, next_alphabetic=>1, # if you want output order to be deterministic, otherwise will display paths in arbitrary order start => $startnode); $t->dfs; print "\n";