plagent has asked for the wisdom of the Perl Monks concerning the following question:

I have those links.For example, Point A with Point C, Point C with Point E, So, ACE is the cluster grouped from those links. Which module should I use?
A   C
C   E
D   C
L   G
J   K
O   P
I   M
L   K
Now I want to cluster those links into those following groups:

ACED,

LKGJ,

OP

IM

Replies are listed 'Best First'.
Re: how can I group those links together?
by choroba (Cardinal) on Dec 03, 2015 at 07:26 UTC
    You can use the Graph module:
    #!/usr/bin/perl use warnings; use strict; use Graph; my @links = (qw( A C C E D C L G J K O P I M L K )); my $g = 'Graph::Undirected'->new; $g->add_edge(splice @links, 0, 2) while @links; print "@$_\n" for $g->connected_components;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      this is much a simple way. Thanks.
Re: how can I group those links together?
by Athanasius (Archbishop) on Dec 03, 2015 at 07:49 UTC

    Hello plagent,

    In the spirit of TMTOWTDI: You can also use one of the Set modules, such as Set::Tiny:

    #! perl use strict; use warnings; use Set::Tiny qw( set ); my @groups; push @groups, set(split) while <DATA>; OUTER: while (1) { for my $i (0 .. $#groups - 1) { for my $j ($i + 1 .. $#groups) { unless ($groups[$i]->is_disjoint($groups[$j])) { $groups[$i] = $groups[$i]->union($groups[$j]); splice @groups, $j, 1; next OUTER; } } } last; } print $_->as_string, "\n" for @groups; __DATA__ A C C E D C L G J K O P I M L K

    Output:

    17:47 >perl 1468_SoPW.pl (A C D E) (G J K L) (O P) (I M) 17:47 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,