From 3f87fcc4b67ef0ca69eb8c8bc536e8a51108ac32 Mon Sep 17 00:00:00 2001 From: coderanant Date: Thu, 21 May 2020 23:29:52 +0530 Subject: [PATCH 1/3] feat: add algorithm to check number of components with the help of Union Find Structure --- graph/connected_components_with_dsu.cpp | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 graph/connected_components_with_dsu.cpp diff --git a/graph/connected_components_with_dsu.cpp b/graph/connected_components_with_dsu.cpp new file mode 100644 index 000000000..f06f1beb9 --- /dev/null +++ b/graph/connected_components_with_dsu.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +int N; // denotes number of nodes; +int parent[1000001]; +int siz[1000001]; +void make_set() { // function the initialize every node as it's own parent + for (int i = 1; i <= N; i++) { + parent[i] = i; + siz[i] = 1; + } +} +// To find the component where following node belongs to +int find_set(int v) { + if (v == parent[v]) + return v; + return parent[v] = find_set(parent[v]); +} + +void union_sets(int a, int b) { // To join 2 components to belong to one + a = find_set(a); + b = find_set(b); + if (a != b) { + if (siz[a] < siz[b]) + std::swap(a, b); + parent[b] = a; + siz[a] += siz[b]; + } +} + +int no_of_connected_components() { // To find total no of connected components + std::set temp; // temp set to count number of connected components + for (int i = 1; i <= N; i++) + temp.insert(find_set(i)); + return temp.size(); +} + +// All critical/corner cases have been taken care of. +// Input your required values: (not hardcoded) +int32_t main() { + std::cin >> N; + make_set(); + int edges; + std::cin >> edges; // no of edges in the graph + while (edges--) { + int node_a, node_b; + std::cin >> node_a >> node_b; + union_sets(node_a, node_b); + } + std::cout << no_of_connected_components() << std::endl; + return 0; +} From 19be84cf588944f1a5b6755b0cc0b6764c08323d Mon Sep 17 00:00:00 2001 From: coderanant Date: Fri, 22 May 2020 00:49:03 +0530 Subject: [PATCH 2/3] fix bug --- graph/connected_components_with_dsu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graph/connected_components_with_dsu.cpp b/graph/connected_components_with_dsu.cpp index f06f1beb9..d8d0b011b 100644 --- a/graph/connected_components_with_dsu.cpp +++ b/graph/connected_components_with_dsu.cpp @@ -38,7 +38,7 @@ int no_of_connected_components() { // To find total no of connected components // All critical/corner cases have been taken care of. // Input your required values: (not hardcoded) -int32_t main() { +int main() { std::cin >> N; make_set(); int edges; From d3b60e0de80b288913d9d059e9a0aed38910cb92 Mon Sep 17 00:00:00 2001 From: coderanant Date: Fri, 22 May 2020 01:06:40 +0530 Subject: [PATCH 3/3] fix: dynamically allocate all arrays --- graph/connected_components_with_dsu.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/graph/connected_components_with_dsu.cpp b/graph/connected_components_with_dsu.cpp index d8d0b011b..2c5c6dab5 100644 --- a/graph/connected_components_with_dsu.cpp +++ b/graph/connected_components_with_dsu.cpp @@ -3,8 +3,8 @@ #include int N; // denotes number of nodes; -int parent[1000001]; -int siz[1000001]; +std::vector parent; +std::vector siz; void make_set() { // function the initialize every node as it's own parent for (int i = 1; i <= N; i++) { parent[i] = i; @@ -40,6 +40,8 @@ int no_of_connected_components() { // To find total no of connected components // Input your required values: (not hardcoded) int main() { std::cin >> N; + parent.resize(N + 1); + siz.resize(N + 1); make_set(); int edges; std::cin >> edges; // no of edges in the graph