34 #include "../exceptions/WOutOfBounds.h"
35 #include "../WAssert.h"
36 #include "../WLogger.h"
37 #include "../WStringUtils.h"
38 #include "WDendrogram.h"
67 WAssert( n > 0,
"A Dendrogram for an empty set of objects was created which makes no sence, if your really need it implement it!" );
77 throw WOutOfBounds(
"Accessed an empty WDendrogam via a call to a memeber function: " + caller +
" which needs access to elements." );
86 std::stringstream errMsg;
87 errMsg <<
"Bug: n=" <<
m_heights.size() <<
" many leafs can lead maximal to 2n-1 many nodes in a tree but this was violated now!" << std::endl;
104 std::stringstream result;
105 std::map< size_t, std::vector< size_t > > childsOfInnerNodes;
106 std::map< size_t, std::vector< size_t > > preds;
107 std::vector< size_t > level( 2 *
m_heights.size() + 1, 0 );
117 for(
size_t i = 0; i <
m_heights.size() + 1; ++i )
119 result <<
"(0, (" << i <<
",))" << std::endl;
120 childsOfInnerNodes[
m_parents[i] ].push_back( i );
131 size_t left = *( preds[ i ].begin() );
132 size_t right = *( preds[ i ].rbegin() );
133 level[i] = std::max( level[left], level[right] ) + 1;
137 childsOfInnerNodes[
m_parents[i] ].reserve( childsOfInnerNodes[
m_parents[i] ].size() + childsOfInnerNodes[i].size() );
138 childsOfInnerNodes[
m_parents[i] ].insert( childsOfInnerNodes[
m_parents[i] ].end(), childsOfInnerNodes[i].begin(),
139 childsOfInnerNodes[i].end() );
141 result <<
"(" << level[i] <<
", (";
142 size_t numElements = childsOfInnerNodes[i].size();
143 for( std::vector< size_t >::const_iterator cit = childsOfInnerNodes[i].begin(); cit != childsOfInnerNodes[i].end(); ++cit )
145 if( numElements == 1 )
147 result << *cit <<
"), ";
151 result << *cit <<
", ";
159 result <<
"(" << left <<
", " << right <<
"), " <<
m_heights[ i -
m_heights.size() - 1 ] <<
")" << std::endl;
const std::vector< double > & getHeights() const
Const reference to the heights array.
static boost::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
std::vector< size_t > m_parents
Stores the parents of leafs as well as of inner nodes.
Indicates invalid element access of a container.
void reset(size_t n)
Resets the whole dendrogram to the number of elements it should be used for.
const std::vector< size_t > & getParents() const
Returns const reference to the internal parents array.
void checkAndThrowExceptionIfUsedUninitialized(std::string caller) const
Checks if this instance is initialized.
std::vector< double > m_heights
Stores only for the inner nodes their heights.
size_t merge(size_t i, size_t j, double height)
Merges two elements (either inner nodes or leafs) given via the indices i and j.
WDendrogram()
Default constructs an empty dendrogram.
Class building the interface for classes that might be transferred using WModuleConnector.
std::string toString() const
Transform this dendrogram into a string, where each leaf or inner node is mapped to a special string...
static boost::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.