carl  24.04
Computer ARithmetic Library
SetCover.cpp
Go to the documentation of this file.
1 #include "SetCover.h"
2 
4 
5 #include <cassert>
6 #include <numeric>
7 
8 namespace carl::covering {
9 
10 void SetCover::set(std::size_t set, std::size_t element) {
11  while (set >= mSets.size()) {
12  mSets.emplace_back();
13  }
14  mSets[set].set(element);
15 }
16 
17 void SetCover::set(std::size_t set, const Bitset& elements) {
18  while (set >= mSets.size()) {
19  mSets.emplace_back();
20  }
21  mSets[set] |= elements;
22 }
23 
24 std::size_t SetCover::element_count() const {
25  std::size_t max = 0;
26  for (const auto& d: mSets) {
27  max = std::max(max, d.size());
28  }
29  return max;
30 }
31 
33  for (auto& d: mSets) {
34  if (d.none()) {
35  d = Bitset();
36  }
37  }
38 }
39 
40 std::size_t SetCover::set_count() const {
41  return mSets.size();
42 }
43 
44 std::size_t SetCover::active_set_count() const {
45  return static_cast<std::size_t>(std::count_if(mSets.begin(), mSets.end(),
46  [](const auto& d){ return d.any(); }
47  ));
48 }
49 
50 std::size_t SetCover::largest_set() const {
51  assert(mSets.size() > 0);
52  std::size_t largest_id = 0;
53  std::size_t largest_size = mSets[0].count();
54  for (std::size_t id = 1; id < mSets.size(); ++id) {
55  std::size_t size = mSets[id].count();
56  if (size > largest_size) {
57  largest_id = id;
58  largest_size = size;
59  }
60  }
61  return largest_id;
62 }
63 
64 std::size_t SetCover::largest_set(const std::vector<double>& weights) const {
65  assert(mSets.size() > 0);
66  std::size_t largest_id = 0;
67  double largest_size = mSets[0].count() * weights[0];
68  for (std::size_t id = 1; id < mSets.size(); ++id) {
69  double size = mSets[id].count() * weights[id];
70  if (size > largest_size) {
71  largest_id = id;
72  largest_size = size;
73  }
74  }
75  return largest_id;
76 }
77 
79  return std::accumulate(mSets.begin(), mSets.end(), Bitset(),
80  [](const auto& lhs, const auto& rhs){
81  return lhs | rhs;
82  }
83  );
84 }
85 
86 void SetCover::select_set(std::size_t s) {
87  assert(mSets.size() > s);
88  auto selected = mSets[s];
89  for (auto& d: mSets){
90  d -= selected;
91  }
92 }
93 
94 std::ostream& operator<<(std::ostream& os, const SetCover& sc) {
95  std::size_t elems = sc.element_count();
96  os << "SetCover:" << std::endl;
97  for (const auto& s: sc.mSets) {
98  s.resize(elems);
99  os << "\t" << s << std::endl;
100  }
101  return os;
102 }
103 
104 }
std::ostream & operator<<(std::ostream &os, const SetCover &sc)
Print the set cover to os.
Definition: SetCover.cpp:94
This class is a simple wrapper around boost::dynamic_bitset.
Definition: Bitset.h:20
Represents a set cover problem.
Definition: SetCover.h:15
std::size_t active_set_count() const
Returns the number of active sets (that still cover uncovered elements).
Definition: SetCover.cpp:44
void select_set(std::size_t s)
Selects the given set and purges the covered elements from all other sets.
Definition: SetCover.cpp:86
void prune_sets()
Removes empty sets.
Definition: SetCover.cpp:32
std::vector< Bitset > mSets
The actual sets.
Definition: SetCover.h:20
Bitset get_uncovered() const
Returns the uncovered elements.
Definition: SetCover.cpp:78
std::size_t set_count() const
Returns the number of sets.
Definition: SetCover.cpp:40
std::size_t element_count() const
Returns the number of elements.
Definition: SetCover.cpp:24
void set(std::size_t set, std::size_t element)
States that s covers the given element.
Definition: SetCover.cpp:10
std::size_t largest_set() const
Returns the id of the largest set.
Definition: SetCover.cpp:50