TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Disjoint Sets Data Structure (Disjoint Sets) More...
#include <iostream>
#include <vector>
Go to the source code of this file.
Functions | |
void | CreateSet (int n) |
int | Find (int x) |
bool | InSameUnion (int x, int y) |
void | Union (int x, int y) |
int | main () |
Variables | |
vector< int > | root |
vector< int > | rank |
Disjoint Sets Data Structure (Disjoint Sets)
A disjoint set data structure (also called union find or merge find set) is a data structure that tracks a set of elements partitioned into a number of disjoint (non-overlapping) subsets. Some situations where disjoint sets can be used are- to find connected components of a graph, kruskal's algorithm for finding Minimum Spanning Tree etc. There are two operation which we perform on disjoint sets - 1) Union 2) Find
Definition in file disjoint_set.cpp.
void CreateSet | ( | int | n | ) |
Function to create a set
n | number of element |
Definition at line 37 of file disjoint_set.cpp.
int Find | ( | int | x | ) |
Find operation takes a number x and returns the set to which this number belongs to.
x | element of some set |
Definition at line 53 of file disjoint_set.cpp.
bool InSameUnion | ( | int | x, |
int | y ) |
A utility function to check if x and y are from same set or not
x | element of some set |
y | element of some set |
Definition at line 67 of file disjoint_set.cpp.
int main | ( | void | ) |
Main function
Definition at line 93 of file disjoint_set.cpp.
void Union | ( | int | x, |
int | y ) |
Union operation combines two disjoint sets to make a single set in this union function we pass two elements and check if they are from different sets then combine those sets
x | element of some set |
y | element of some set |
Definition at line 78 of file disjoint_set.cpp.
vector<int> rank |
Definition at line 29 of file disjoint_set.cpp.
vector<int> root |
Definition at line 29 of file disjoint_set.cpp.