lab_graphs
Gangnam-Style Graphs
dsets.h
1 /**********************************************************
2  * dsets.h -- part of mp7
3  *
4  * Allows us to create disjoint sets. We have up-trees stored as a vector of
5  * integers. It also uses path compression and union-by-size. Each place in the
6  * private vector represents a node. (Note that this means that the elements in
7  * our universe are indexed starting at 0). A nonnegative number is the index
8  * of the parent of the current node; a negative number in a root node is the
9  * negative of the set size.
10  *
11  * Authors: Kevin Cathey and G. Carl Evans
12  * Date: 22/04/2007
13  */
14 
15 #ifndef dsets_H
16 #define dsets_H
17 
18 #include <vector>
19 
21 {
22  public:
23  DisjointSets();
24 
25  /*
26  * This function creates n unconnected root nodes at the end of the
27  * private vector.
28  */
29  void addelements(int n);
30 
31  /*
32  * Finds the set given by the integer.
33  */
34  int find(int a);
35 
36  /*
37  * Unions two sets, a and b
38  */
39  void setunion(int a, int b);
40 
41  private:
42  /*
43  * This class is the node that is actual stored in the vector of nodes.
44  */
45  typedef struct _MPDisjointSetsNode
46  {
47  public:
48  /*
49  * Creates a node with no owner and size of 1
50  */
51  _MPDisjointSetsNode():owner(-1),size(1) {};
52 
53  /*
54  * States whether the receiver is bigger than the given node.
55  * Ties are returned false. Think about why (hint, > != >=).
56  */
57  bool isBiggerThanNode(_MPDisjointSetsNode &node)
58  {
59  return (size > node.size);
60  }
61 
62  /*
63  * Tells us if the receiver is a root node. A root node is
64  * determined by whether or not the owner is less than 0, or -1
65  * in our case.
66  */
67  bool isRoot()
68  {
69  return (owner < 0);
70  }
71 
72  /*
73  * Member Variables. Owner is the node the node belongs too. If
74  * it is -1, then we have a root. The size is the size of the
75  * node. The size really only applies to the root node, since
76  * that is where the size gets set by the DisjointSets class.
77  */
78  int owner;
79  int size;
80  } MPDisjointSetsNode;
81 
82  std::vector<MPDisjointSetsNode> _nodes;
83 };
84 
85 #endif
Definition: dsets.h:20