Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit b79d625bbcbe89fc9dc8f86c77bc0be1f6a74c16 @angusb committed Jan 13, 2010
Showing with 185 additions and 0 deletions.
  1. +100 −0 DisjointSet.cpp
  2. +36 −0 DisjointSet.h
  3. +10 −0 Makefile
  4. 0 README
  5. +39 −0 Sample.cpp
100 DisjointSet.cpp
@@ -0,0 +1,100 @@
+#include "DisjointSet.h"
+
+DisjointSet::DisjointSet()
+ :numElts(0), numSets(0) {}
+
+DisjointSet::DisjointSet(const DisjointSet &d) {
+ copyAll(d);
+}
+
+DisjointSet::DisjointSet(int num) {
+ numElts = 0;
+ numSets = 0;
+ AddElements(num);
+}
+
+DisjointSet& DisjointSet::operator=(const DisjointSet &d) {
+ if (this != &d)
+ copyAll(d);
+
+ return *this;
+}
+
+void DisjointSet::AddElements(int num) {
+ assert (num >=0);
+
+ nodes.insert(nodes.end(), num, (Dnode*)(NULL));
+ for (int i=numElts; i<numElts+num; i++) {
+ nodes[i] = new Dnode();
+ nodes[i]->index = i;
+ }
+ numElts += num;
+ numSets += num;
+}
+
+int DisjointSet::Find(int elt) {
+ assert(elt < numElts);
+
+ Dnode* root = nodes[elt];
+
+ while (root->parent != NULL)
+ root = root->parent;
+
+ Dnode* setIter = nodes[elt];
+ while (setIter != root) {
+ Dnode* parent = setIter->parent;
+ setIter->parent = root;
+ setIter = parent;
+ }
+
+ return root->index;
+}
+
+void DisjointSet::Union(int set1, int set2) {
+ assert(set1 < numElts && set2 < numElts);
+
+ Dnode* set1_rep = nodes[Find(set1)];
+ Dnode* set2_rep = nodes[Find(set2)];
+
+ if (set1_rep == set2_rep)
+ return;
+
+ if (set1_rep->rank < set2_rep->rank)
+ set1_rep->parent = set2_rep;
+ else if (set1_rep->rank > set2_rep->rank)
+ set2_rep->parent = set1_rep;
+ else {
+ set2_rep->parent = set1_rep; // arbitrary, set1_rep->parent = set2->rep
+ set1_rep->rank += 1;
+ }
+
+ --numSets;
+}
+
+int DisjointSet::NumElements() const {
+ return numElts;
+}
+
+int DisjointSet::NumSets() const {
+ return numSets;
+}
+
+DisjointSet::~DisjointSet() {
+ for (int i=0; i<numElts; i++)
+ delete nodes[i];
+
+ nodes.clear();
+}
+
+void DisjointSet::copyAll(const DisjointSet &d) {
+ this->numElts = d.numElts;
+ this->numSets = d.numSets;
+
+ nodes.resize(numElts);
+ for (int i=0; i<numElts; i++)
+ nodes[i] = new Dnode(*d.nodes[i]);
+
+ for (int i=0; i<numElts; i++)
+ if (d.nodes[i]->parent != NULL)
+ nodes[i]->parent = nodes[d.nodes[i]->parent->index];
+}
36 DisjointSet.h
@@ -0,0 +1,36 @@
+#include <vector>
+#include <cassert>
+
+class DisjointSet {
+ public:
+ DisjointSet();
+ DisjointSet(const DisjointSet &d);
+ DisjointSet(int num);
+ DisjointSet &operator=(const DisjointSet &d);
+ ~DisjointSet();
+
+ void AddElements(int num); // REQ: num >= 0
+ void Union(int set1, int set2);
+ int Find(int element);
+ int NumElements() const;
+ int NumSets() const;
+
+ private:
+ void copyAll(const DisjointSet &d);
+
+ struct Dnode {
+ int index; // index of the elment the node represents
+ int rank;
+ Dnode* parent;
+
+ Dnode() {
+ parent = NULL;
+ index = -1;
+ rank = 0;
+ }
+ };
+
+ int numElts;
+ int numSets;
+ std::vector<Dnode*> nodes;
+};
10 Makefile
@@ -0,0 +1,10 @@
+all: djset
+
+CXX = g++
+OFLAGS = -03
+
+djset: DisjointSet.cpp Sample.cpp
+ $(CXX) $(OFLAGS) DisjointSet.cpp Sample.cpp -o djset
+
+clean:
+ /bin/rm djset
0 README
No changes.
39 Sample.cpp
@@ -0,0 +1,39 @@
+#include <iostream>
+#include "DisjointSet.h"
+
+using namespace std;
+
+void printElementSets(DisjointSet & s)
+{
+ for (int i = 0; i < s.NumElements(); ++i)
+ cout << s.Find(i) << " ";
+ cout << endl;
+}
+
+int main()
+{
+ DisjointSet s(10);
+ printElementSets(s);
+ // s.Union(s.Find(5),s.Find(3));
+ s.Union(5,3);
+ printElementSets(s);
+ s.Union(s.Find(1),s.Find(3));
+ printElementSets(s);
+ s.Union(s.Find(6),s.Find(7));
+ printElementSets(s);
+ s.Union(s.Find(8),s.Find(9));
+ printElementSets(s);
+ s.Union(s.Find(6),s.Find(9));
+ printElementSets(s);
+ s.AddElements(3);
+ printElementSets(s);
+ s.Union(s.Find(11),s.Find(12));
+ printElementSets(s);
+ s.Union(s.Find(9),s.Find(10));
+ printElementSets(s);
+ s.Union(s.Find(7),s.Find(11));
+ printElementSets(s);
+
+ system("pause");
+ return 0;
+}

0 comments on commit b79d625

Please sign in to comment.