From 105370118b69634b50ee8ac7935fac42d221154c Mon Sep 17 00:00:00 2001 From: Aayush Vyas <70998175+AayushVyasKIIT@users.noreply.github.com> Date: Thu, 2 Sep 2021 21:30:28 +0530 Subject: [PATCH] added self-implementation test cases, changed int to uint64_t --- data_structures/dsu_path_compression.cpp | 100 +++++++++++++++-------- data_structures/dsu_union_rank.cpp | 93 ++++++++++++++------- 2 files changed, 130 insertions(+), 63 deletions(-) diff --git a/data_structures/dsu_path_compression.cpp b/data_structures/dsu_path_compression.cpp index d71bb6ac3..dc6b7a94a 100644 --- a/data_structures/dsu_path_compression.cpp +++ b/data_structures/dsu_path_compression.cpp @@ -19,6 +19,7 @@ #include /// for IO operations #include /// for std::vector +#include /// for assert using std::cout; using std::endl; @@ -30,34 +31,34 @@ using std::vector; */ class dsu{ private: - vector p; /// depth; /// setSize;/// maxElement;/// minElement;/// p; /// depth; /// setSize;/// maxElement;/// minElement;/// get(uint64_t i){ + vector ans; + ans.push_back(get_min(i)); + ans.push_back(get_max(i)); + ans.push_back(size(i)); + return ans; + } /** * @brief A utility function that returns the size of the set to which i belongs to * @param i element of some set * @returns size of the set to which i belongs to */ - int size(int i){ + uint64_t size(uint64_t i){ return setSize[findSet(i)]; } /** @@ -144,7 +150,7 @@ class dsu{ * @param i element of some set * @returns maximum of the set to which i belongs to */ - int get_max(int i){ + uint64_t get_max(uint64_t i){ return maxElement[findSet(i)]; } /** @@ -152,32 +158,58 @@ class dsu{ * @param i element of some set * @returns minimum of the set to which i belongs to */ - int get_min(int i){ + uint64_t get_min(uint64_t i){ return minElement[findSet(i)]; } }; +/** + * @brief Self-implementation Test case #1 + * @returns void + */ +static void test1() { + /* the minimum, maximum and size of the set*/ + uint64_t n = 10;///< number of items + dsu d(n+1);///< object of class disjoint sets + //set 1 + cout << "Test case# 1: passed"< ans = {1,4,3}; + for(uint64_t i=0;i ans = {3,7,4}; + for(uint64_t i=0;i /// for IO operations #include /// for std::vector +#include /// for assert using std::cout; @@ -31,20 +32,20 @@ using std::vector; */ class dsu{ private: - vector p; /// depth; /// setSize;/// p; /// depth; /// setSize;/// getParents(uint64_t i){ + vector ans; while(p[i]!=i){ - cout << i << " ->"; + ans.push_back(i); i = p[i]; } - cout << p[i] << endl; + ans.push_back(i); + return ans; } }; +/** + * @brief Self-implementation Test case #1 + * @returns void + */ +static void test1() { + /* checks the parents in the resultant structures */ + cout << "Test case# 1: passed"< ans = {7,5}; + for(uint64_t i=0;i ans = {2,1,10}; + for(uint64_t i=0;i