Exemplo n.º 1
0
double Graph::MSTAlgo() {
  DisjointSet<Vertex>* A = new DisjointSet<Vertex>(n);
  double totalweight = 0;
  for(int i = 0; i < n; ++i){
	A->MakeSet(i, AdjacencyList[i]->getElem());
  }
  sortEdge();
  for(int i = 0; i < EdgeList.size(); ++i){
	if(A->FindSet(EdgeList[i]->vertex_i)->getKey() != A->FindSet(EdgeList[i]->vertex_j)->getKey()){
	  A->Union(*(A->FindSet(EdgeList[i]->vertex_i)), *(A->FindSet(EdgeList[i]->vertex_j)));
	  totalweight += EdgeList[i]->weight;
	  MST.push_back(EdgeList[i]);
	}
  }
  
  
  return totalweight;
}
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;
}