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-implementations, 1st test. | |
static void | test2 () |
Self-implementations, 2nd test. | |
int | main () |
Main function. | |
dsu : It is a very powerful data structure which 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) getParents(i): prints the parent of i and so on and so forth. Below is the class-based approach which uses the heuristic of union-ranks. Using union-rank in findSet(i),we are able to get to the representative of i in slightly delayed O(logN) time but it allows us to keep tracks of the parent of i.
Definition in file dsu_union_rank.cpp.
int main | ( | void | ) |
Main function.
Definition at line 183 of file dsu_union_rank.cpp.
|
static |
Self-implementations, 1st test.
< number of elements
< object of class disjoint sets
< performs union operation on 1 and 2
Definition at line 134 of file dsu_union_rank.cpp.
|
static |
Self-implementations, 2nd test.
< number of elements
< object of class disjoint sets
performs union operation on 1 and 2
keeping track of the changes using parent pointers
makes sure algorithm works fine
Definition at line 158 of file dsu_union_rank.cpp.