----connected components---------------
- d: []
- a: []
b: []
####
----connected components---------------
- d: [e]
- a: [c]
b: [c]
####
#This program read the triplets from file named "data" into
#an array of array.
#use strict;
use warnings;
use Data::Dumper;
use Graph;
use Graph::Subgraph;
# use PDL;
my @S;
while (<>) {
push @S, [ split ];
}
print "#########TRIPLETS#####\n";
print Dumper \@S;
print "\n$S[0][1]\n";
# Find the number of vertices
my @L;
for my $i ( 0 .. $#S ) {
for my $j ( 0 .. $#{ $S[$i] } ) {
#print "elt $i $j is $S[$i][$j]\n";
push (@L,$S[$i][$j]);
}
}
my %seen;
@L = grep { ! $seen{ $_ }++ } @L;
print "########VERTICES##########\n";
print Dumper \@L;
# Now lets generate the G(L)
# In order to generate the G(L) we'll extract first two columns of S into another matrix
my @GL=@S;
splice(@$_, 2, 1)
foreach @GL;
print Dumper \@GL;
my %h = map { $_->[0] => $_->[1] } @S;
print Dumper(\%h);
##### CONNECTED COMPONENTS ##########
my $g = Graph->new( undirected => 1 );
for my $src ( keys %h ) {
for my $tgt ( \@{ $h{$src} } ) {
$g->add_edge($src, $tgt);
}
}
my @subgraphs = $g->connected_components;
my @allgraphs;
my $V = $g->vertices;
print "\n$V\n";
for my $subgraph ( @subgraphs ) {
push @allgraphs, {};
for my $node ( @$subgraph ) {
if ( exists $h{ $node } ) {
$allgraphs[-1]{$node} = [ @{ $h{$node} } ];
}
}
}
print "----connected components------------";
use YAML; print Dump \@allgraphs;