1#ifndef PBAT_GRAPH_DISJOINTSETUNIONFIND_H
2#define PBAT_GRAPH_DISJOINTSETUNIONFIND_H
19template <common::CIndex TIndex = Index>
20class DisjointSetUnionFind
25 DisjointSetUnionFind() =
default;
68 Eigen::Vector<IndexType, Eigen::Dynamic> mParent;
69 Eigen::Vector<IndexType, Eigen::Dynamic> mRank;
70 Eigen::Vector<IndexType, Eigen::Dynamic> mSize;
73template <common::CIndex TIndex>
74DisjointSetUnionFind<TIndex>::DisjointSetUnionFind(
IndexType n) : DisjointSetUnionFind<TIndex>()
79template <common::CIndex TIndex>
82 mParent = Eigen::Vector<IndexType, Eigen::Dynamic>::LinSpaced(n,
IndexType(0), n - 1);
87template <common::CIndex TIndex>
91 mParent[u] =
Find(mParent[u]);
95template <common::CIndex TIndex>
98 assert(u != v &&
"Cannot union the same vertex with itself.");
104 if (mRank[ru] > mRank[rv])
107 mSize[ru] += mSize[rv];
113 mSize[rv] += mSize[ru];
119 if (mRank[ru] == mRank[rv])
123 return (mParent[ru] == ru) ? ru : rv;
126template <common::CIndex TIndex>
129 while (mParent[u] != u)
134template <common::CIndex TIndex>
137 return mSize[
Root(u)];
140template <common::CIndex TIndex>
143 return static_cast<TIndex
>(mSize.size());
IndexType Root(IndexType u) const
Find the root of the set containing vertex u without path compression.
Definition DisjointSetUnionFind.h:127
TIndex IndexType
Index type used in the disjoint set union-find structure.
Definition DisjointSetUnionFind.h:23
void Prepare(IndexType n)
Reserve memory for n vertices and reset the structure.
Definition DisjointSetUnionFind.h:80
IndexType Union(IndexType u, IndexType v)
Merge the sets containing vertices u and v
Definition DisjointSetUnionFind.h:96
IndexType Size(IndexType u) const
Find the size of the set containing vertex u
Definition DisjointSetUnionFind.h:135
IndexType NumVertices() const
Get the number of vertices in the disjoint set union-find structure.
Definition DisjointSetUnionFind.h:141
IndexType Find(IndexType u)
Find the root of the set containing vertex u
Definition DisjointSetUnionFind.h:88
Concepts for common types.
Definition Adjacency.h:24