All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
k_center.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014 Piotr Smulewicz
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_K_CENTER_HPP
16 #define PAAL_K_CENTER_HPP
17 
20 
21 #include <vector>
22 
23 namespace paal {
24 namespace greedy {
41 template <typename Metric, class OutputIterator, typename ItemIterator>
42 typename data_structures::metric_traits<Metric>::DistanceType
43 kCenter(const Metric &metric, unsigned int numberOfClusters,
44  const ItemIterator iBegin, const ItemIterator iEnd,
45  OutputIterator result) {
46 
48  std::vector<Dist> distance_from_closest_center(
49  std::distance(iBegin, iEnd), std::numeric_limits<Dist>::max());
50  ItemIterator last_centre = iBegin;
51  ItemIterator farthest_centre = iBegin;
52  Dist radius;
53  assert(numberOfClusters > 0);
54  do {
55  *result = *farthest_centre;
56  ++result;
57  radius = std::numeric_limits<Dist>::min();
58  auto it = distance_from_closest_center.begin();
59  for (auto i = iBegin; i != iEnd; ++i) {
60  assign_min(*it, metric(*last_centre, *i));
61  if (*it > radius) {
62  farthest_centre = i;
63  radius = *it;
64  }
65  ++it;
66  }
67  last_centre = farthest_centre;
68  } while (--numberOfClusters);
69  return radius;
70 }
71 
72 }
73 }
74 
75 #endif // PAAL_K_CENTER_HPP
data_structures::metric_traits< Metric >::DistanceType kCenter(const Metric &metric, unsigned int numberOfClusters, const ItemIterator iBegin, const ItemIterator iEnd, OutputIterator result)
this is solve K Center problem and return radius example:
Definition: k_center.hpp:43
void assign_min(T &t, const T &u)