comparison Framework/Toolbox/DisjointDataSet.h @ 1005:7e861cfd142d

Added a union find class (on integers) called DisjointDataSet
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 20 Sep 2019 12:01:20 +0200
parents
children 58eed6bbcabb
comparison
equal deleted inserted replaced
1004:633186a2ed03 1005:7e861cfd142d
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21 #pragma once
22
23 #include <vector>
24
25 #include "../StoneException.h"
26
27 namespace OrthancStone
28 {
29 class DisjointDataSet
30 {
31 public:
32 DisjointDataSet(size_t itemCount)
33 : ranks_(itemCount)
34 , parents_(itemCount)
35 {
36 for (size_t index = 0; index < parents_.size(); index++)
37 {
38 SetParent(index,index);
39 ranks_[index] = 1;
40 }
41 }
42
43 size_t Find(size_t item)
44 {
45 /*
46 If parents_[i] == i, it means i is representative of a set.
47 Otherwise, we go up the tree...
48 */
49 if (GetParent(item) != item)
50 {
51 // if item is not a top item (representative of its set),
52 // we use path compression to improve future lookups
53 // see: https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Path_compression
54 SetParent(item, Find(parents_[item]));
55 }
56
57 // now that paths have been compressed, we are positively certain
58 // that item's parent is a set ("X is a set" means that X is the
59 // representative of a set)
60 return GetParent(item);
61 }
62
63 /*
64 This merge the two sets that contains itemA and itemB
65 */
66 void Union(size_t itemA, size_t itemB)
67 {
68 // Find current sets of x and y
69 size_t setA = Find(itemA);
70 size_t setB = Find(itemB);
71
72 // if setA == setB, it means they are already in the same set and
73 // do not need to be merged!
74 if (setA != setB)
75 {
76 // we need to merge the sets, which means that the trees representing
77 // the sets needs to be merged (there must be a single top parent to
78 // all the items originally belonging to setA and setB must be the same)
79
80 // since the algorithm speed is inversely proportional to the tree
81 // height (the rank), we need to combine trees in a way that
82 // minimizes this rank. See "Union by rank" at
83 // https://en.wikipedia.org/wiki/Disjoint-set_data_structure#by_rank
84 if (GetRank(setA) < GetRank(setB))
85 {
86 SetParent(setA, setB);
87 }
88 else if (GetRank(setA) > GetRank(setB))
89 {
90 SetParent(setB, setA);
91 }
92 else
93 {
94 SetParent(setB, setA);
95 BumpRank(setA);
96 // the trees had the same height but we attached the whole of setB
97 // under setA (under its parent), so the resulting tree is now
98 // 1 higher. setB is NOT representative of a set anymore.
99 }
100 }
101 }
102
103 private:
104 size_t GetRank(size_t i) const
105 {
106 ORTHANC_ASSERT(i < ranks_.size());
107 ORTHANC_ASSERT(ranks_.size() == parents_.size());
108 return ranks_[i];
109 }
110
111 size_t GetParent(size_t i) const
112 {
113 ORTHANC_ASSERT(i < parents_.size());
114 ORTHANC_ASSERT(ranks_.size() == parents_.size());
115 return parents_[i];
116 }
117
118 void SetParent(size_t i, size_t parent)
119 {
120 ORTHANC_ASSERT(i < parents_.size());
121 ORTHANC_ASSERT(ranks_.size() == parents_.size());
122 parents_[i] = parent;
123 }
124
125 void BumpRank(size_t i)
126 {
127 ORTHANC_ASSERT(i < ranks_.size());
128 ORTHANC_ASSERT(ranks_.size() == parents_.size());
129 ranks_[i] = ranks_[i] + 1u;
130 }
131
132 /*
133 This vector contains the direct parent of each item
134 */
135 std::vector<size_t> parents_;
136
137 /*
138 This vector contains the tree height of each set. The values in the
139 vector for non-representative items is UNDEFINED!
140 */
141 std::vector<size_t> ranks_;
142 };
143
144 }