31namespace gtsam::utils {
36inline 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];
54template <
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 "
67 const std::vector<size_t> sortedIndices =
68 gtsam::utils::sortedIndices(weights);
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.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
Allow for arbitrary type in DSF.
Disjoint set forest using an STL map data structure underneath Uses rank compression and union by ran...
Definition DSFMap.h:34
KEY find(const KEY &key) const
Given key, find the representative key for the set in which it lives.
Definition DSFMap.h:81
void merge(const KEY &x, const KEY &y)
Merge two sets.
Definition DSFMap.h:87
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition FactorGraph.h:58
size_t size() const
return the number of factors (including any null factors set by remove() ).
Definition FactorGraph.h:297
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition VariableIndex.h:41
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition VariableIndex.h:78
std::vector< size_t > kruskal(const FactorGraph< FACTOR > &fg, const std::vector< double > &weights)
Compute the minimum spanning tree (MST) using Kruskal's algorithm.
Definition kruskal-inl.h:55