Go to the documentation of this file.00001
00002 #ifndef bsta_k_medoid_h_
00003 #define bsta_k_medoid_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <vcl_vector.h>
00025 #include <vcl_cassert.h>
00026 #include <vcl_iostream.h>
00027 #include <vbl/vbl_array_2d.h>
00028
00029 class bsta_k_medoid
00030 {
00031 public:
00032 bsta_k_medoid(unsigned n_elements, bool verbose = false);
00033 ~bsta_k_medoid(){}
00034
00035
00036 inline void insert_distance(const unsigned i, const unsigned j, double d)
00037 {assert((i<n_elements_)&&(j<n_elements_));
00038 distance_array_[i][j] = d; distance_array_[j][i] = d;}
00039
00040
00041 inline double distance(const unsigned i, const unsigned j) const
00042 {assert((i<n_elements_)&&(j<n_elements_)); return distance_array_[i][j];}
00043
00044
00045 void do_clustering(const unsigned k);
00046
00047
00048 inline unsigned k() const
00049 {return medoids_.size();}
00050
00051
00052 unsigned medoid(const unsigned i) const
00053 {assert(i<medoids_.size()); return medoids_[i];}
00054
00055
00056 bool is_medoid(const unsigned i) const;
00057
00058
00059 inline unsigned size(const unsigned k) const
00060 {assert(k<this->k());return clusters_[k].size();}
00061
00062
00063 inline vcl_vector<unsigned> elements(const unsigned k)
00064 {assert(k<this->k());return clusters_[k];}
00065
00066
00067 bool in_cluster(const unsigned i, const unsigned k) const;
00068
00069
00070 double medoid_distance(const unsigned i) const;
00071
00072
00073 double total_distance(const unsigned k) const;
00074
00075
00076 inline void print_distance_array(vcl_ostream & str = vcl_cout)
00077 {str << '\n' << distance_array_ << '\n';}
00078
00079 protected:
00080
00081
00082 double dc(const unsigned i, const unsigned j, const unsigned k);
00083
00084
00085 double dcm(const unsigned j, const unsigned k);
00086
00087
00088 bool replace_medoid(const unsigned j, const unsigned k);
00089
00090
00091 bool test_medoid_swap(unsigned& mj, unsigned& mk);
00092
00093
00094 void clear_clusters();
00095
00096
00097 void form_clusters();
00098
00099 private:
00100
00101 bool verbose_;
00102
00103
00104 unsigned n_elements_;
00105
00106
00107 vcl_vector<unsigned> medoids_;
00108
00109
00110 vcl_vector<vcl_vector<unsigned> > clusters_;
00111
00112
00113 vbl_array_2d<double> distance_array_;
00114 };
00115
00116 #endif // bsta_k_medoid_h_