vbl_disjoint_sets.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_disjoint_sets.h
2 #ifndef vbl_disjoint_sets_h_
3 #define vbl_disjoint_sets_h_
4 //:
5 // \file
6 // \brief implements a disjoint set (union, find)
7 // \author Emil Stefanov
8 //
9 // \verbatim
10 // Adapted to VXL by J. Mundy Feb. 14, 2013
11 // \endverbatim
12 //-----------------------------------------------------------------------------
13 
14 #include <vector>
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 #include "vbl_array_1d.h"
19 // Disjoint Set Data Structure
20 // Author: Emil Stefanov
21 // Date: 03/28/06
22 // Implementation is as described in http://en.wikipedia.org/wiki/Disjoint-set_data_structure
23 // Copyrighted according to the MIT license
24 // http://opensource.org/licenses/mit-license.html
25 // 12/17/2016 - JLM
26 // changed node storage to vbl_array_1d so that delete is fast
28 {
29  public:
30 
31  // Create an empty vbl_disjoint_sets data structure
33  //: Create a vbl_disjoint_sets data structure with a specified number of elements (with element id's from 0 to count-1)
34  vbl_disjoint_sets(int count);
35  //:Copy constructor
37  //: Destructor
39 
40  //: Find the set identifier that an element currently belongs to.
41  int find_set(int element) const;
42 
43  //: Combine two sets into one.
44  // All elements in those two sets will share the same set id that
45  // can be retrieved using find_set.
46  void set_union(int set_id1, int set_id2);
47 
48  // Add a specified number of elements to the data structure.
49  // The element id's of the new elements are numbered
50  // consequitively starting with the first never-before-used element_id.
51  void add_elements(int num_to_add);
52 
53  // Returns the number of elements currently in the data structure.
54  int num_elements() const;
55 
56  //: Returns the number of elements in set specified by set_id
57  int size(int set_id) const;
58 
59  //: Returns the number of sets
60  int num_sets() const;
61 
62  private:
63 
64  // Internal node data structure used for representing an element
65  struct node
66  {
67  node():rank(0), index(0), parent(nullptr), size(1){}
68  //: represents the approximate max height of the node in its subtree
69  int rank;
70  int index; // The index of the element the node represents
71  node* parent; // The parent node of the node
72  int size; // the number of elements in the set
73  };
74  int num_elements_; // the number of elements
75  int num_sets_; // the number of sets
76  // std::vector<node*> nodes_; // the list of nodes representing the elements
77  vbl_array_1d<node> nodes_; // changed to vbl_array since deleting was very slow
78 };
79 
80 #endif // vbl_disjoint_sets_h_
int rank
represents the approximate max height of the node in its subtree.
void add_elements(int num_to_add)
~vbl_disjoint_sets()
Destructor.
vbl_array_1d< node > nodes_
A simple container.
Definition: vbl_array_1d.h:28
A simple container.
void set_union(int set_id1, int set_id2)
Combine two sets into one.
int find_set(int element) const
Find the set identifier that an element currently belongs to.
int num_sets() const
Returns the number of sets.
int size(int set_id) const
Returns the number of elements in set specified by set_id.