
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Disjoint Set Data Structure
Disjoint set is basically as group of sets where no item can be in more than one set. It supports union and find operation on subsets.
Find(): It is used to find in which subset a particular element is in and returns the representative of that particular set.
Union(): It merges two different subsets into a single subset and representative of one set becomes representative of other.
Functions and pseudocodes
Begin Assume k is the element makeset(k): k.parent = k. Find(x): If k.parent == k return k. else return Find(k.parent) Union (a,b): Take two set a and b as input. aroot = Find(a) broot = Find(b) aroot.parent = broot End
Example
#include <iostream> #include <vector> #include <unordered_map> using namespace std; class DisjointSet { //to represent disjoint set unordered_map<int, int> parent; public: void makeSet(vector<int> const &wholeset){ //perform makeset operation for (int i : wholeset) // create n disjoint sets (one for each item) parent[i] = i; } int Find(int l) { // Find the root of the set in which element l belongs if (parent[l] == l) // if l is root return l; return Find(parent[l]); // recurs for parent till we find root } void Union(int m, int n) { // perform Union of two subsets m and n int x = Find(m); int y = Find(n); parent[x] = y; } }; void print(vector<int> const &universe, DisjointSet &dis) { for (int i : universe) cout << dis.Find(i) << " "; cout << '\n'; } int main() { vector<int> wholeset = { 6,7,1,2,3 }; // items of whole set DisjointSet dis; //initialize DisjointSet class dis.makeSet(wholeset); // create individual set of the items of wholeset dis.Union(7, 6); // 7,6 are in same set print(wholeset, dis); if (dis.Find(7) == dis.Find(6)) // if they are belong to same set or not. cout<<"Yes"<<endl; else cout<<"No"; if (dis.Find(3) == dis.Find(4)) cout<<"Yes"<<endl; else cout<<"No"; return 0; }
Output
6 6 1 2 3 Yes No
- Related Articles
- Golang program to implement stack data structure
- Golang program to implement graph data structure
- Java Program to Implement the graph data structure
- Java Program to Implement the queue data structure
- Golang program to implement the Queue data structure
- Golang program to implement binary tree data structure
- Python Program to Implement Queue Data Structure using Linked List
- Program to define set data structure without using library set class in Python
- Set Data Structure in Javascript
- C++ Program to Implement Set in STL
- Data Stream as Disjoint Intervals in C++
- C++ Program to Implement Dijkstra’s Algorithm Using Set
- C++ program to implement standard deviation of grouped data
- Find the number of Islands Using Disjoint Set in C++
- C++ Program to Find Maximum Number of Edge Disjoint Paths

Advertisements