TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
connected_components_with_dsu.cpp File Reference

Disjoint union More...

#include <cstdint>
#include <iostream>
#include <set>
#include <vector>
Include dependency graph for connected_components_with_dsu.cpp:

Go to the source code of this file.

Namespaces

namespace  graph
 Graph Algorithms.
 
namespace  disjoint_union
 Functions for Disjoint union implementation.
 

Functions

void graph::disjoint_union::make_set ()
 function the initialize every node as it's own parent
 
int64_t graph::disjoint_union::find_set (int64_t val)
 Find the component where following node belongs to.
 
void graph::disjoint_union::union_sets (int64_t node1, int64_t node2)
 Merge 2 components to become one.
 
uint32_t graph::disjoint_union::no_of_connected_components ()
 Find total no. of connected components.
 
static void test ()
 Test Implementations.
 
int main ()
 Main function.
 

Variables

uint32_t graph::disjoint_union::number_of_nodes = 0
 
std::vector< int64_t > graph::disjoint_union::parent {}
 
std::vector< uint32_t > graph::disjoint_union::connected_set_size {}
 

Detailed Description

Disjoint union

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.
Author
Unknown author
Sagar Pandya

Definition in file connected_components_with_dsu.cpp.

Function Documentation

◆ find_set()

int64_t graph::disjoint_union::find_set ( int64_t val)

Find the component where following node belongs to.

Parameters
valparent of val should be found
Returns
parent of val

Definition at line 54 of file connected_components_with_dsu.cpp.

54 {
55 while (parent[val] != val) {
56 parent[val] = parent[parent[val]];
57 val = parent[val];
58 }
59 return val;
60}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 117 of file connected_components_with_dsu.cpp.

117 {
118 test(); // Execute the tests
119 return 0;
120}
static void test()
Test Implementations.

◆ make_set()

void graph::disjoint_union::make_set ( )

function the initialize every node as it's own parent

Returns
void

Definition at line 43 of file connected_components_with_dsu.cpp.

43 {
44 for (uint32_t i = 1; i <= number_of_nodes; i++) {
45 parent[i] = i;
46 connected_set_size[i] = 1;
47 }
48}

◆ no_of_connected_components()

uint32_t graph::disjoint_union::no_of_connected_components ( )

Find total no. of connected components.

Returns
Number of connected components

Definition at line 85 of file connected_components_with_dsu.cpp.

85 {
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}
int64_t find_set(int64_t val)
Find the component where following node belongs to.

◆ test()

static void test ( )
static

Test Implementations.

Returns
void

Definition at line 97 of file connected_components_with_dsu.cpp.

97 {
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}
Disjoint sets union data structure, class based representation.

◆ union_sets()

void graph::disjoint_union::union_sets ( int64_t node1,
int64_t node2 )

Merge 2 components to become one.

Parameters
node11st component
node22nd component
Returns
void

Definition at line 67 of file connected_components_with_dsu.cpp.

67 {
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}

Variable Documentation

◆ connected_set_size

std::vector<uint32_t> graph::disjoint_union::connected_set_size {}

Definition at line 38 of file connected_components_with_dsu.cpp.

38{}; // size of each set

◆ number_of_nodes

uint32_t graph::disjoint_union::number_of_nodes = 0

Definition at line 36 of file connected_components_with_dsu.cpp.

◆ parent

std::vector<int64_t> graph::disjoint_union::parent {}

Definition at line 37 of file connected_components_with_dsu.cpp.

37{}; // parent of each node