/usr/share/perl5/Graph/UnionFind.pm is in libgraph-perl 1:0.91-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 170 171 172 173 174 175 176 177 178 179 180 181 182 | package Graph::UnionFind;
use strict;
sub _PARENT () { 0 }
sub _RANK () { 1 }
sub new {
my $class = shift;
bless { }, $class;
}
sub add {
my ($self, $elem) = @_;
$self->{ $elem } = [ $elem, 0 ] unless defined $self->{$elem};
}
sub has {
my ($self, $elem) = @_;
exists $self->{ $elem };
}
sub _parent {
return undef unless defined $_[1];
if (@_ == 2) {
exists $_[0]->{ $_[ 1 ] } ? $_[0]->{ $_[1] }->[ _PARENT ] : undef;
} elsif (@_ == 3) {
$_[0]->{ $_[1] }->[ _PARENT ] = $_[2];
} else {
require Carp;
Carp::croak(__PACKAGE__ . "::_parent: bad arity");
}
}
sub _rank {
return unless defined $_[1];
if (@_ == 2) {
exists $_[0]->{ $_[1] } ? $_[0]->{ $_[1] }->[ _RANK ] : undef;
} elsif (@_ == 3) {
$_[0]->{ $_[1] }->[ _RANK ] = $_[2];
} else {
require Carp;
Carp::croak(__PACKAGE__ . "::_rank: bad arity");
}
}
sub find {
my ($self, $x) = @_;
my $px = $self->_parent( $x );
return unless defined $px;
$self->_parent( $x, $self->find( $px ) ) if $px ne $x;
$self->_parent( $x );
}
sub union {
my ($self, $x, $y) = @_;
$self->add($x) unless $self->has($x);
$self->add($y) unless $self->has($y);
my $px = $self->find( $x );
my $py = $self->find( $y );
return 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 = $uf->find($u);
return undef unless defined $fu;
my $fv = $uf->find($v);
return undef unless defined $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 known also as I<disjoint sets>).
Graph::UnionFind() is used for Graph::connected_components(),
Graph::connected_component(), and Graph::same_connected_components()
if you specify a true C<union_find> parameter when you create an undirected
graph.
Note that union-find is one way: you cannot (easily) 'ununion'
vertices once you have 'unioned' them. This means that if you
delete edges from a C<union_find> graph, you will get wrong results
from the Graph::connected_components(), Graph::connected_component(),
and Graph::same_connected_components().
=head2 API
=over 4
=item add
$uf->add($v)
Add the vertex v to the union-find.
=item union
$uf->union($u, $v)
Add the edge u-v to the union-find. Also implicitly adds the vertices.
=item has
$uf->has($v)
Return true if the vertex v has been added to the union-find, false otherwise.
=item find
$uf->find($v)
Return the union-find partition the vertex v 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
|