1Graph::UnionFind(3) User Contributed Perl Documentation Graph::UnionFind(3)
2
3
4
6 Graph::UnionFind - union-find data structures
7
9 use Graph::UnionFind;
10 my $uf = Graph::UnionFind->new;
11
12 # Add the vertices to the data structure.
13 $uf->add($u);
14 $uf->add($v);
15
16 # Join the partitions of the vertices.
17 $uf->union( $u, $v );
18
19 # Find the partitions the vertices belong to
20 # in the union-find data structure. If they
21 # are equal, they are in the same partition.
22 # If the vertex has not been seen,
23 # undef is returned.
24 my $pu = $uf->find( $u );
25 my $pv = $uf->find( $v );
26 $uf->same($u, $v) # Equal to $pu eq $pv.
27
28 # Has the union-find seen this vertex?
29 $uf->has( $v )
30
32 Union-find is a special data structure that can be used to track the
33 partitioning of a set into subsets (a problem known also as disjoint
34 sets).
35
36 Graph::UnionFind() is used for Graph::connected_components(),
37 Graph::connected_component(), and Graph::same_connected_components() if
38 you specify a true "union_find" parameter when you create an undirected
39 graph.
40
41 Note that union-find is one way: you cannot (easily) 'ununion' vertices
42 once you have 'unioned' them. This means that if you delete edges from
43 a "union_find" graph, you will get wrong results from the
44 Graph::connected_components(), Graph::connected_component(), and
45 Graph::same_connected_components().
46
47 API
48 add
49 $uf->add($v)
50
51 Add the vertex v to the union-find.
52
53 union
54 $uf->union($u, $v)
55
56 Add the edge u-v to the union-find. Also implicitly adds the
57 vertices.
58
59 has
60 $uf->has($v)
61
62 Return true if the vertex v has been added to the union-find, false
63 otherwise.
64
65 find
66 $uf->find($v)
67
68 Return the union-find partition the vertex v belongs to, or "undef"
69 if it has not been added.
70
71 new
72 $uf = Graph::UnionFind->new()
73
74 The constructor.
75
76 same
77 $uf->same($u, $v)
78
79 Return true of the vertices belong to the same union-find partition
80 the vertex v belongs to, false otherwise.
81
83 Jarkko Hietaniemi jhi@iki.fi
84
86 This module is licensed under the same terms as Perl itself.
87
88
89
90perl v5.32.1 2021-01-27 Graph::UnionFind(3)