Skip to content
Snippets Groups Projects
Commit afeae3db authored by Studer Gabriel's avatar Studer Gabriel
Browse files

nicify according to Gerardos suggestions

parent 94ce3418
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#include <promod3/core/message.hh> #include <promod3/core/message.hh>
#include <queue> #include <queue>
#include <iostream>
namespace { namespace {
...@@ -155,17 +154,12 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric, ...@@ -155,17 +154,12 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric,
// represent this process using a binary tree, that has 2*num-1 nodes. // represent this process using a binary tree, that has 2*num-1 nodes.
// This is the upper limit for us because of the thresh value. // This is the upper limit for us because of the thresh value.
// so we might stop merging before building up the whole tree // so we might stop merging before building up the whole tree
Cluster** clusters = new Cluster*[2*num-1]; // The idea is to build up sub trees from the bottom up and only
// keep their roots (clusters that can be merged)
// on one hand, the is_root vector controls what clusters are root nodes, std::vector<Cluster*> clusters(2*num - 1, NULL);
// on the other hand it tracks what pointers are actually set
// root node means: a cluster that can be merged with another root node
std::vector<int> is_root(2*num - 1, 0);
for (uint i = 0; i < num; ++i) { for (uint i = 0; i < num; ++i) {
clusters[i] = new Cluster; clusters[i] = new Cluster;
clusters[i]->members.push_back(i); clusters[i]->members.push_back(i);
is_root[i] = 1;
} }
// generate a priority queue containing the distances between clusters // generate a priority queue containing the distances between clusters
...@@ -186,33 +180,27 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric, ...@@ -186,33 +180,27 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric,
} }
} }
int cluster_a = 0; uint merge_iteration = 0;
int cluster_b = 0;
Real actual_distance = 0.0;
// there is a maximum of num - 1 merge events // there is a maximum of num - 1 merge events
for(uint merge_iteration = 0; merge_iteration < num - 1; ++merge_iteration) { // after that, everything would be in the same cluster
for( ; merge_iteration < num - 1; ++merge_iteration) {
// search in priority queue until we get an element containing two // search in priority queue until we get an element containing two
// root clusters. Note, that we also find distance entries associated to // root clusters. Note, that we also find distance entries associated to
// clusters that have already been merged (and therefore deleted). // clusters that have already been merged (and therefore deleted).
// We just ignore them and continue the search // We just ignore them and continue the search
// Important: given the fact, that all clusters are merged into one cluster // Important: All clusters are merged into one cluster after num-1 merge steps.
// after num-1 merge steps. We're therefore guaranteed to find two clusters // We're therefore guaranteed to find two clusters to merge at this point.
// to merge at this point. while((clusters[distance_queue.top().first] == NULL ||
while(!distance_queue.empty()){ clusters[distance_queue.top().second] == NULL)){
const QueueEntry& current_entry = distance_queue.top();
if(is_root[current_entry.first]){
if(is_root[current_entry.second]){
cluster_a = current_entry.first;
cluster_b = current_entry.second;
actual_distance = current_entry.dist;
distance_queue.pop(); distance_queue.pop();
break;
}
} }
int cluster_a = distance_queue.top().first;
int cluster_b = distance_queue.top().second;
Real actual_distance = distance_queue.top().dist;
distance_queue.pop(); distance_queue.pop();
}
if(!(*cmp_ptr)(actual_distance, thresh)){ if(!(*cmp_ptr)(actual_distance, thresh)){
// don't merge clusters anymore, they're all above/below thresh // don't merge clusters anymore, they're all above/below thresh
...@@ -224,16 +212,15 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric, ...@@ -224,16 +212,15 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric,
clusters[new_cluster_idx] = new Cluster(*clusters[cluster_a]); clusters[new_cluster_idx] = new Cluster(*clusters[cluster_a]);
clusters[new_cluster_idx]->Merge(*clusters[cluster_b]); clusters[new_cluster_idx]->Merge(*clusters[cluster_b]);
// delete the old clusters and update the is_root vector // delete the old clusters (we only keep the roots!)
delete clusters[cluster_a]; delete clusters[cluster_a];
delete clusters[cluster_b]; delete clusters[cluster_b];
is_root[cluster_a] = 0; clusters[cluster_a] = NULL;
is_root[cluster_b] = 0; clusters[cluster_b] = NULL;
is_root[new_cluster_idx] = 1;
// calculate the distance from new cluster to all other root clusters // calculate the distance from new cluster to all other root clusters
for(int i = 0; i < new_cluster_idx; ++i){ for(int i = 0; i < new_cluster_idx; ++i){
if(is_root[i]){ if(clusters[i] != NULL){
Real distance = (*metric_ptr)(*clusters[i], *clusters[new_cluster_idx], Real distance = (*metric_ptr)(*clusters[i], *clusters[new_cluster_idx],
dist_matrix); dist_matrix);
distance_queue.push(QueueEntry(i, new_cluster_idx, distance)); distance_queue.push(QueueEntry(i, new_cluster_idx, distance));
...@@ -242,14 +229,12 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric, ...@@ -242,14 +229,12 @@ void DoClustering(Real** dist_matrix, uint num, DistanceMetric metric,
} }
//fill the output and clean up //fill the output and clean up
for (uint i = 0; i < is_root.size(); ++i) { for (uint i = 0; i < num + merge_iteration; ++i) {
if(is_root[i]){ if(clusters[i] != NULL){
output.push_back(clusters[i]->members); output.push_back(clusters[i]->members);
delete clusters[i]; delete clusters[i];
} }
} }
delete [] clusters;
} }
}} // ns }} // ns
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment