in reply to Re^3: How do I use Graph::Traversal?
in thread How do I use Graph::Traversal?

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

Replies are listed 'Best First'.
Re^5: How do I use Graph::Traversal?
by melmoth (Acolyte) on Aug 09, 2016 at 04:36 UTC

    A non redundant set of paths would be better as output if someone can do that, or, given the output as it is above, easily remove the redundant paths.

      Paths to what?

        Paths to what? From each source vertex return all paths to each reachable sink vertices.