Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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 
135  {
136  delete[] this->m_parents;
137  delete[] this->m_parameters;
138  delete[] this->m_siblings;
139  }
140 
142 
147  int find(int set)
148  {
149  OGDF_ASSERT(set >= 0);
150  OGDF_ASSERT(set < m_numberOfElements);
152  }
153 
155 
160  int getRepresentative(int set)
161  {
162  OGDF_ASSERT(set >= 0);
163  OGDF_ASSERT(set < m_numberOfElements);
164  while (set!=m_parents[set]) set=m_parents[set];
165  return set;
166  }
167 
169 
172  int makeSet()
173  {
174  if (this->m_numberOfElements==this->m_maxNumberOfElements)
175  {
176  int *parents = this->m_parents;
177  this->m_parents = new int[this->m_maxNumberOfElements * 2];
178  memcpy(this->m_parents,parents,sizeof(int)*this->m_maxNumberOfElements);
179  delete[] parents;
180 
181  if (this->m_parameters != nullptr)
182  {
183  int *parameters = this->m_parameters;
184  this->m_parameters = new int[this->m_maxNumberOfElements*2];
185  memcpy(this->m_parameters,parameters,sizeof(int)*this->m_maxNumberOfElements);
186  delete[] parameters;
187  }
188 
189  if (this->m_siblings != nullptr)
190  {
191  int *siblings = this->m_siblings;
192  this->m_siblings = new int[this->m_maxNumberOfElements*2];
193  memcpy(this->m_siblings,siblings,sizeof(int)*this->m_maxNumberOfElements);
194  delete[] siblings;
195  }
196  this->m_maxNumberOfElements*=2;
197  }
198  this->m_numberOfSets++;
199  int id = this->m_numberOfElements++;
200  this->m_parents[id]=id;
201  //Initialize size/ rank/ sibling.
202  if (linkOption == LinkOptions::Size) this->m_parameters[id]=1;
203  else if (linkOption == LinkOptions::Rank) this->m_parameters[id]=0;
204  if (compressionOption == CompressionOptions::Collapsing) this->m_siblings[id] = -1;
205  return id;
206  }
207 
209 
213  int link(int set1, int set2)
214  {
215  OGDF_ASSERT(set1 == getRepresentative(set1));
216  OGDF_ASSERT(set2 == getRepresentative(set2));
217  if (set1==set2) return -1;
218  this->m_numberOfSets--;
219  return linkPure(set1, set2);
220  }
221 
223 
226  bool quickUnion(int set1, int set2)
227  {
228  if (set1==set2) return false;
230  m_numberOfSets -= result;
231  return result;
232  }
233 
235  int getNumberOfSets() { return m_numberOfSets; }
236 
238  int getNumberOfElements() {return m_numberOfElements; }
239 
240 private:
242 
246  int linkPure(int set1, int set2)
247  {
248  int superset = link(disjoint_sets::LinkOption<linkOption>(), set1, set2);
249  //Collapse subset tree.
250  if (compressionOption == CompressionOptions::Collapsing)
251  {
252  int subset = set1 == superset ? set2 : set1;
253  int id = subset;
254  while (this->m_siblings[id] != -1)
255  {
256  id = this->m_siblings[id];
257  this->m_parents[id]=superset;
258  }
259  this->m_siblings[id] = this->m_siblings[superset];
260  this->m_siblings[superset] = subset;
261  }
262  return superset;
263  }
264 };
265 
266 
267 //find
268 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
270 {
271  int parent = m_parents[set];
272  if (set==parent)
273  {
274  return set;
275  }
276  else
277  {
279  m_parents[set]=parent;
280  return parent;
281  }
282 }
283 
284 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
286 {
287  while (set!=m_parents[set])
288  {
289  int parent = m_parents[set];
290  int grandParent = m_parents[parent];
291  m_parents[set]=grandParent;
292  set = grandParent;
293  }
294  return set;
295 }
296 
297 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
299 {
300  int parent = m_parents[set];
301  int grandParent = m_parents[parent];
302  while (parent!=grandParent)
303  {
304  m_parents[set]=grandParent;
305  set = parent;
306  parent = grandParent;
307  grandParent = m_parents[grandParent];
308  }
309  return parent;
310 }
311 
312 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
314 {
315  int root = set;
316  set = m_parents[root];
317 
318  while (set!=m_parents[set])
319  {
320  int parent = m_parents[set];
321  m_parents[set] = root;
322  set = parent;
323  }
324  m_parents[root] = set;
325  return set;
326 }
327 
328 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
330 {
331  while (set!=m_parents[set]) set=m_parents[set];
332  return set;
333 }
334 
335 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
337 {
338  return m_parents[set];
339 }
340 
341 
342 //quickUnion
343 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
347  int set1, int set2)
348 {
349 #ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
350  if (m_parents[set1]==m_parents[set2]) return false;
351 #endif
352  set1 = find(set1);
353  set2 = find(set2);
354  if (set1 != set2)
355  {
356  linkPure(set1,set2);
357  return true;
358  }
359  return false;
360 }
361 
362 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
366  int set1, int set2)
367 {
368 #ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
369  if (m_parents[set1]==m_parents[set2]) return false;
370 #endif
371  int root = set2;
372  int set = set2;
373  int parent = m_parents[set];
374  m_parents[set]=root;
375  while (set != parent)
376  {
377  if (parent == set1)
378  {
379  m_parents[root]=set1;
380  return false;
381  }
382  set = parent;
383  parent = m_parents[set];
384  m_parents[set]=root;
385  }
386 
387  set = set1;
388  parent = m_parents[set];
389  while (true)
390  {
391  if (parent == root) return false;
392  m_parents[set] = root;
393  if (parent == set){
394  return true;
395  }
396  set = parent;
397  parent = m_parents[set];
398  }
399 }
400 
401 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
405  int set1, int set2)
406 {
407  int r_x = set1; int r_y = set2;
408  int p_r_x =m_parents[r_x];
409  int p_r_y =m_parents[r_y];
410  while (p_r_x != p_r_y)
411  {
412  if (p_r_x < p_r_y)
413  {
414  if (r_x == p_r_x)
415  {
416  m_parents[r_x]=p_r_y;
417  return true;
418  }
419  m_parents[r_x]=p_r_y;
420  r_x = p_r_x;
421  p_r_x = m_parents[r_x];
422  }
423  else
424  {
425  if (r_y == p_r_y)
426  {
427  m_parents[r_y]=p_r_x;
428  return true;
429  }
430  m_parents[r_y]=p_r_x;
431  r_y = p_r_y;
432  p_r_y = m_parents[r_y];
433  }
434  }
435  return false;
436 }
437 
438 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
442  int set1, int set2)
443 {
444 #ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
445  if (m_parents[set1]==m_parents[set2]) return false;
446 #endif
447  int set = set1;
448 
449  if (set1 < set2)
450  {
451  set = set2;
452  set2 = set1;
453  set1 = set;
454  }
455 
457  set = m_parents[set];
458  int parent = m_parents[set];
459  int grandParent = m_parents[parent];
460  while (parent!=grandParent)
461  {
462  m_parents[set]=grandParent;
463  set = parent;
464  parent = grandParent;
465  grandParent = m_parents[grandParent];
466  }
467  m_parents[set1]=parent;
468  int root = parent;
469 
471  set = set2;
472  parent = m_parents[set];
473  while (true)
474  {
475  if (parent < root)
476  {
477  m_parents[set]=root;
478  if (set == parent){
479  return true;
480  }
481  set=parent;
482  parent = m_parents[set];
483  }
484  else if (parent > root)
485  {
486  m_parents[root]=parent;
487  m_parents[set1]=parent;
488  m_parents[set2]=parent;
489  return true;
490  }
491  else return false;
492  }
493 }
494 
495 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
499  int set1, int set2)
500 {
501  int r_x = set1; int r_y = set2;
502  int p_r_x = m_parents[r_x];
503  int p_r_y = m_parents[r_y];
504  while (p_r_x != p_r_y)
505  {
506  if (m_parameters[p_r_x]<=m_parameters[p_r_y])
507  {
508  if (r_x==p_r_x)
509  {
510  if (m_parameters[p_r_x] == m_parameters[p_r_y]
511  && p_r_y == m_parents[p_r_y])
512  {
513  m_parameters[p_r_y]++;
514  }
515  m_parents[r_x]=m_parents[p_r_y];
516  return true;
517  }
518  m_parents[r_x]=p_r_y;
519  r_x = p_r_x;
520  p_r_x = m_parents[r_x];
521  }
522  else
523  {
524  if (r_y==p_r_y)
525  {
526  m_parents[r_y]=m_parents[p_r_x];
527  return true;
528  }
529  m_parents[r_y]=p_r_x;
530  r_y = p_r_y;
531  p_r_y = m_parents[r_y];
532  }
533  }
534  return false;
535 }
536 
537 
538 //link
539 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
541 {
542  if (set1<set2)
543  {
544  m_parents[set1]=set2;
545  return set2;
546  }
547  else
548  {
549  m_parents[set2]=set1;
550  return set1;
551  }
552 }
553 
554 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
556 {
557  int parameter1 = m_parameters[set1];
558  int parameter2 = m_parameters[set2];
559 
560  if (parameter1<parameter2)
561  {
562  m_parents[set1]=set2;
563  return set2;
564  }
565  else if (parameter1>parameter2)
566  {
567  m_parents[set2]=set1;
568  return set1;
569  }
570  else
571  {
572  m_parents[set1]=set2;
573  m_parameters[set2]++;
574  return set2;
575  }
576 }
577 
578 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
580 {
581  int parameter1 = m_parameters[set1];
582  int parameter2 = m_parameters[set2];
583 
584  if (parameter1<parameter2)
585  {
586  m_parents[set1]=set2;
587  m_parameters[set2]+=parameter1;
588  return set2;
589  }
590  else
591  {
592  m_parents[set2]=set1;
593  m_parameters[set1]+=parameter2;
594  return set1;
595  }
596 }
597 
598 template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
600 {
601  m_parents[set1]=set2;
602  return set2;
603 }
604 
605 } // end namespace ogdf
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:235
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:147
int getNumberOfElements()
Returns the current number of elements.
Definition: DisjointSets.h:238
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:226
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:213
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:403
int getRepresentative(int set)
Returns the id of the largest superset of set.
Definition: DisjointSets.h:160
int * m_parents
Maps set id to parent set id.
Definition: DisjointSets.h:94
int linkPure(int set1, int set2)
Unions set1 and set2 w/o decreasing the numberOfSets.
Definition: DisjointSets.h:246
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:269
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:172
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:599
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) ...