Here's my non Graph::Traversal solution. Still looking for something simpler and cleaner with ::Traversal fi someone can help thanks
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Graph; use Graph::Traversal::DFS; my $g = Graph->new(); $g->add_edges ( ['A', 'B'], ['B', 'C'], ['C', 'D'], ['D', 'K'], ['D', +'Z'], ['K', 'R'] ); my @source = $g->source_vertices; foreach ( @source ) { DFS($_); print qq[\n\n]; } sub DFS { my $start_node = shift; my @queue = ($start_node); my @paths; while(scalar(@queue) > 0) { my $node = pop(@queue); my @next_nodes; if ( index $node, ':' ) { my @n = split ':', $node; my $nnode = $n[-1]; @next_nodes = $g->successors($nnode); } else { @next_nodes = $g->successors($node); } @next_nodes = map { "$node".':'."$_" } @next_nodes; push @paths, $_ foreach @next_nodes; push @queue, @next_nodes; } for my $path ( @paths ) { print qq[$path\n]; } }
A:B
A:B:C
A:B:C:D
A:B:C:D:Z
A:B:C:D:K
A:B:C:D:K:R
In reply to Re^4: How do I use Graph::Traversal?
by Anonymous Monk
in thread How do I use Graph::Traversal?
by thor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |