iRoCS Toolbox  1.1.0
DisjointSets.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Kun Liu, Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 #ifndef DISJOINTSETS_H_
24 #define DISJOINTSETS_H_
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.hh>
28 #endif
29 
30 // Disjoint Set Data Structure
31 // Author: Emil Stefanov
32 // Date: 03/28/06
33 // Implementaton is as described in http://en.wikipedia.org/wiki/Disjoint-set_data_structure
34 
35 #include <vector>
36 
38 {
39 public:
40 
41  // Create an empty DisjointSets data structure
42  DisjointSets();
43  // Create a DisjointSets data structure with a specified number of elements (with element id's from 0 to count-1)
44  DisjointSets(int count);
45  // Copy constructor
46  DisjointSets(const DisjointSets & s);
47  // Destructor
48  ~DisjointSets();
49 
50  // Find the set identifier that an element currently belongs to.
51  // Note: some internal data is modified for optimization even though this method is consant.
52  int
53  FindSet(int element) const;
54  // Combine two sets into one. All elements in those two sets will share the same set id that can be gotten using FindSet.
55  void
56  Union(int setId1, int setId2);
57  // Add a specified number of elements to the DisjointSets data structure. The element id's of the new elements are numbered
58  // consequitively starting with the first never-before-used elementId.
59  void
60  AddElements(int numToAdd);
61  // Returns the number of elements currently in the DisjointSets data structure.
62  int
63  NumElements() const;
64  // Returns the number of sets currently in the DisjointSets data structure.
65  int
66  NumSets() const;
67 
68  // to renumber the sets, necessary for connected component labelling
69  void
70  ReNum();
71 
72 private:
73 
74  // Internal Node data structure used for representing an element
75  struct Node
76  {
77  int rank; // This roughly represent the max height of the node in its subtree
78  int index; // The index of the element the node represents
79  Node* parent; // The parent node of the node
80  };
81 
82  int m_numElements; // the number of elements currently in the DisjointSets data structure.
83  int m_numSets; // the number of sets currently in the DisjointSets data structure.
84  std::vector<Node*> m_nodes; // the list of nodes representing the elements
85 };
86 
87 #endif /* DISJOINTSETS_H_ */
void Union(int setId1, int setId2)
int NumSets() const
int FindSet(int element) const
void AddElements(int numToAdd)
int NumElements() const