From 0665fbdf64701026b2b5b268ee444f4e62aeb0c3 Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Thu, 11 Feb 2021 11:23:42 +0530 Subject: [PATCH] fix: merge conflict --- graph/connected_components_with_dsu.cpp | 49 +++++++++++++------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/graph/connected_components_with_dsu.cpp b/graph/connected_components_with_dsu.cpp index 5317361b2..670aa4496 100644 --- a/graph/connected_components_with_dsu.cpp +++ b/graph/connected_components_with_dsu.cpp @@ -10,8 +10,11 @@ * 1. Depth first search * 2. Disjoint union * 1st option is inefficient, Disjoint union is the most optimal way to find this. + * + * @author Unknown author + * @author [Sagar Pandya](https://github.com/sagarpandyansit) */ -#include /// for io operations +#include /// for IO operations #include /// for std::set #include /// for std::vector @@ -20,28 +23,26 @@ * @brief Graph Algorithms */ namespace graph { - /** * @namespace disjoint_union - * @brief Function for [Disjoint union] (https://en.wikipedia.org/wiki/Disjoint_union) implementation + * @brief Functions for [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union) implementation */ namespace disjoint_union { - -int64_t number_of_nodes; // denotes number of nodes -std::vector parent; // parent of each node -std::vector connected_set_size; // size of each set +uint32_t number_of_nodes = 0; // denotes number of nodes +std::vector parent{}; // parent of each node +std::vector connected_set_size{}; // size of each set /** * @brief function the initialize every node as it's own parent * @returns void */ void make_set() { - for (int64_t i = 1; i <= number_of_nodes; i++) { + for (uint32_t i = 1; i <= number_of_nodes; i++) { parent[i] = i; connected_set_size[i] = 1; } } /** - * @brief To find the component where following node belongs to + * @brief Find the component where following node belongs to * @param val parent of val should be found * @return parent of val */ @@ -53,7 +54,7 @@ int64_t find_set(int64_t val) { return val; } /** - * @brief To join 2 components to belong to one + * @brief Merge 2 components to become one * @param node1 1st component * @param node2 2nd component * @returns void @@ -73,40 +74,42 @@ void union_sets(int64_t node1, int64_t node2) { } } /** - * @brief To find total no of connected components + * @brief Find total no. of connected components * @return Number of connected components */ -int64_t no_of_connected_components() { +uint32_t no_of_connected_components() { std::set temp; // temp set to count number of connected components - for (int64_t i = 1; i <= number_of_nodes; i++) temp.insert(find_set(i)); + for (uint32_t i = 1; i <= number_of_nodes; i++) temp.insert(find_set(i)); return temp.size(); // return the size of temp set } +} // namespace disjoint_union +} // namespace graph + /** * @brief Test Implementations * @returns void */ static void test() { - std::cin >> number_of_nodes; - parent.resize(number_of_nodes + 1); - connected_set_size.resize(number_of_nodes + 1); - make_set(); - int64_t edges = 0; + namespace dsu = graph::disjoint_union; + std::cin >> dsu::number_of_nodes; + dsu::parent.resize(dsu::number_of_nodes + 1); + dsu::connected_set_size.resize(dsu::number_of_nodes + 1); + dsu::make_set(); + uint32_t edges = 0; std::cin >> edges; // no of edges in the graph while (edges--) { int64_t node_a = 0, node_b = 0; std::cin >> node_a >> node_b; - union_sets(node_a, node_b); + dsu::union_sets(node_a, node_b); } - std::cout << no_of_connected_components() << std::endl; + std::cout << dsu::no_of_connected_components() << std::endl; } -} // namespace disjoint_union -} // namespace graph /** * @brief Main function * @returns 0 on exit */ int main() { - graph::disjoint_union::test(); // Execute the tests + test(); // Execute the tests return 0; } \ No newline at end of file