TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
Classes | |
class | dsu |
Disjoint sets union data structure, class based representation. More... | |
Functions | |
static void | test1 () |
Self-test implementations, 1st test. | |
static void | test2 () |
Self-implementations, 2nd test. | |
int | main () |
Main function. | |
It is a very powerful data structure that keeps track of different clusters(sets) of elements, these sets are disjoint(doesnot have a common element). Disjoint sets uses cases : for finding connected components in a graph, used in Kruskal's algorithm for finding Minimum Spanning tree. Operations that can be performed: 1) UnionSet(i,j): add(element i and j to the set) 2) findSet(i): returns the representative of the set to which i belogngs to. 3) get_max(i),get_min(i) : returns the maximum and minimum Below is the class-based approach which uses the heuristic of path compression. Using path compression in findSet(i),we are able to get to the representative of i in O(1) time.
Definition in file dsu_path_compression.cpp.
int main | ( | void | ) |
Main function.
< number of items
< object of class disjoint sets
Definition at line 206 of file dsu_path_compression.cpp.
|
static |
Self-test implementations, 1st test.
< number of items
< object of class disjoint sets
Definition at line 170 of file dsu_path_compression.cpp.
|
static |
Self-implementations, 2nd test.
< number of items
< object of class disjoint sets
Definition at line 187 of file dsu_path_compression.cpp.