Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnDisjointSets.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnDisjointSets.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 
17 // drwnDisjointSets ---------------------------------------------------------
26 
28  protected:
29  typedef struct _node_t {
30  int rank; // depth of node
31  int size; // size of set (for root nodes)
32  int index; // index of the element represented by this node
33  _node_t *parent; // parent of this node
34  } node_t;
35 
36  int _nElements;
37  int _nSets;
38  std::vector<node_t> _nodes;
39 
40  public:
42  drwnDisjointSets(unsigned int count = 0);
47 
49  inline int size() const { return _nElements; }
51  int size(int setId) const;
53  inline int sets() const { return _nSets; }
55  std::set<int> getMembers(int setId);
57  void add(int count);
59  int find(int elementId);
61  int join(int setId1, int setId2);
63  std::vector<int> getSetIds() const;
64 
67 };
std::vector< int > getSetIds() const
return a list of valid set ids
Definition: drwnDisjointSets.cpp:151
drwnDisjointSets(unsigned int count=0)
construct a disjoint dataset with count nodes
Definition: drwnDisjointSets.cpp:26
drwnDisjointSets & operator=(const drwnDisjointSets &dset)
assignment operator
Definition: drwnDisjointSets.cpp:165
int join(int setId1, int setId2)
join two sets and return the joined set id (either setId1 or setId2)
Definition: drwnDisjointSets.cpp:120
int find(int elementId)
find which set a particular node belongs to
Definition: drwnDisjointSets.cpp:87
void add(int count)
add count (disconnected) nodes
Definition: drwnDisjointSets.cpp:73
Implements a forest of disjoint sets abstract data type.
Definition: drwnDisjointSets.h:27
std::vector< node_t > _nodes
list of nodes
Definition: drwnDisjointSets.h:38
Definition: drwnDisjointSets.h:29
int sets() const
return the number of disjoint sets
Definition: drwnDisjointSets.h:53
int _nSets
number of sets in the forest
Definition: drwnDisjointSets.h:37
~drwnDisjointSets()
destructor
Definition: drwnDisjointSets.cpp:49
int _nElements
number of elements in the forest
Definition: drwnDisjointSets.h:36
int size() const
return the total number of nodes in the disjoint set
Definition: drwnDisjointSets.h:49
std::set< int > getMembers(int setId)
get the ids of nodes in set with id setId
Definition: drwnDisjointSets.cpp:62