Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: treat pairs, set as one item: looking for better way

by LanX (Chancellor)
on Oct 19, 2013 at 10:30 UTC ( #1058897=note: print w/replies, xml ) Need Help??


in reply to treat pairs, set as one item: looking for better way

Hello remiah!

It's a classical problem from graph theory.

Your pairs are edges and you wanna determine connectivity resp. connected components.

Don't know if the other posters are aware of edge cases (your example data is too simple to show them).

But there are already efficient (and proven) algorithms available!

Cheers Rolf

( addicted to the Perl Programming Language)

  • Comment on Re: treat pairs, set as one item: looking for better way

Replies are listed 'Best First'.
Re^2: treat pairs, set as one item: looking for better way
by remiah (Hermit) on Oct 19, 2013 at 11:58 UTC

    Hello LanX ! and oh my god !!!

    #!/usr/bin/perl use Graph::Undirected; use strict; use warnings; my $g = Graph::Undirected->new; my @key_array = 'a' .. 'z'; my @set_array = ( ['a', 'b'], ['e','f'], ['f','g'] ); $g->add_vertex( $_ ) for @key_array; $g->add_edge( $_->[0], $_->[1] ) for @set_array; print "$_\n" for sort { length($b) <=> length($a) || $a cmp $b } map {join( ',', @$_) } $g->connected_components;
    I totally forgot about Graph module until you tell me.
    I would like to read your pages you gave me.

    Really thanks for your reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058897]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2017-02-23 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (338 votes). Check out past polls.