use strict; use warnings; use Graph::Directed; my $g = "Graph::Directed"->new; $g->add_edge(split ":") for qw( 0:6 0:12 0:18 0:24 6:12 6:18 6:24 12:18 12:24 18:24 ); # This doesn't protect against cyclic graphs sub paths_from_vertex { my $g = shift; my $v = shift; return unless $g->has_vertex($v); my @s = $g->successors($v); return [$v] unless @s; my @p = map { paths_from_vertex($g, $_) } @s; unshift @$_, $v for @p; return @p; } sub print_path { my @path = @{+shift}; for my $i ( 0 .. $#path-1 ) { print "$path[$i]:$path[$i+1] "; } print "\n"; } for my $i (0..24) { print_path($_) for paths_from_vertex($g, $i); }