2 #ifndef HUGO_UNION_FIND_H
3 #define HUGO_UNION_FIND_H
7 //!\brief Union-Find data structures.
19 //! \addtogroup auxdat
23 * \brief A \e Union-Find data structure implementation
25 * The class implements the \e Union-Find data structure.
26 * The union operation uses rank heuristic, while
27 * the find operation uses path compresson.
28 * This is a very simple but efficient implementation, providing
29 * only four methods: join (union), find, insert and size.
30 * For more features see the \ref UnionFindEnum class.
32 * \pre The elements are automatically added only if the map
33 * given to the constructor was filled with -1's. Otherwise you
34 * need to add all the elements by the \ref insert() method.
37 template <typename T, typename TIntMap>
41 typedef T ElementType;
42 typedef std::pair<int,int> PairType;
45 std::vector<PairType> data;
49 UnionFind(TIntMap& m) : map(m) {}
52 * \brief Returns the index of the element's component.
54 * The method returns the index of the element's component.
55 * This is an integer between zero and the number of inserted elements.
66 while ( (next = data[comp].first) != comp) {
69 while ( (next = data[comp0].first) != comp) {
70 data[comp0].first = comp;
78 * \brief Insert a new element into the structure.
80 * This method inserts a new element into the data structure.
82 * It is not required to use this method:
83 * if the map given to the constructor was filled
84 * with -1's then it is called automatically
85 * on the first \ref find or \ref join.
87 * The method returns the index of the new component.
93 data.push_back(std::make_pair(n, 1));
99 * \brief Joining the components of element \e a and element \e b.
101 * This is the \e union operation of the Union-Find structure.
102 * Joins the component of elemenent \e a and component of
103 * element \e b. If \e a and \e b are in the same component then
104 * it returns false otherwise it returns true.
115 if ( data[ca].second > data[cb].second ) {
117 data[ca].second += data[cb].second;
121 data[cb].second += data[ca].second;
127 * \brief Returns the size of the component of element \e a.
129 * Returns the size of the component of element \e a.
135 return data[ca].second;
143 /*******************************************************/
146 #ifdef DEVELOPMENT_DOCS
149 * \brief The auxiliary class for the \ref UnionFindEnum class.
151 * In the \ref UnionFindEnum class all components are represented as
153 * Items of these lists are UnionFindEnumItem structures.
155 * The class has four fields:
156 * - T me - the actual element
157 * - IIter parent - the parent of the element in the union-find structure
158 * - int size - the size of the component of the element.
159 * Only valid if the element
160 * is the leader of the component.
161 * - CIter my_class - pointer into the list of components
162 * pointing to the component of the element.
163 * Only valid if the element is the leader of the component.
168 template <typename T>
169 struct UnionFindEnumItem {
171 typedef std::list<UnionFindEnumItem> ItemList;
172 typedef std::list<ItemList> ClassList;
173 typedef typename ItemList::iterator IIter;
174 typedef typename ClassList::iterator CIter;
181 UnionFindEnumItem() {}
182 UnionFindEnumItem(const T &_me, CIter _my_class):
183 me(_me), size(1), my_class(_my_class) {}
188 * \brief A \e Union-Find data structure implementation which
189 * is able to enumerate the components.
191 * The class implements an \e Union-Find data structure
192 * which is able to enumerate the components and the items in
193 * a component. If you don't need this feature then perhaps it's
194 * better to use the \ref UnionFind class which is more efficient.
196 * The union operation uses rank heuristic, while
197 * the find operation uses path compresson.
200 * need to add all the elements by the \ref insert() method.
204 template <typename T, template <typename Item> class Map>
205 class UnionFindEnum {
207 typedef std::list<UnionFindEnumItem<T> > ItemList;
208 typedef std::list<ItemList> ClassList;
209 typedef typename ItemList::iterator IIter;
210 typedef typename ItemList::const_iterator IcIter;
211 typedef typename ClassList::iterator CIter;
212 typedef typename ClassList::const_iterator CcIter;
215 typedef T ElementType;
216 typedef UnionFindEnumItem<T> ItemType;
217 typedef Map< IIter > MapType;
223 IIter _find(IIter a) const {
226 while( (next = comp->parent) != comp ) {
231 while( (next = comp1->parent) != comp ) {
232 comp1->parent = comp->parent;
239 UnionFindEnum(MapType& _m) : m(_m) {}
243 * \brief Insert the given element into a new component.
245 * This method creates a new component consisting only of the
249 void insert(const T &a)
253 classes.push_back(ItemList());
254 CIter aclass = classes.end();
257 ItemList &alist = *aclass;
258 alist.push_back(ItemType(a, aclass));
259 IIter ai = alist.begin();
267 * \brief Find the leader of the component of the given element.
269 * The method returns the leader of the component of the given element.
272 T find(const T &a) const {
273 return _find(m[a])->me;
278 * \brief Joining the component of element \e a and element \e b.
280 * This is the \e union operation of the Union-Find structure.
281 * Joins the component of elemenent \e a and component of
282 * element \e b. If \e a and \e b are in the same component then
283 * returns false else returns true.
286 bool join(T a, T b) {
288 IIter ca = _find(m[a]);
289 IIter cb = _find(m[b]);
295 if ( ca->size > cb->size ) {
297 cb->parent = ca->parent;
298 ca->size += cb->size;
300 ItemList &alist = *ca->my_class;
301 alist.splice(alist.end(),*cb->my_class);
303 classes.erase(cb->my_class);
308 ca->parent = cb->parent;
309 cb->size += ca->size;
311 ItemList &blist = *cb->my_class;
312 blist.splice(blist.end(),*ca->my_class);
314 classes.erase(ca->my_class);
323 * \brief Returns the size of the component of element \e a.
325 * Returns the size of the component of element \e a.
328 int size(const T &a) const {
329 return _find(m[a])->size;
334 * \brief Split up the component of the element.
336 * Splitting the component of the element into sigleton
337 * components (component of size one).
340 void split(const T &a) {
342 IIter ca = _find(m[a]);
347 CIter aclass = ca->my_class;
349 for(IIter curr = ca; ++curr != aclass->end(); curr=ca) {
350 classes.push_back(ItemList());
351 CIter nl = --classes.end();
352 nl->splice(nl->end(), *aclass, curr);
365 * \brief Set the given element to the leader element of its component.
367 * Set the given element to the leader element of its component.
370 void makeRep(const T &a) {
373 IIter la = _find(ia);
374 if (la == ia) return;
376 ia->my_class = la->my_class;
381 CIter l = ia->my_class;
382 l->splice(l->begin(),*l,ia);
390 * \brief Remove the given element from the structure.
392 * Remove the given element from the structure.
394 * Removes the element from its component and if the component becomes
395 * empty then removes that component from the component list.
397 void erase(const T &a) {
402 IIter la = _find(ma);
404 if (ma -> size == 1){
405 classes.erase(ma->my_class);
411 la->my_class = ma->my_class;
414 for (IIter i = la; i != la->my_class->end(); ++i) {
419 la->my_class->erase(ma);
424 * \brief Removes the component of the given element from the structure.
426 * Removes the component of the given element from the structure.
429 void eraseClass(const T &a) {
433 CIter c = _find(ma)->my_class;
434 for (IIter i=c->begin(); i!=c->end(); ++i)
437 classes.erase(_find(ma)->my_class);
442 friend class UnionFindEnum;
446 ClassIt(Invalid): i(0) {}
449 operator const T& () const {
450 ItemList const &ll = *i;
451 return (ll.begin())->me; }
452 bool operator == (ClassIt it) const {
455 bool operator != (ClassIt it) const {
458 bool operator < (ClassIt it) const {
462 bool valid() const { return i != 0; }
464 void first(const ClassList &l) { i = l.begin(); validate(l); }
465 void next(const ClassList &l) {
469 void validate(const ClassList &l) {
476 * \brief Sets the iterator to point to the first component.
478 * Sets the iterator to point to the first component.
480 * With the \ref first, \ref valid and \ref next methods you can
481 * iterate through the components. For example:
483 * UnionFindEnum<Graph::Node, Graph::NodeMap>::MapType map(G);
484 * UnionFindEnum<Graph::Node, Graph::NodeMap> U(map);
485 * UnionFindEnum<Graph::Node, Graph::NodeMap>::ClassIt iter;
486 * for (U.first(iter); U.valid(iter); U.next(iter)) {
487 * // iter is convertible to Graph::Node
488 * cout << iter << endl;
493 ClassIt& first(ClassIt& it) const {
499 * \brief Returns whether the iterator is valid.
501 * Returns whether the iterator is valid.
503 * With the \ref first, \ref valid and \ref next methods you can
504 * iterate through the components. See the example here: \ref first.
507 bool valid(ClassIt const &it) const {
512 * \brief Steps the iterator to the next component.
514 * Steps the iterator to the next component.
516 * With the \ref first, \ref valid and \ref next methods you can
517 * iterate through the components. See the example here: \ref first.
520 ClassIt& next(ClassIt& it) const {
527 friend class UnionFindEnum;
532 ItemIt(Invalid): i(0) {}
535 operator const T& () const { return i->me; }
536 bool operator == (ItemIt it) const {
539 bool operator != (ItemIt it) const {
542 bool operator < (ItemIt it) const {
546 bool valid() const { return i != 0; }
548 void first(const ItemList &il) { l=&il; i = l->begin(); validate(); }
562 * \brief Sets the iterator to point to the first element of the component.
565 * Sets the iterator to point to the first element of the component.
567 * With the \ref first2 "first", \ref valid2 "valid"
568 * and \ref next2 "next" methods you can
569 * iterate through the elements of a component. For example
570 * (iterating through the component of the node \e node):
572 * Graph::Node node = ...;
573 * UnionFindEnum<Graph::Node, Graph::NodeMap>::MapType map(G);
574 * UnionFindEnum<Graph::Node, Graph::NodeMap> U(map);
575 * UnionFindEnum<Graph::Node, Graph::NodeMap>::ItemIt iiter;
576 * for (U.first(iiter, node); U.valid(iiter); U.next(iiter)) {
577 * // iiter is convertible to Graph::Node
578 * cout << iiter << endl;
583 ItemIt& first(ItemIt& it, const T& a) const {
584 it.first( * _find(m[a])->my_class );
589 * \brief Returns whether the iterator is valid.
592 * Returns whether the iterator is valid.
594 * With the \ref first2 "first", \ref valid2 "valid"
595 * and \ref next2 "next" methods you can
596 * iterate through the elements of a component.
597 * See the example here: \ref first2 "first".
600 bool valid(ItemIt const &it) const {
605 * \brief Steps the iterator to the next component.
608 * Steps the iterator to the next component.
610 * With the \ref first2 "first", \ref valid2 "valid"
611 * and \ref next2 "next" methods you can
612 * iterate through the elements of a component.
613 * See the example here: \ref first2 "first".
616 ItemIt& next(ItemIt& it) const {
628 #endif //HUGO_UNION_FIND_H