Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: How do I use Graph::Traversal?

by Anonymous Monk
on Aug 08, 2016 at 22:08 UTC ( [id://1169377]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1169377]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2024-04-19 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found