1 package Graph
::UnionFind
;
14 my ($self, $elem) = @_;
15 $self->{ $elem } = [ $elem, 0 ];
19 my ($self, $elem) = @_;
20 exists $self->{ $elem };
24 return undef unless defined $_[1];
26 exists $_[0]->{ $_[ 1 ] } ?
$_[0]->{ $_[1] }->[ _PARENT
] : undef;
28 $_[0]->{ $_[1] }->[ _PARENT
] = $_[2];
31 Carp
::croak
(__PACKAGE__
. "::_parent: bad arity");
36 return unless defined $_[1];
38 exists $_[0]->{ $_[1] } ?
$_[0]->{ $_[1] }->[ _RANK
] : undef;
40 $_[0]->{ $_[1] }->[ _RANK
] = $_[2];
43 Carp
::croak
(__PACKAGE__
. "::_rank: bad arity");
49 my $px = $self->_parent( $x );
50 return unless defined $px;
51 $self->_parent( $x, $self->find( $px ) ) if $px ne $x;
56 my ($self, $x, $y) = @_;
57 $self->add($x) unless $self->has($x);
58 $self->add($y) unless $self->has($y);
59 my $px = $self->find( $x );
60 my $py = $self->find( $y );
62 my $rx = $self->_rank( $px );
63 my $ry = $self->_rank( $py );
64 # print "union($x, $y): px = $px, py = $py, rx = $rx, ry = $ry\n";
66 $self->_parent( $py, $px );
68 $self->_parent( $px, $py );
69 $self->_rank( $py, $ry + 1 ) if $rx == $ry;
74 my ($uf, $u, $v) = @_;
75 my $fu = $uf->find($u);
76 return undef unless defined $fu;
77 my $fv = $uf->find($v);
78 return undef unless defined $fv;
88 Graph::UnionFind - union-find data structures
93 my $uf = Graph::UnionFind->new;
95 # Add the vertices to the data structure.
99 # Join the partitions of the vertices.
100 $uf->union( $u, $v );
102 # Find the partitions the vertices belong to
103 # in the union-find data structure. If they
104 # are equal, they are in the same partition.
105 # If the vertex has not been seen,
107 my $pu = $uf->find( $u );
108 my $pv = $uf->find( $v );
109 $uf->same($u, $v) # Equal to $pu eq $pv.
111 # Has the union-find seen this vertex?
116 I<Union-find> is a special data structure that can be used to track the
117 partitioning of a set into subsets (a problem known also as I<disjoint sets>).
119 Graph::UnionFind() is used for Graph::connected_components(),
120 Graph::connected_component(), and Graph::same_connected_components()
121 if you specify a true C<union_find> parameter when you create an undirected
124 Note that union-find is one way: you cannot (easily) 'ununion'
125 vertices once you have 'unioned' them. This means that if you
126 delete edges from a C<union_find> graph, you will get wrong results
127 from the Graph::connected_components(), Graph::connected_component(),
128 and Graph::same_connected_components().
138 Add the vertex v to the union-find.
144 Add the edge u-v to the union-find. Also implicitly adds the vertices.
150 Return true if the vertex v has been added to the union-find, false otherwise.
156 Return the union-find partition the vertex v belongs to,
157 or C<undef> if it has not been added.
161 $uf = Graph::UnionFind->new()
169 Return true of the vertices belong to the same union-find partition
170 the vertex v belongs to, false otherwise.
174 =head1 AUTHOR AND COPYRIGHT
176 Jarkko Hietaniemi F<jhi@iki.fi>
180 This module is licensed under the same terms as Perl itself.