1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package Graph::UnionFind;
use strict;
use warnings;
sub _PARENT () { 0 }
sub _RANK () { 1 }
sub new {
my $class = shift;
bless { }, $class;
}
sub add {
my ($self, @elems) = @_;
@elems = grep !defined $self->{$_}, @elems;
@$self{ @elems } = map [ $_, 0 ], @elems;
}
sub _parent {
return undef unless defined $_[1];
Graph::__carp_confess(__PACKAGE__ . "::_parent: bad arity") if @_ < 2 or @_ > 3;
if (@_ == 2) {
exists $_[0]->{ $_[ 1 ] } ? $_[0]->{ $_[1] }->[ _PARENT ] : undef;
} else {
$_[0]->{ $_[1] }->[ _PARENT ] = $_[2];
}
}
sub _rank {
return unless defined $_[1];
Graph::__carp_confess(__PACKAGE__ . "::_rank: bad arity") if @_ < 2 or @_ > 3;
if (@_ == 2) {
exists $_[0]->{ $_[1] } ? $_[0]->{ $_[1] }->[ _RANK ] : undef;
} else {
$_[0]->{ $_[1] }->[ _RANK ] = $_[2];
}
}
sub find {
my ($self, @v) = @_;
my @ret;
for my $x (@v) {
push(@ret, undef), next unless defined(my $px = $self->_parent($x));
$self->_parent( $x, $self->find( $px ) ) if $px ne $x;
push @ret, $self->_parent( $x );
}
@ret;
}
sub union {
my ($self, @edges) = @_;
$self->add(map @$_, @edges);
for my $e (@edges) {
my ($px, $py) = $self->find( @$e );
next if $px eq $py;
my $rx = $self->_rank( $px );
my $ry = $self->_rank( $py );
# print "union($x, $y): px = $px, py = $py, rx = $rx, ry = $ry\n";
if ( $rx > $ry ) {
$self->_parent( $py, $px );
} else {
$self->_parent( $px, $py );
$self->_rank( $py, $ry + 1 ) if $rx == $ry;
}
}
}
sub same {
my ($uf, $u, $v) = @_;
my ($fu, $fv) = $uf->find($u, $v);
return undef if grep !defined, $fu, $fv;
$fu eq $fv;
}
1;
__END__
=pod
=head1 NAME
Graph::UnionFind - union-find data structures
=head1 SYNOPSIS
use Graph::UnionFind;
my $uf = Graph::UnionFind->new;
# Add the vertices to the data structure.
$uf->add($u);
$uf->add($v);
# Join the partitions of the vertices.
$uf->union( $u, $v );
# Find the partitions the vertices belong to
# in the union-find data structure. If they
# are equal, they are in the same partition.
# If the vertex has not been seen,
# undef is returned.
my $pu = $uf->find( $u );
my $pv = $uf->find( $v );
$uf->same($u, $v) # Equal to $pu eq $pv.
# Has the union-find seen this vertex?
$uf->has( $v )
=head1 DESCRIPTION
I<Union-find> is a special data structure that can be used to track the
partitioning of a set into subsets (a problem also known as I<disjoint sets>).
C<Graph::UnionFind> is used for L<Graph/connected_components>,
L<Graph/connected_component>, and L<Graph/same_connected_components>
if you specify a true C<unionfind> parameter when you create an undirected
graph.
Union-find is one way: you cannot (easily) 'ununion' vertices once you
have 'unioned' them. This is why L<Graph> throws an exception if you
try to delete edges from a union-find graph.
=head2 API
=over 4
=item add
$uf->add(@v)
Add the vertices to the union-find.
=item union
$uf->union([$u, $v], [$w, $x], ...)
Add the edge u-v to the union-find. Also implicitly adds the vertices.
=item find
@partitions = $uf->find(@v)
For each given vertex, return the union-find partition it belongs to,
or C<undef> if it has not been added.
=item new
$uf = Graph::UnionFind->new()
The constructor.
=item same
$uf->same($u, $v)
Return true of the vertices belong to the same union-find partition
the vertex v belongs to, false otherwise.
=back
=head1 AUTHOR AND COPYRIGHT
Jarkko Hietaniemi F<jhi@iki.fi>
=head1 LICENSE
This module is licensed under the same terms as Perl itself.
=cut
|