mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
readability improved
This commit is contained in:
parent
f3e58c0f05
commit
5f873a7b11
@ -42,21 +42,21 @@ int find_set(int val) {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @brief To join 2 components to belong to one
|
* @brief To join 2 components to belong to one
|
||||||
* @param a 1st component
|
* @param node1 1st component
|
||||||
* @param b 2nd component
|
* @param node2 2nd component
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
void union_sets(int a, int b) {
|
void union_sets(int node1, int node2) {
|
||||||
a = find_set(a); // find the parent of a
|
node1 = find_set(node1); // find the parent of node1
|
||||||
b = find_set(b); // find the parent of b
|
node2 = find_set(node2); // find the parent of node2
|
||||||
|
|
||||||
// If parents of both nodes are not same, combine them
|
// If parents of both nodes are not same, combine them
|
||||||
if (a != b) {
|
if (node1 != node2) {
|
||||||
if (connected_set_size[a] < connected_set_size[b]) {
|
if (connected_set_size[node1] < connected_set_size[node2]) {
|
||||||
std::swap(a, b); // swap both components
|
std::swap(node1, node2); // swap both components
|
||||||
}
|
}
|
||||||
parent[b] = a; // make a node as parent of b node.
|
parent[node2] = node1; // make node1 as parent of node2.
|
||||||
connected_set_size[a] += connected_set_size[b]; // sum the size of both as they combined
|
connected_set_size[node1] += connected_set_size[node2]; // sum the size of both as they combined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user