carl  24.04
Computer ARithmetic Library
greedy.cpp
Go to the documentation of this file.
1 #include "greedy.h"
2 
4 
6 
8  Bitset result;
9  Bitset uncovered = sc.get_uncovered();
10  while (uncovered.any()) {
11  auto s = sc.largest_set();
12  result.set(s);
13  uncovered -= sc.get_set(s);
14  sc.select_set(s);
15  }
16  return result;
17 }
18 
19 Bitset greedy_bounded(SetCover& sc, std::size_t bound) {
20  Bitset result;
21  Bitset uncovered = sc.get_uncovered();
22  while (sc.active_set_count() > bound) {
23  auto s = sc.largest_set();
24  result.set(s);
25  sc.select_set(s);
26  sc.prune_sets();
27  }
28  return result;
29 }
30 
31 Bitset greedy_weighted(SetCover& sc, const std::vector<double>& weights, std::size_t bound) {
32  Bitset result;
33  Bitset uncovered = sc.get_uncovered();
34  while (sc.active_set_count() > bound) {
35  auto s = sc.largest_set(weights);
36  result.set(s);
37  sc.select_set(s);
38  sc.prune_sets();
39  }
40  return result;
41 }
42 
43 }
Bitset greedy(SetCover &sc)
Simple greedy heuristic: Selects the largest remaining set until all elements are covered.
Definition: greedy.cpp:7
Bitset greedy_weighted(SetCover &sc, const std::vector< double > &weights, std::size_t bound)
Weighted greedy heuristic: Selects the largest remaining set according to the given weight function u...
Definition: greedy.cpp:31
Bitset greedy_bounded(SetCover &sc, std::size_t bound)
Bounded greedy heuristic: Selects the largest remaining set until at most bound constraints remain.
Definition: greedy.cpp:19
This class is a simple wrapper around boost::dynamic_bitset.
Definition: Bitset.h:20
Bitset & set(std::size_t n, bool value=true)
Sets the given bit to a value, true by default.
Definition: Bitset.h:140
bool any() const
Checks if any bits are set to true. Asserts that mDefault is false.
Definition: Bitset.h:167
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
const auto & get_set(std::size_t set) const
Returns the given set.
Definition: SetCover.h:27
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
Bitset get_uncovered() const
Returns the uncovered elements.
Definition: SetCover.cpp:78
std::size_t largest_set() const
Returns the id of the largest set.
Definition: SetCover.cpp:50