Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: How to save first two columns of an array into another array

by zing (Beadle)
on Oct 02, 2012 at 15:19 UTC ( #996872=note: print w/ replies, xml ) Need Help??


in reply to Re^3: How to save first two columns of an array into another array
in thread How to save first two columns of an array into another array

Thanks all. So after introducing corrections suggested by you,I achieved at this code below. It operates on the same data file and outputs the connected components.But the problem is that the last part of the code i.e. the connected components part isnt working well.

If I enable "use strict" i get this error :- "Can't use string ("c") as an ARRAY ref while "strict refs" in use at arrar_input.pl line 44, <> line 3." Line 44 being this "for my $tgt ( \@{ $h{$src} } ) {"

Whereas if I disable "use strict" i get incomplete output for the connected components as :

----connected components--------------- - d: [] - a: [] b: []
The correct output should be this
----connected components--------------- - d: [e] - a: [c] b: [c]
My second question is that how can I access each of these connected components separately as per my need(i.e. either of them).
#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 i +nto 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;


Comment on Re^4: How to save first two columns of an array into another array
Select or Download Code
Re^5: How to save first two columns of an array into another array
by Athanasius (Monsignor) on Oct 03, 2012 at 03:38 UTC

    I can’t find any module named Graph::Subgraph on CPAN or meta::cpan. When I comment out the line use Graph::Subgraph; and run the code, I get the error you describe. The strict pragma is telling you that the syntax is wrong, so removing strict here would merely make the problem harder to identify!

    I’m not sure what the code in this for loop is meant to achieve, but perhaps something as simple as

    for my $src (keys %h) { $g->add_edge($src, $h{$src}); }

    will do what you want? If so, that leads to another, similar, "strict refs" error in the line $allgraphs[-1]{$node} = [ @{ $h{$node} } ];. But since I don’t understand what this final for loop is doing, I’m afraid I can’t offer any help here.  :-(

    Tip: In future, please show the minimum code that demonstrates the problem, and explain exactly what the code is intended to do. Comment the code liberally. See How do I post a question effectively?.

    Update: Just discovered, this issue has been re-addressed in Finding connected components in a graph..

    Athanasius <°(((><contra mundum

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-07-28 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (186 votes), past polls