1

I try to understand boost::disjoint_sets_with_storage but even the simplest example possible doesn't work and I can't understand why.

#include <boost/pending/disjoint_sets.hpp>

int main(){
    boost::disjoint_sets_with_storage<> union_find;
    union_find.make_set(1);
    //..
}

The above code compiles but then it produces a segfault. I want use integers as elements but in the boost documentation there is no "Element" template parameter, so I assume it infers the type. But, what is the problem ? Thanks

François
  • 471
  • 1
  • 4
  • 10
  • you may find what you are looking for here [http://stackoverflow.com/questions/4134703/understanding-boostdisjoint-sets](http://stackoverflow.com/questions/4134703/understanding-boostdisjoint-sets) – Greg Jul 11 '16 at 21:18
  • I never liked this implementation. For all my needs I use my own implementation using list and unordered map. – Arunmu Jul 12 '16 at 05:29
  • @Greg Thanks but no, its not the same interface – François Jul 12 '16 at 05:52

1 Answers1

-1

I'm not 100% confident on working with it yet, but I was given an example which I try to simplify for here. I think it's easiest if you can simply work with integers from 0 to n-1 that are either in the same set or not.

#include <iostream>
#include <boost/pending/disjoint_sets.hpp>
#include <vector>
typedef boost::disjoint_sets_with_storage<> Uf;

std::vector<pair<int,int>> same_set;
// fill the vector with number pairs between 0 and n-1 where
// n is the number of unique elements in the set
// ...
int n = ...; // n is the number of unique set elements

Uf union_find_set(n); // creates the structure with numbers 0 to n-1
// in seperate sets.  -> one set is for 0, one set for 1, ...

for (auto same : same_set) {
    union_find_set.union_set(same.first, same.second);
}
// union_find_set now contains sets with the numbers
// 0 to n-1 according to which sets should be combined
// given in the vector same_set.

// check whether two elements are in the same set, i.e. 0 and 2:
std::cout << union_find_set.find_set(0, 2);
ABBDVD
  • 67
  • 10