Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Using a hash is a good solution. I used the following algorithm: If both the numbers to be made related already belong to different classes, the classes are "merged", i.e. all numbers belonging to the class with a greater index are re-classified to the other class.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @a = (1, 1, 2, 3, 4, 4, 8, 8); my @b = (3, 4, 3, 5, 6, 7, 9, 10); print Dumper related(\@a, \@b); sub related { my @a = @{ +shift }; my @b = @{ +shift }; die "Different length.\n" if @a != @b; my %r; PAIR: for my $i (0 .. $#a) { my @classes; for my $e ($a[$i], $b[$i]) { push @classes, $r{$e} if exists $r{$e}; } # Both numbers already classified. Merge their classes if diff +erent. if (@classes == 2) { next PAIR if $classes[0] == $classes[1]; my ($min, $max) = sort { $a <=> $b } @r{$a[$i], $b[$i]}; $r{$_} = $min for grep $r{$_} == $max, keys %r; # Just one number already classified. Classify the second one +to the same class. } elsif (@classes == 1) { if (exists $r{$a[$i]}) { $r{$b[$i]} = $r{$a[$i]}; } else { $r{$a[$i]} = $r{$b[$i]}; } # Both numbers are seen for the first time. } else { # @classes == 0 my $min = $a[$i] < $b[$i] ? $a[$i] : $b[$i]; @r{$a[$i], $b[$i]} = ($min) x 2; } } return \%r; }
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: to find inter connected items by choroba
in thread to find inter connected items by Janaki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (11)
    As of 2014-12-22 19:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (128 votes), past polls