#define UNIONFIND_HH
#include "array.hh"
-/*
+/**
which points of a graph are connected?.
Union find, a standard algorithm:
Union_find (int sz);
private:
- Array<int> classes;
-
+ /**
+ This array provides the representing point for each node in the graph.
+ */
+ Array<int> classes_;
};
#endif
Union_find::Union_find (int n)
{
- classes.set_size (n);
+ classes_.set_size (n);
for (int i=0; i < n; i++)
{
- classes[i] = i;
+ classes_[i] = i;
}
}
Union_find::find (int i)
{
int rep = i;
- while (classes[rep] != rep)
- rep = classes[rep];
- while (classes[i] != rep)
+ while (classes_[rep] != rep)
+ rep = classes_[rep];
+ while (classes_[i] != rep)
{
- int next =classes[i];
- classes[i] = rep;
- i = next;
+ int next =classes_[i];
+ classes_[i] = rep;
+ i = next;
}
return rep;
}
{
i = find (i);
j = find (j);
- classes[i] = j;
+ classes_[i] = j;
}