2021-01-08 17:41:34 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union)
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
* The Disjoint union is the technique to find connected component in graph efficiently.
|
|
|
|
*
|
|
|
|
* ### Algorithm
|
|
|
|
* In Graph, if you have to find out the number of connected components, there are 2 options
|
|
|
|
* 1. Depth first search
|
|
|
|
* 2. Disjoint union
|
|
|
|
* 1st option is inefficient, Disjoint union is the most optimal way to find this.
|
2021-02-11 13:53:42 +08:00
|
|
|
*
|
|
|
|
* @author Unknown author
|
|
|
|
* @author [Sagar Pandya](https://github.com/sagarpandyansit)
|
2021-01-08 17:41:34 +08:00
|
|
|
*/
|
2021-02-11 13:53:42 +08:00
|
|
|
#include <iostream> /// for IO operations
|
2021-01-08 17:41:34 +08:00
|
|
|
#include <set> /// for std::set
|
|
|
|
#include <vector> /// for std::vector
|
2020-05-22 01:59:52 +08:00
|
|
|
|
2021-01-08 17:41:34 +08:00
|
|
|
/**
|
|
|
|
* @namespace graph
|
|
|
|
* @brief Graph Algorithms
|
|
|
|
*/
|
|
|
|
namespace graph {
|
|
|
|
/**
|
|
|
|
* @namespace disjoint_union
|
2021-02-11 13:53:42 +08:00
|
|
|
* @brief Functions for [Disjoint union](https://en.wikipedia.org/wiki/Disjoint_union) implementation
|
2021-01-08 17:41:34 +08:00
|
|
|
*/
|
|
|
|
namespace disjoint_union {
|
2021-02-11 13:53:42 +08:00
|
|
|
uint32_t number_of_nodes = 0; // denotes number of nodes
|
|
|
|
std::vector<int64_t> parent{}; // parent of each node
|
|
|
|
std::vector<uint32_t> connected_set_size{}; // size of each set
|
2021-01-08 17:41:34 +08:00
|
|
|
/**
|
|
|
|
* @brief function the initialize every node as it's own parent
|
|
|
|
* @returns void
|
|
|
|
*/
|
|
|
|
void make_set() {
|
2021-02-11 13:53:42 +08:00
|
|
|
for (uint32_t i = 1; i <= number_of_nodes; i++) {
|
2020-05-22 01:59:52 +08:00
|
|
|
parent[i] = i;
|
2020-08-08 00:49:12 +08:00
|
|
|
connected_set_size[i] = 1;
|
2020-05-22 01:59:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 17:41:34 +08:00
|
|
|
/**
|
2021-02-11 13:53:42 +08:00
|
|
|
* @brief Find the component where following node belongs to
|
2021-01-08 17:41:34 +08:00
|
|
|
* @param val parent of val should be found
|
|
|
|
* @return parent of val
|
|
|
|
*/
|
|
|
|
int64_t find_set(int64_t val) {
|
|
|
|
while (parent[val] != val) {
|
|
|
|
parent[val] = parent[parent[val]];
|
|
|
|
val = parent[val];
|
2020-08-08 00:49:12 +08:00
|
|
|
}
|
2021-01-08 17:41:34 +08:00
|
|
|
return val;
|
2020-05-22 01:59:52 +08:00
|
|
|
}
|
2021-01-08 17:41:34 +08:00
|
|
|
/**
|
2021-02-11 13:53:42 +08:00
|
|
|
* @brief Merge 2 components to become one
|
2021-01-08 17:41:34 +08:00
|
|
|
* @param node1 1st component
|
|
|
|
* @param node2 2nd component
|
|
|
|
* @returns void
|
|
|
|
*/
|
|
|
|
void union_sets(int64_t node1, int64_t node2) {
|
|
|
|
node1 = find_set(node1); // find the parent of node1
|
|
|
|
node2 = find_set(node2); // find the parent of node2
|
2020-05-22 01:59:52 +08:00
|
|
|
|
2021-01-08 17:41:34 +08:00
|
|
|
// If parents of both nodes are not same, combine them
|
|
|
|
if (node1 != node2) {
|
|
|
|
if (connected_set_size[node1] < connected_set_size[node2]) {
|
|
|
|
std::swap(node1, node2); // swap both components
|
2020-08-08 00:49:12 +08:00
|
|
|
}
|
2021-01-08 17:41:34 +08:00
|
|
|
parent[node2] = node1; // make node1 as parent of node2.
|
|
|
|
connected_set_size[node1] +=
|
|
|
|
connected_set_size[node2]; // sum the size of both as they combined
|
2020-05-22 01:59:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 17:41:34 +08:00
|
|
|
/**
|
2021-02-11 13:53:42 +08:00
|
|
|
* @brief Find total no. of connected components
|
2021-01-08 17:41:34 +08:00
|
|
|
* @return Number of connected components
|
|
|
|
*/
|
2021-02-11 13:53:42 +08:00
|
|
|
uint32_t no_of_connected_components() {
|
2021-01-08 17:41:34 +08:00
|
|
|
std::set<int64_t> temp; // temp set to count number of connected components
|
2021-02-11 13:53:42 +08:00
|
|
|
for (uint32_t i = 1; i <= number_of_nodes; i++) temp.insert(find_set(i));
|
2021-01-08 17:41:34 +08:00
|
|
|
return temp.size(); // return the size of temp set
|
2020-05-22 01:59:52 +08:00
|
|
|
}
|
2021-02-11 13:53:42 +08:00
|
|
|
} // namespace disjoint_union
|
|
|
|
} // namespace graph
|
|
|
|
|
2021-01-08 17:41:34 +08:00
|
|
|
/**
|
|
|
|
* @brief Test Implementations
|
|
|
|
* @returns void
|
|
|
|
*/
|
|
|
|
static void test() {
|
2021-02-11 13:53:42 +08:00
|
|
|
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;
|
2020-05-22 01:59:52 +08:00
|
|
|
std::cin >> edges; // no of edges in the graph
|
|
|
|
while (edges--) {
|
2021-01-08 17:41:34 +08:00
|
|
|
int64_t node_a = 0, node_b = 0;
|
2020-05-22 01:59:52 +08:00
|
|
|
std::cin >> node_a >> node_b;
|
2021-02-11 13:53:42 +08:00
|
|
|
dsu::union_sets(node_a, node_b);
|
2020-05-22 01:59:52 +08:00
|
|
|
}
|
2021-02-11 13:53:42 +08:00
|
|
|
std::cout << dsu::no_of_connected_components() << std::endl;
|
2020-05-22 01:59:52 +08:00
|
|
|
}
|
2021-01-08 17:41:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
|
|
|
int main() {
|
2021-02-11 13:53:42 +08:00
|
|
|
test(); // Execute the tests
|
2021-01-08 17:41:34 +08:00
|
|
|
return 0;
|
2021-02-12 10:34:41 +08:00
|
|
|
}
|