Hello.
With hash of hash, for looking ups.
#!/usr/bin/perl
use strict; use warnings; use Data::Dumper;
my @a=(1,1,2,3,4,4,8,8);
my @b=(3,4,3,5,6,7,9,10);
my $hoh={};
for ( 0 .. $#a){
$hoh->{$a[$_]}->{$b[$_]}++;
$hoh->{$b[$_]}->{$a[$_]}++;
}
sub recursive {
my ($hoh, $key, $path, @vals) =@_;
for ( grep { $_ ne $key } @vals){
next if exists($path->{$_});
$hoh->{$key}->{$_}++;
$path->{$_}++;
recursive($hoh, $key, $path, keys %{$hoh->{$_}} );
}
}
for (sort {$a <=> $b}keys %$hoh){
recursive($hoh, $_, {}, keys %{$hoh->{$_}} );
}
#print Dumper $hoh;
for (sort {$a <=> $b}keys %$hoh){
print "$_=" , join(",", keys %{$hoh->{$_}}), "\n";
}
__DATA__
this prints ...
1=6,4,3,7,2,5
2=6,4,1,3,7,5
3=6,4,1,7,2,5
4=6,1,3,7,2,5
5=6,1,4,3,7,2
6=1,4,3,7,2,5
7=6,1,4,3,2,5
8=10,9
9=8,10
10=8,9
And as BillKSmith points out, if you take this as a graph,
#!/usr/bin/perl
#with graph
use strict; use warnings; use Data::Dumper;
use Graph;
my $g =Graph::Undirected->new;
my @a=(1,1,2,3,4,4,8,8);
my @b=(3,4,3,5,6,7,9,10);
for ( 0 .. $#a){
$g->add_edge($a[$_], $b[$_]);
}
print Dumper $g->connected_components;
__DATA__
this prints ...$VAR1 = [
9,
8,
10
];
$VAR2 = [
6,
4,
1,
3,
2,
5,
7
];
regards.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.