36 inline std::vector<size_t>
sortedIndices(
const std::vector<double> &weights) {
38 const size_t n = weights.size();
41 std::vector<size_t> indices(n);
42 std::iota(indices.begin(), indices.end(), 0);
45 std::stable_sort(indices.begin(), indices.end(),
46 [&weights](
const size_t i0,
const size_t i1) {
47 return weights[i0] < weights[i1];
54 template <
class FACTOR>
56 const std::vector<double> &weights) {
57 if (fg.
size() != weights.size()) {
58 throw std::runtime_error(
59 "kruskal() failure: fg.size() != weights.size(), all factors need to " 71 const size_t n = variableIndex.
size();
72 std::vector<size_t> treeIndices;
73 treeIndices.reserve(n - 1);
80 for (
const size_t index : sortedIndices) {
81 const auto factor = fg[index];
84 if (factor->size() != 2)
continue;
88 const auto u = dsf.
find(factor->front()),
v = dsf.
find(factor->back());
91 const bool loop = (u ==
v);
97 treeIndices.push_back(index);
100 if (++count == n - 1)
break;
Typedefs for easier changing of types.
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
void merge(const KEY &x, const KEY &y)
Merge two sets.
std::vector< size_t > kruskal(const FactorGraph< FACTOR > &fg, const std::vector< double > &weights)
std::vector< size_t > sortedIndices(const std::vector< double > &weights)
Array< int, Dynamic, 1 > v
A thin wrapper around std::map that uses boost's fast_pool_allocator.
Allow for arbitrary type in DSF.
KEY find(const KEY &key) const
Given key, find the representative key for the set in which it lives.