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

o.k that last one was a poor example as I could have computed the components first. The solution is required for other logical possibilities, such as:

v1 -> v2

v2 -> v3

v2 -> v5

v3 -> v4

The paths would then be:

v1 -> v2 ->v3 -> v4

v1 -> v2 -> v3

Replies are listed 'Best First'.
Re^4: How do I use Graph::Traversal?
by Anonymous Monk on Aug 08, 2016 at 22:08 UTC

    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

      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?
Re^4: How do I use Graph::Traversal?
by Anonymous Monk on Aug 08, 2016 at 04:07 UTC
    Can you post runnable code?
      #!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Graph; use Graph::Traversal::DFS; my $g = Graph->new(); $graph->add_edges ( ['A', 'B'], ['B', 'C'], ['C', 'D'], ['D', 'K'], [' +D', 'Z'], ['K', 'R'] ); =some Path 1: A -> B -> C -> D -> K -> R Path 2: A -> B -> C -> D -> Z =cut my $t = Graph::Traversal::DFS->new($g); my @v = $t->preorder; print qq[Preorder:\n]; print qq[$_\t] foreach @v; print qq[\n]; @v = $t->postorder; print qq[Postorder:\n]; print qq[$_\t] foreach @v; print qq[\n]; my @r = $t->roots; print qq[Roots:\n]; print qq[$_\t] foreach @r; print qq[\n];