|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
#include <cassert>#include <iostream>#include <vector>Classes | |
| class | dsu |
| Disjoint sets union data structure, class based representation. More... | |
Functions | |
| static void | test1 () |
| Self-implementations, 1st test. More... | |
| static void | test2 () |
| Self-implementations, 2nd test. More... | |
| int | main () |
| Main function. More... | |
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.
| int main | ( | void | ) |
Main function.
|
static |
Self-implementations, 1st test.
< number of elements
< object of class disjoint sets
< performs union operation on 1 and 2
|
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