Graph::UnionFind(3pm)
π Graph::UnionFind π
okay bestie, so this is the Graph::UnionFind page from the User Contributed Perl Documentation section of the manual (that's the 3pm zone, iykyk) β it's a whole little module and honestly it's giving main character energy for a data structure.
πΈ NAME πΈ
Graph::UnionFind β union-find data structures π«Ά
β¨ SYNOPSIS β¨
like, here's the whole vibe in code, no cap:
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 )
π¦ DESCRIPTION π¦
so union-find is this special data structure that tracks how a set gets partitioned into subsets β a problem that also goes by the name disjoint sets, in case you see that name out in the wild and go "wait, do I know her?" π
Graph::UnionFind is what powers connected_components in Graph, connected_component in Graph, and same_connected_components in Graph β but only if you pass a true unionfind parameter when you create an undirected graph. that's the deal, fr fr. π
and bestie, important: union-find is one way. you cannot (easily) 'ununion' vertices once you have 'unioned' them. it's giving "no takebacks". π that is exactly why Graph throws an exception if you try to delete edges from a union-find graph β it is not playing about it.
π API π
π add
$uf->add(@v)
adds the vertices to the union-find. simple as that, slay. β¨
π union
$uf->union([$u, $v], [$w, $x], ...)
adds the edge u-v to the union-find. it also implicitly adds the vertices, so you don't gotta do that part yourself. π«Ά
π find
@partitions = $uf->find(@v)
for each vertex you hand it, it returns the union-find partition that vertex belongs to β or undef if it hasn't been added. undef is the "never seen her" of return values. π
π new
$uf = Graph::UnionFind->new()
the constructor. the origin story. the birth of the vibe. π±
π same
$uf->same($u, $v)
returns true if the vertices belong to the same union-find partition the vertex v belongs to, and false otherwise. π
π AUTHOR AND COPYRIGHT π
Jarkko Hietaniemi jhi@iki.fi β icon, legend, the moment. π
π LICENSE π
this module is licensed under the same terms as Perl itself. periodt. π
and that's the whole page, bestie!! thank you for coming to my TED talk π
perl v5.40.1 Β· 2025-03-28 Β· Graph::UnionFind(3pm) β mwah, go union some vertices, you got this π¦β¨
π¦ where this page comes from
- debian trixie: libgraph-perl 1:0.9735-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libgraph-perl 1:0.9735-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libgraph-perl 1:0.9735-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms