TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
connected_components_with_dsu.cpp
Go to the documentation of this file.
1
20
#include <cstdint>
21
#include <iostream>
22
#include <set>
23
#include <vector>
24
29
namespace
graph
{
35
namespace
disjoint_union
{
36
uint32_t number_of_nodes = 0;
// denotes number of nodes
37
std::vector<int64_t> parent{};
// parent of each node
38
std::vector<uint32_t> connected_set_size{};
// size of each set
43
void
make_set
() {
44
for
(uint32_t i = 1; i <= number_of_nodes; i++) {
45
parent[i] = i;
46
connected_set_size[i] = 1;
47
}
48
}
49
54
int64_t
find_set
(int64_t val) {
55
while
(parent[val] != val) {
56
parent[val] = parent[parent[val]];
57
val = parent[val];
58
}
59
return
val;
60
}
61
67
void
union_sets
(int64_t node1, int64_t node2) {
68
node1 =
find_set
(node1);
// find the parent of node1
69
node2 =
find_set
(node2);
// find the parent of node2
70
71
// If parents of both nodes are not same, combine them
72
if
(node1 != node2) {
73
if
(connected_set_size[node1] < connected_set_size[node2]) {
74
std::swap(node1, node2);
// swap both components
75
}
76
parent[node2] = node1;
// make node1 as parent of node2.
77
connected_set_size[node1] +=
78
connected_set_size[node2];
// sum the size of both as they combined
79
}
80
}
81
85
uint32_t
no_of_connected_components
() {
86
std::set<int64_t> temp;
// temp set to count number of connected components
87
for
(uint32_t i = 1; i <= number_of_nodes; i++) temp.insert(
find_set
(i));
88
return
temp.size();
// return the size of temp set
89
}
90
}
// namespace disjoint_union
91
}
// namespace graph
92
97
static
void
test
() {
98
namespace
dsu
= graph::disjoint_union;
99
std::cin >> dsu::number_of_nodes;
100
dsu::parent.resize(dsu::number_of_nodes + 1);
101
dsu::connected_set_size.resize(dsu::number_of_nodes + 1);
102
dsu::make_set();
103
uint32_t edges = 0;
104
std::cin >> edges;
// no of edges in the graph
105
while
(edges--) {
106
int64_t node_a = 0, node_b = 0;
107
std::cin >> node_a >> node_b;
108
dsu::union_sets(node_a, node_b);
109
}
110
std::cout << dsu::no_of_connected_components() << std::endl;
111
}
112
117
int
main
() {
118
test
();
// Execute the tests
119
return
0;
120
}
test
void test()
Definition
caesar_cipher.cpp:100
dsu
Disjoint sets union data structure, class based representation.
Definition
dsu_path_compression.cpp:34
graph::disjoint_union::find_set
int64_t find_set(int64_t val)
Find the component where following node belongs to.
Definition
connected_components_with_dsu.cpp:54
graph::disjoint_union::union_sets
void union_sets(int64_t node1, int64_t node2)
Merge 2 components to become one.
Definition
connected_components_with_dsu.cpp:67
graph::disjoint_union::no_of_connected_components
uint32_t no_of_connected_components()
Find total no. of connected components.
Definition
connected_components_with_dsu.cpp:85
graph::disjoint_union::make_set
void make_set()
function the initialize every node as it's own parent
Definition
connected_components_with_dsu.cpp:43
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
disjoint_union
Functions for Disjoint union implementation.
graph
Graph Algorithms.
graph
connected_components_with_dsu.cpp
Generated by
1.18.0