Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

DisjointSets.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <cstring>
35 #include <ogdf/basic/exceptions.h>
36 
37 namespace ogdf {
38 
39 #define OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
40 
42 enum class LinkOptions {
43  Naive = 0,
44  Index = 1,
45  Size = 2,
46  Rank = 3
47 };
48 
50 enum class CompressionOptions {
51  PathCompression = 0,
52  PathSplitting = 1,
53  PathHalving = 2,
54  Type1Reversal = 4,
55  Collapsing = 5,
56  Disabled = 6
57 };
58 
60 enum class InterleavingOptions {
61  Disabled = 0,
62  Rem = 1,
63  Tarjan = 2,
64  Type0Reversal = 3,
66 };
67 
68 namespace disjoint_sets {
69 
70 struct AnyOption {};
71 template<LinkOptions linkOption> struct LinkOption : AnyOption {};
72 template<CompressionOptions compressionOption> struct CompressionOption : AnyOption {};
73 template<InterleavingOptions interleavingOption> struct InterleavingOption : AnyOption {};
74 
75 }
76 
78 template <LinkOptions linkOption = LinkOptions::Index,
82 {
83 static_assert(interleavingOption != InterleavingOptions::Rem || linkOption == LinkOptions::Index, "Rem's Algorithm requires linking by index.");
84 static_assert(interleavingOption != InterleavingOptions::Tarjan || linkOption == LinkOptions::Rank, "Tarjan and van Leeuwen's Algorithm requires linking by rank.");
85 static_assert(interleavingOption != InterleavingOptions::Type0Reversal || linkOption == LinkOptions::Naive, "Interleaved Reversal Type 0 requires naïve linking.");
86 static_assert(interleavingOption != InterleavingOptions::SplittingCompression || linkOption == LinkOptions::Index, "Interleaved Path Splitting Path Compression requires linking by index.");
87 private:
88  int m_numberOfSets;
91 
92  // Arrays parents, elements, parameters, siblings map a set id to its properties.
93 
94  int *m_parents;
95  int *m_parameters;
96  int *m_siblings;
97 
98  //find
105 
106  //link
107  int link(disjoint_sets::LinkOption<LinkOptions::Naive>,int set1,int set2);
108  int link(disjoint_sets::LinkOption<LinkOptions::Index>,int set1,int set2);
109  int link(disjoint_sets::LinkOption<LinkOptions::Size>,int set1,int set2);
110  int link(disjoint_sets::LinkOption<LinkOptions::Rank>,int set1,int set2);
111 
112  //quickUnion
118 
119 public:
121 
124  explicit DisjointSets(int maxNumberOfElements = (1<<15) )
125  {
126  this->m_numberOfSets=0;
127  this->m_numberOfElements=0;
128  this->m_maxNumberOfElements = maxNumberOfElements;
129  this->m_parents = new int[this->m_maxNumberOfElements];
130  this->m_parameters = (linkOption==LinkOptions::Rank || linkOption==LinkOptions::Size) ? new int[this->m_maxNumberOfElements] : nullptr;
131  this->m_siblings = (compressionOption==CompressionOptions::Collapsing) ? new int[this->m_maxNumberOfElements] : nullptr;
132  }
133 
134  DisjointSets(const DisjointSets&) = delete;
135 
136  DisjointSets& operator=(const DisjointSets&) = delete;
137 
139  {
140  delete[] this->m_parents;
141  delete[] this->m_parameters;
142  delete[] this->m_siblings;
143  }
144 
146 
151  int find(int set)
152  {
153  OGDF_ASSERT(set >= 0);
154  OGDF_ASSERT(set < m_numberOfElements);
156  }
157 
159 
164  int getRepresentative(int set) const
165  {
166  OGDF_ASSERT(set >= 0);
167  OGDF_ASSERT(set < m_numberOfElements);
168  while (set!=m_parents[set]) set=m_parents[set];
169  return set;
170  }
171 
173 
176  int makeSet()
177  {
178  if (this->m_numberOfElements==this->m_maxNumberOfElements)
179  {
180  int *parents = this->m_parents;
181  this->m_parents = new int[this->m_maxNumberOfElements * 2];
182  memcpy(this->m_parents,parents,sizeof(int)*this->m_maxNumberOfElements);
183  delete[] parents;
184 
185  if (this->m_parameters != nullptr)
186  {
187  int *parameters = this->m_parameters;
188  this->m_parameters = new int[this->m_maxNumberOfElements*2];
189  memcpy(this->m_parameters,parameters,sizeof(int)*this->m_maxNumberOfElements);
190  delete[] parameters;
191  }
192 
193  if (this->m_siblings != nullptr)
194  {
195  int *siblings = this->m_siblings;
196  this->m_siblings = new int[this->m_maxNumberOfElements*2];
197  memcpy(this->m_siblings,siblings,sizeof(int)*this->m_maxNumberOfElements);
198  delete[] siblings;
199  }
200  this->m_maxNumberOfElements*=2;
201  }
202  this->m_numberOfSets++;
203  int id = this->m_numberOfElements++;
204  this->m_parents[id]=id;
205  //Initialize size/ rank/ sibling.
206  if (linkOption == LinkOptions::Size) this->m_parameters[id]=1;
207  else if (linkOption == LinkOptions::Rank) this->m_parameters[id]=0;
208  if (compressionOption == CompressionOptions::Collapsing) this->m_siblings[id] = -1;
209  return id;
210  }
211 
213 
217  int link(int set1, int set2)
218  {
219  OGDF_ASSERT(set1 == getRepresentative(set1));
220  OGDF_ASSERT(set2 == getRepresentative(set2));
221  if (set1==set2) return -1;
222  this->m_numberOfSets--;
223  return linkPure(set1, set2);
224  }
225 
227 
230  bool quickUnion(int set1, int set2)
231  {
232  if (set1==set2) return false;
234  m_numberOfSets -= result;
235  return result;
236  }
237 
239  int getNumberOfSets() { return m_numberOfSets; }
240 
242  int getNumberOfElements() {return m_numberOfElements; }
243 
244 private:
246 
250  int linkPure(int set1, int set2)
251  {
252  int superset = link(disjoint_sets::LinkOption<linkOption>(), set1, set2);
253  //Collapse subset tree.
254  if (compressionOption == CompressionOptions::Collapsing)
255  {
256  int subset = set1 == superset ? set2 : set1;
257  int id = subset;
258  while (this->m_siblings[id] != -1)
259  {
260  id = this->m_siblings[id];
261  this->m_parents[id]=superset;
262  }
263  this->m_siblings[id] = this->m_siblings[superset];
264  this->m_siblings[superset] = subset;
265  }
266  return superset;
267  }
268 };
269 
270 
271 //find
272 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
274 {
275  int parent = m_parents[set];
276  if (set==parent)
277  {
278  return set;
279  }
280  else
281  {
283  m_parents[set]=parent;
284  return parent;
285  }
286 }
287 
288 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
290 {
291  while (set!=m_parents[set])
292  {
293  int parent = m_parents[set];
294  int grandParent = m_parents[parent];
295  m_parents[set]=grandParent;
296  set = grandParent;
297  }
298  return set;
299 }
300 
301 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
303 {
304  int parent = m_parents[set];
305  int grandParent = m_parents[parent];
306  while (parent!=grandParent)
307  {
308  m_parents[set]=grandParent;
309  set = parent;
310  parent = grandParent;
311  grandParent = m_parents[grandParent];
312  }
313  return parent;
314 }
315 
316 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
318 {
319  int root = set;
320  set = m_parents[root];
321 
322  while (set!=m_parents[set])
323  {
324  int parent = m_parents[set];
325  m_parents[set] = root;
326  set = parent;
327  }
328  m_parents[root] = set;
329  return set;
330 }
331 
332 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
334 {
335  while (set!=m_parents[set]) set=m_parents[set];
336  return set;
337 }
338 
339 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
341 {
342  return m_parents[set];
343 }
344 
345 
346 //quickUnion
347 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
351  int set1, int set2)
352 {
353 #ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
354  if (m_parents[set1]==m_parents[set2]) return false;
355 #endif
356  set1 = find(set1);
357  set2 = find(set2);
358  if (set1 != set2)
359  {
360  linkPure(set1,set2);
361  return true;
362  }
363  return false;
364 }
365 
366 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
370  int set1, int set2)
371 {
372 #ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
373  if (m_parents[set1]==m_parents[set2]) return false;
374 #endif
375  int root = set2;
376  int set = set2;
377  int parent = m_parents[set];
378  m_parents[set]=root;
379  while (set != parent)
380  {
381  if (parent == set1)
382  {
383  m_parents[root]=set1;
384  return false;
385  }
386  set = parent;
387  parent = m_parents[set];
388  m_parents[set]=root;
389  }
390 
391  set = set1;
392  parent = m_parents[set];
393  while (true)
394  {
395  if (parent == root) return false;
396  m_parents[set] = root;
397  if (parent == set){
398  return true;
399  }
400  set = parent;
401  parent = m_parents[set];
402  }
403 }
404 
405 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
409  int set1, int set2)
410 {
411  int r_x = set1; int r_y = set2;
412  int p_r_x =m_parents[r_x];
413  int p_r_y =m_parents[r_y];
414  while (p_r_x != p_r_y)
415  {
416  if (p_r_x < p_r_y)
417  {
418  if (r_x == p_r_x)
419  {
420  m_parents[r_x]=p_r_y;
421  return true;
422  }
423  m_parents[r_x]=p_r_y;
424  r_x = p_r_x;
425  p_r_x = m_parents[r_x];
426  }
427  else
428  {
429  if (r_y == p_r_y)
430  {
431  m_parents[r_y]=p_r_x;
432  return true;
433  }
434  m_parents[r_y]=p_r_x;
435  r_y = p_r_y;
436  p_r_y = m_parents[r_y];
437  }
438  }
439  return false;
440 }
441 
442 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
446  int set1, int set2)
447 {
448 #ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
449  if (m_parents[set1]==m_parents[set2]) return false;
450 #endif
451  int set = set1;
452 
453  if (set1 < set2)
454  {
455  set = set2;
456  set2 = set1;
457  set1 = set;
458  }
459 
461  set = m_parents[set];
462  int parent = m_parents[set];
463  int grandParent = m_parents[parent];
464  while (parent!=grandParent)
465  {
466  m_parents[set]=grandParent;
467  set = parent;
468  parent = grandParent;
469  grandParent = m_parents[grandParent];
470  }
471  m_parents[set1]=parent;
472  int root = parent;
473 
475  set = set2;
476  parent = m_parents[set];
477  while (true)
478  {
479  if (parent < root)
480  {
481  m_parents[set]=root;
482  if (set == parent){
483  return true;
484  }
485  set=parent;
486  parent = m_parents[set];
487  }
488  else if (parent > root)
489  {
490  m_parents[root]=parent;
491  m_parents[set1]=parent;
492  m_parents[set2]=parent;
493  return true;
494  }
495  else return false;
496  }
497 }
498 
499 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
503  int set1, int set2)
504 {
505  int r_x = set1; int r_y = set2;
506  int p_r_x = m_parents[r_x];
507  int p_r_y = m_parents[r_y];
508  while (p_r_x != p_r_y)
509  {
510  if (m_parameters[p_r_x]<=m_parameters[p_r_y])
511  {
512  if (r_x==p_r_x)
513  {
514  if (m_parameters[p_r_x] == m_parameters[p_r_y]
515  && p_r_y == m_parents[p_r_y])
516  {
517  m_parameters[p_r_y]++;
518  }
519  m_parents[r_x]=m_parents[p_r_y];
520  return true;
521  }
522  m_parents[r_x]=p_r_y;
523  r_x = p_r_x;
524  p_r_x = m_parents[r_x];
525  }
526  else
527  {
528  if (r_y==p_r_y)
529  {
530  m_parents[r_y]=m_parents[p_r_x];
531  return true;
532  }
533  m_parents[r_y]=p_r_x;
534  r_y = p_r_y;
535  p_r_y = m_parents[r_y];
536  }
537  }
538  return false;
539 }
540 
541 
542 //link
543 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
545 {
546  if (set1<set2)
547  {
548  m_parents[set1]=set2;
549  return set2;
550  }
551  else
552  {
553  m_parents[set2]=set1;
554  return set1;
555  }
556 }
557 
558 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
560 {
561  int parameter1 = m_parameters[set1];
562  int parameter2 = m_parameters[set2];
563 
564  if (parameter1<parameter2)
565  {
566  m_parents[set1]=set2;
567  return set2;
568  }
569  else if (parameter1>parameter2)
570  {
571  m_parents[set2]=set1;
572  return set1;
573  }
574  else
575  {
576  m_parents[set1]=set2;
577  m_parameters[set2]++;
578  return set2;
579  }
580 }
581 
582 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
584 {
585  int parameter1 = m_parameters[set1];
586  int parameter2 = m_parameters[set2];
587 
588  if (parameter1<parameter2)
589  {
590  m_parents[set1]=set2;
591  m_parameters[set2]+=parameter1;
592  return set2;
593  }
594  else
595  {
596  m_parents[set2]=set1;
597  m_parameters[set1]+=parameter2;
598  return set1;
599  }
600 }
601 
602 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
604 {
605  m_parents[set1]=set2;
606  return set2;
607 }
608 
609 }
int * m_siblings
Maps set id to sibling set id.
Definition: DisjointSets.h:96
CompressionOptions
Defines options for compression search paths.
Definition: DisjointSets.h:50
int getNumberOfSets()
Returns the current number of disjoint sets.
Definition: DisjointSets.h:239
InterleavingOptions
Defines options for interleaving find/link operations in quickUnion.
Definition: DisjointSets.h:60
int find(int set)
Returns the id of the largest superset of set and compresses the path according to CompressionOptions...
Definition: DisjointSets.h:151
int getNumberOfElements()
Returns the current number of elements.
Definition: DisjointSets.h:242
Interleaved Reversal of Type 0 (only compatible with LinkOptions::Naive)
bool quickUnion(int set1, int set2)
Unions the maximal disjoint sets containing set1 and set2.
Definition: DisjointSets.h:230
DisjointSets(int maxNumberOfElements=(1<< 15))
Creates an empty DisjointSets structure.
Definition: DisjointSets.h:124
LinkOptions
Defines options for linking two sets.
Definition: DisjointSets.h:42
int link(int set1, int set2)
Unions set1 and set2.
Definition: DisjointSets.h:217
Rem&#39;s Algorithm (only compatible with LinkOptions::Index)
bool quickUnion(disjoint_sets::LinkOption< LinkOptions::Index >, disjoint_sets::InterleavingOption< InterleavingOptions::Rem >, int set1, int set2)
Definition: DisjointSets.h:407
int * m_parents
Maps set id to parent set id.
Definition: DisjointSets.h:94
int getRepresentative(int set) const
Returns the id of the largest superset of set.
Definition: DisjointSets.h:164
int linkPure(int set1, int set2)
Unions set1 and set2 w/o decreasing the numberOfSets.
Definition: DisjointSets.h:250
Tarjan&#39;s and van Leeuwen&#39;s Algorithm (only compatible with LinkOptions::Rank)
int m_numberOfSets
Current number of disjoint sets.
Definition: DisjointSets.h:83
int find(disjoint_sets::CompressionOption< CompressionOptions::PathCompression >, int set)
Definition: DisjointSets.h:273
Definition of exception classes.
A Union/Find data structure for maintaining disjoint sets.
Definition: DisjointSets.h:81
int m_numberOfElements
Current number of elements.
Definition: DisjointSets.h:89
int * m_parameters
Maps set id to rank/size.
Definition: DisjointSets.h:95
int m_maxNumberOfElements
Maximum number of elements (array size) adjusted dynamically.
Definition: DisjointSets.h:90
Link by index (default)
int makeSet()
Initializes a singleton set.
Definition: DisjointSets.h:176
static bool find(V &ts, const T &t)
Definition: Alg.h:47
int link(disjoint_sets::LinkOption< LinkOptions::Naive >, int set1, int set2)
Definition: DisjointSets.h:603
The namespace for all OGDF objects.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
Interleaved Path Splitting Path Compression (only compatible with LinkOptions::Index) ...