use Graph; use Data::Dumper; use strict; use warnings; my @pairs = qw( a,b c,b d,f e,b f,g ); my $g = new Graph edges => [ map [ split /,/ ], @pairs ]; my @av = map [ map { my $v = $_; map [$v,$_], $g->successors($v) } @$_ ], $g->weakly_connected_components; print Dumper @av;