Exemplo n.º 1
0
int main() {
	DisjointSet DS;
	DS.makeSet(0);
	DS.makeSet(1);
	DS.makeSet(2);
	DS.makeSet(3);
	DS.makeSet(4);
	DS.union_sets(0, 2);
	//cout << DS.find(2) << endl;
	DS.union_sets(3, 4);
	DS.union_sets(0, 1);
	DS.union_sets(1, 2);
	DS.union_sets(1, 3);
	cout << DS.size() << endl;
	cout << DS.find(0) << endl;
	cout << DS.find(1) << endl;
	cout << DS.find(2) << endl;
	cout << DS.find(3) << endl;
	cout << DS.find(4) << endl;
	//for(int i = 0; i < DS.size(); i++)
	//	cout << i << "'s root: " << DS.find(i) << endl;
	//DS.print();
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
  DisjointSet ds;
  for (int set_number=0; set_number<DSET_SIZE; ++set_number) {
    ds.MakeSet(set_number);
  }
  srand(time(NULL));
  int unions = 0;
  int attempts = 0;
  while (ds.size() > 1) {
    // generate random couples and do Union operations, up to when there is
    // only a single set left.
    int x = rand() % DSET_SIZE;
    int y = rand() % DSET_SIZE;
    if (ds.Union(x,y)) {
      unions += 1;
    } else {
      attempts += 1;
    }
    //std::cout << ds.toDot() << std::endl;
  }
  std::cerr << "unions=" << unions << ", attempts=" << attempts << std::endl;
  std::cout << ds.toDot() << std::endl;
  return 0;
}
Exemplo n.º 3
0
#define DISJOINT_SET_TEST

#include "third_party/catch.hpp"
#include "data_structures/disjoint_set.cpp"

TEST_CASE("Create a large set containing 10^8 elements", "[disjoint-set]") {
    /*
        Note: This test requires around 800MB of memory.
    */
    DisjointSet ds(1e8);
    REQUIRE(ds.size() == 1e8);
}

TEST_CASE("Check initial state", "[disjoint-set]") {
    DisjointSet ds(20);

    for (size_t element = 0; element < ds.size(); element++) {
        REQUIRE(ds.find(element) == element);
    }
}

TEST_CASE("Connect elements and verify their representatives", "[disjoint-set]") {
    DisjointSet ds(16);     // create a disjoint-set with elements from 0 to 15

    ds.join(0, 8);
    REQUIRE(ds.find(0) == 0);
    REQUIRE(ds.find(8) == 0);

    ds.join(3, 15);
    REQUIRE(ds.find(3) == 3);
    REQUIRE(ds.find(15) == 3);