38#include <gtest/gtest.h>
61 vector<size_t> parent;
65 explicit RefDsu(
size_t n) : parent(n), sz(n, 1), blocks(n)
67 for (
size_t i = 0; i < n; ++i)
73 if (n <= parent.size())
76 const size_t old = parent.size();
79 for (
size_t i = old; i < n; ++i)
86 while (x != parent[x])
88 parent[x] = parent[parent[x]];
94 bool connected(
size_t a,
size_t b)
99 void unite(
size_t a,
size_t b)
121TEST(UnionFixedRelation, empty_and_out_of_range)
124 EXPECT_EQ(rel.
size(), 0u);
127 EXPECT_THROW(rel.
join(0, 0), std::out_of_range);
130TEST(UnionFixedRelation, basic_connectivity_and_blocks)
133 EXPECT_EQ(rel.size(), 5u);
134 EXPECT_EQ(rel.get_num_blocks(), 5u);
136 EXPECT_TRUE(rel.are_connected(0, 0));
137 EXPECT_FALSE(rel.are_connected(0, 1));
140 EXPECT_TRUE(rel.are_connected(0, 1));
141 EXPECT_EQ(rel.get_num_blocks(), 4u);
144 EXPECT_TRUE(rel.are_connected(0, 2));
145 EXPECT_EQ(rel.get_num_blocks(), 3u);
147 const size_t before = rel.get_num_blocks();
149 EXPECT_EQ(rel.get_num_blocks(), before);
151 EXPECT_THROW(rel.are_connected(0, 5), std::out_of_range);
152 EXPECT_THROW(rel.join(4, 5), std::out_of_range);
155TEST(UnionFixedRelation, set_n_resets_state)
159 EXPECT_TRUE(rel.are_connected(0, 1));
160 EXPECT_EQ(rel.get_num_blocks(), 3u);
163 EXPECT_EQ(rel.size(), 2u);
164 EXPECT_EQ(rel.get_num_blocks(), 2u);
165 EXPECT_FALSE(rel.are_connected(0, 1));
166 EXPECT_THROW(rel.are_connected(0, 2), std::out_of_range);
169TEST(UnionFixedRelation, depth_cycle_and_invalid_parent_detection)
172 FixedRelationInspector rel(3);
176 EXPECT_THROW(rel.depth(0), std::logic_error);
180 FixedRelationInspector rel(3);
182 EXPECT_THROW(rel.depth(0), std::out_of_range);
186TEST(UnionFixedRelation, randomized_operations_match_reference)
188 constexpr size_t n = 50;
193 std::uniform_int_distribution<size_t> pick(0, n - 1);
195 for (
int step = 0; step < 2000; ++step)
197 const size_t a = pick(
rng);
198 const size_t b = pick(
rng);
202 EXPECT_EQ(rel.are_connected(a, b), ref.connected(a, b));
208 EXPECT_EQ(rel.get_num_blocks(), ref.blocks);
213TEST(UnionRelation, grows_dynamically_and_updates_blocks)
216 EXPECT_EQ(rel.
size(), 0u);
220 EXPECT_EQ(rel.
size(), 1u);
224 EXPECT_EQ(rel.
size(), 2u);
229 EXPECT_EQ(rel.
size(), 2u);
233 EXPECT_EQ(rel.
size(), 101u);
239TEST(UnionRelation, randomized_growth_matches_reference)
245 std::uniform_int_distribution<size_t> pick(0, 200);
247 for (
int step = 0; step < 1500; ++step)
249 const size_t a = pick(
rng);
250 const size_t b = pick(
rng);
252 ref.ensure(std::max(a, b) + 1);
257 EXPECT_EQ(rel.
size(), ref.parent.size());
264 EXPECT_EQ(rel.
size(), ref.parent.size());
270TEST(UnionRelationT, inserts_items_on_query_and_preserves_unique_ids)
275 EXPECT_EQ(rel.
size(), 1u);
279 EXPECT_EQ(rel.
size(), 2u);
286 const size_t size_before = rel.
size();
289 EXPECT_EQ(rel.
size(), size_before);
293TEST(UnionRelationT, supports_multiple_item_types)
297 EXPECT_FALSE(rel.
are_connected(std::string(
"a"), std::string(
"b")));
298 EXPECT_EQ(rel.
size(), 2u);
301 rel.
join(std::string(
"a"), std::string(
"b"));
302 EXPECT_TRUE(rel.
are_connected(std::string(
"a"), std::string(
"b")));
305 EXPECT_FALSE(rel.
are_connected(std::string(
"c"), std::string(
"a")));
306 EXPECT_EQ(rel.
size(), 3u);
Binary relation between a set of integers.
Fixed_Relation(const size_t n=0)
Initialize an empty binary relation of integers between 0 and n - 1.
size_t size() const noexcept
Return the number of items of the set (not of relation)
virtual size_t root(size_t i)
constexpr size_t get_num_blocks() const noexcept
Return the number of connected blocks, which is the number of equivalence classes.
void join(size_t i, size_t j)
Insert the pair '(i,j)' in the relation.
bool are_connected(size_t i, size_t j)
Return true if item i is related to item j.
size_t depth(size_t i) const
Binary relation between a set of items.
bool are_connected(const T &p, const T &q)
Return true if p and q are connected; inserts missing items into the relation.
void join(const T &p, const T &q)
Join p with q; inserts missing items into the relation.
Binary relation between a set of integers.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Union-Find (Disjoint Set Union) data structure.