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

Re: connected component

by water (Deacon)
on May 14, 2005 at 18:56 UTC ( [id://457117]=note: print w/replies, xml ) Need Help??


in reply to connected component

Some time later, my own home-rolled hack, using a breadth first search:
package Algorithm::Graph; use strict; use Util qw(unique); # dedupes an array sub connected_components { my ($g) = @_; die "g must be arrayref" unless ref $g eq 'ARRAY'; die "empty graph?" unless @$g>0; my $adj; foreach my $pair (@$g) { die "g must be arrayref of arrayrefs" unless ref $pair eq 'ARR +AY'; die "g must be arrayref of 2-elem arrayrefs" unless @$pair == +2; my ( $x, $y ) = @$pair; $adj->{$x}{$y} = 1; $adj->{$y}{$x} = 1; } my %comp; for my $node ( keys %$adj ) { next if $comp{$node}; $comp{$node} = $node; my @neighbors = keys %{ $adj->{$node} }; while ( my $n = pop@ neighbors ) { die "set diff?" if $comp{$n} && $comp{$n} ne $node; $comp{$n} = $node; push( @neighbors, grep {! exists($comp{$_})} keys %{ $adj- +>{$n} } ); } } return [ map { my $c = $_; [ sort grep { $comp{$_} eq $c } keys %comp ]; } (sort (unique(values %comp))) ]; } 1;
And the tests
use strict; use warnings FATAL => 'all'; use Test::Exception; use Test::More tests => 12; use_ok('Algorithm::Graph'); my %graphs = ( '1,2' => [ [ 1, 2 ] ], 'a|b|c|d' => [ [qw(a a)], [qw(b b)], [qw(c c)], [qw(d d)] ], '1,2,3,4,99|a,b,c,d,e,f,g,z' => [ [qw(a b)], [qw(b c)], [qw(d c)], [qw(d e)], [qw(d f)], [qw(g d +)], [qw(z g)], [ 1, 2 ], [ 3, 2 ], [ 4, 3 ], [ 99, 1 ] ], '1,2,3,4,99|apple,kiwi,pear|a,b,c,d,e,f,g,z' => [ [qw(a b)], [qw(b c)], [qw(d c)], [qw(d e)], [qw(d f)], [qw(g d)], [qw(z g)], [ 1, 2 ], [ 3, 2 ], [ 4, 3 ], [ 99, 1 ], [ 'apple', 'pear' ], [ 'apple', 'kiwi' ] ], ); foreach my $result ( keys %graphs ) { my $cc = Algorithm::Graph::connected_components( $graphs{$result} +); my $cc2 = join( "|", map { join( ",", @$_ ) } @$cc ); is( $cc2, $result, $cc2 ); } throws_ok { Algorithm::Graph::connected_components() } qr/arrayref/, ' +empty'; throws_ok { Algorithm::Graph::connected_components('fish') } qr/arrayr +ef/, 'fish'; throws_ok { Algorithm::Graph::connected_components( {} ) } qr/arrayref +/, 'hash'; throws_ok { Algorithm::Graph::connected_components( [] ) } qr/empty/, +'[]'; throws_ok { Algorithm::Graph::connected_components( [ [] ] ) } qr/arra +yref/, '[[]]'; throws_ok { Algorithm::Graph::connected_components( [ [ 1, 2, 3 ] ] ) +} qr/2-elem/, '[[1,2,3]]'; throws_ok { Algorithm::Graph::connected_components( [ [ 1, 2 ], [] ] ) + } qr/2-elem/, '[[1,2],[]]';
Your mileage may vary.

water

Replies are listed 'Best First'.
Re^2: connected component
by tlm (Prior) on May 15, 2005 at 09:32 UTC

    Representing graphs as edge sets is not sufficiently general, because edge sets alone cannot represent graphs that have isolated nodes. That's why the standard mathematical definition of a graph consists of both a set of nodes and a set of edges formed from nodes in the set of nodes.

    the lowliest monk

      an inelegant cheesy solution, but I had this case covered by bogus self <-> self links, as shown by the test on 4 iso nodes, which return as 4 iso components.
      # resulting components <--- graph 'a|b|c|d' => [ [qw(a a)], [qw(b b)], [qw(c c)], [qw(d d)] ],
      again, a cheesy handrolled quick hack for what I needed, is all.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found