OpenWalnut
1.5.0dev
|
Implements a very simple union-find datastructure aka disjoint_sets. More...
#include <WUnionFind.h>
Public Member Functions | |
WUnionFind (size_t size) | |
Creates a new union find datastructure with size elements where each element is initialized as a single set. More... | |
size_t | find (size_t x) |
Find the canonical element of the given element and do path compression. More... | |
std::shared_ptr< std::set< size_t > > | getMaxSet () |
Computes the set with maximum number of elements. More... | |
void | merge (size_t i, size_t j) |
Merges two components (iow: makes a union) where the given elements are members of. More... | |
Private Attributes | |
std::vector< size_t > | m_component |
Stores for each index its ID. More... | |
Friends | |
class | WUnionFindTest |
Access for test class. More... | |
Implements a very simple union-find datastructure aka disjoint_sets.
And you may use it like this:
typedef std::vector< SizeType > RankVector; typedef RankVector::iterator RankRAIter; typedef std::vector< VertexDescriptor > ParentVector; typedef ParentVector::iterator ParentRAIter; typedef boost::iterator_property_map< RankRAIter, IndexMap, std::iterator_traits< RankRAIter >::value_type, std::iterator_traits< RankRAIter >::reference > RankMap; typedef boost::iterator_property_map< ParentRAIter, IndexMap, std::iterator_traits< ParentRAIter >::value_type, std::iterator_traits< ParentRAIter >::reference > ParentMap; RankVector ranks( d_numOfVertices ); ParentVector parents( d_numOfVertices ); boost::disjoint_sets< RankMap, ParentMap > dset( boost::make_iterator_property_map( ranks.begin(), boost::get( boost::vertex_index, g ), ranks[0] ), boost::make_iterator_property_map( parents.begin(), boost::get( boost::vertex_index, g ), parents[0] ) ); // After the disjoint set dset is construct, we can use dset.make_set( u ); // make u a set dset.link( u, v ); // u and v are belonging to the same set. dset.find_set( u ); // find the set owning u. A representative of the set is returned
Definition at line 73 of file WUnionFind.h.
|
explicit |
Creates a new union find datastructure with size elements where each element is initialized as a single set.
size | Number of elements |
Definition at line 36 of file WUnionFind.cpp.
References m_component.
size_t WUnionFind::find | ( | size_t | x | ) |
Find the canonical element of the given element and do path compression.
http://en.wikipedia.org/wiki/Disjoint-set_data_structure
depth of recursion is said to be small, therefore, recursion works fine for large dataset, too.
WOutOfBounds | If x is bigger than the number of elements available |
x | The x'th element |
Definition at line 45 of file WUnionFind.cpp.
References m_component.
Referenced by WJoinContourTree::buildJoinTree(), tm_utils::componentDecomposition(), getMaxSet(), and merge().
std::shared_ptr< std::set< size_t > > WUnionFind::getMaxSet | ( | ) |
Computes the set with maximum number of elements.
If there are more than one candidate one is picked arbitrarily.
Definition at line 82 of file WUnionFind.cpp.
References find(), and m_component.
Referenced by WJoinContourTree::getVolumeVoxelsEnclosedByIsoSurface(), and WUnionFindTest::testMaxSet().
void WUnionFind::merge | ( | size_t | i, |
size_t | j | ||
) |
Merges two components (iow: makes a union) where the given elements are members of.
WOutOfBounds | If i or j are bigger than the number of elements available |
i | Element of some component |
j | Element of some (maybe other) component |
Definition at line 60 of file WUnionFind.cpp.
References find(), and m_component.
Referenced by WJoinContourTree::buildJoinTree(), tm_utils::componentDecomposition(), WJoinContourTree::getVolumeVoxelsEnclosedByIsoSurface(), WUnionFindTest::testMaxSet(), and WUnionFindTest::testUnionMergesToBiggerIndex().
|
friend |
Access for test class.
Definition at line 75 of file WUnionFind.h.
|
private |
Stores for each index its ID.
Definition at line 120 of file WUnionFind.h.
Referenced by find(), getMaxSet(), merge(), WUnionFindTest::testUnionMergesToBiggerIndex(), and WUnionFind().