1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
/*
* $Revision: 4000 $
*
* last checkin:
* $Author: beyer $
* $Date: 2014-03-28 20:18:18 +0100 (Fri, 28 Mar 2014) $
***************************************************************/
/** \file
* \brief Implementation of disjoint sets data structures (union-find functionality).
*
* \author Andrej Dudenhefner
*
* \par License:
* This file is part of the Open Graph Drawing Framework (OGDF).
*
* \par
* Copyright (C)<br>
* See README.txt in the root directory of the OGDF installation for details.
*
* \par
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* Version 2 or 3 as published by the Free Software Foundation;
* see the file LICENSE.txt included in the packaging of this file
* for details.
*
* \par
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* \par
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* \see http://www.gnu.org/copyleft/gpl.html
***************************************************************/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef OGDF_DISJOINT_SETS_H
#define OGDF_DISJOINT_SETS_H
#include <ogdf/basic/basic.h>
namespace ogdf {
#define OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
struct AnyOption {};
/**
* Defines options for linking two sets.
* NL = Naive Link, LI = Link by Index (default), LS = Link by Size, LR = Link by Rank
*/
enum LinkOptions { NL=0, LI=1, LS=2, LR=3 };
template<LinkOptions linkOption> struct LinkOption : AnyOption {};
extern const char *linkOptionNames[];
/**
* Defines options for compression search paths.
* PC = Path Compression, PS = Path Splitting (default), PH = Path Halving, R1 = Reversal of type 1, CO = Collapsing, NF = No Compression
*/
enum CompressionOptions { PC=0, PS=1, PH=2, R1=4, CO=5, NF=6 };
template<CompressionOptions compressionOption> struct CompressionOption : AnyOption {};
extern const char *compressionOptionNames[];
/*
* Defines options for interleaving find/link operations in quickUnion.
* NI = No Interleaving
* Rem = Rem's Algorithm (only compatible with linkOption = LI) (default)
* TvL = Tarjan's and van Leeuwen's Algorithm (only compatible with linkOption = LR)
* IR0 = Interleaved Reversal of Type 0 (only compatible with linkOption = NF)
* IPSPC = Interleaved Path Splitting Path Compression (only compatible with linkOption = LI)
*/
enum InterleavingOptions { NI=0, Rem=1, TvL=2, IR0=3, IPSPC=4 };
template<InterleavingOptions interleavingOption> struct InterleavingOption : AnyOption {};
extern const char *interleavingOptionNames[];
//! A Union/Find data structure for maintaining disjoint sets.
template <LinkOptions linkOption = LI, CompressionOptions compressionOption = PS, InterleavingOptions interleavingOption = NI>
class DisjointSets
{
private:
int numberOfSets; //!< Current number of disjoint sets.
int numberOfElements; //!< Current number of elements.
int maxNumberOfElements; //!< Maximum number of elements (array size) adjusted dynamically.
// Arrays parents, elements, parameters, siblings map a set id to its properties.
int *parents; //!< Maps set id to parent set id.
int *parameters; //!< Maps set id to rank/size.
int *siblings; //!< Maps set id to sibling set id.
//find
int find(CompressionOption<PC>,int set);
int find(CompressionOption<PS>,int set);
int find(CompressionOption<PH>,int set);
int find(CompressionOption<R1>,int set);
int find(CompressionOption<CO>,int set);
int find(CompressionOption<NF>,int set);
//link
int link(LinkOption<NL>,int set1,int set2);
int link(LinkOption<LI>,int set1,int set2);
int link(LinkOption<LS>,int set1,int set2);
int link(LinkOption<LR>,int set1,int set2);
//quickUnion
bool quickUnion(LinkOption<LI>,InterleavingOption<Rem>,int set1,int set2);
bool quickUnion(LinkOption<LI>,InterleavingOption<IPSPC>,int set1,int set2);
bool quickUnion(LinkOption<LR>,InterleavingOption<TvL>,int set1,int set2);
bool quickUnion(AnyOption,InterleavingOption<NI>,int set1,int set2);
bool quickUnion(LinkOption<NL>,InterleavingOption<IR0>,int set1,int set2);
public:
//! Creates an empty DisjointSets structure.
/**
* \param maxNumberOfElements Expected number of Elements.
*/
DisjointSets(int maxNumberOfElements = (1<<15) )
{
this->numberOfSets=0;
this->numberOfElements=0;
this->maxNumberOfElements = maxNumberOfElements;
this->parents = new int[this->maxNumberOfElements];
this->parameters = (linkOption==LR || linkOption==LS) ? new int[this->maxNumberOfElements] : 0;
this->siblings = (compressionOption==CO) ? new int[this->maxNumberOfElements] : 0;
}
~DisjointSets()
{
delete [] this->parents;
if (this->parameters != 0) delete [] this->parameters;
if (this->siblings != 0) delete [] this->siblings;
}
//! Returns the id of the largest superset of \a set and compresses the path according to \a compressionOption.
/**
* \param set Set.
* \return Superset id
* \pre \a set is a non negative properly initialized id.
*/
int find(int set)
{
return find(CompressionOption<compressionOption>(), set);
}
//! Returns the id of the largest superset of \a set.
/**
* \param set Set.
* \return Superset id
* \pre \a set is a non negative properly initialized id.
*/
int getRepresentative(int set)
{
while (set!=parents[set]) set=parents[set];
return set;
}
//! Initializes a singleton set.
/**
* \return Set id of the initialized singleton set.
*/
int makeSet()
{
if (this->numberOfElements==this->maxNumberOfElements)
{
int *parents = this->parents;
this->parents = new int[this->maxNumberOfElements * 2];
memcpy(this->parents,parents,sizeof(int)*this->maxNumberOfElements);
delete [] parents;
if (this->parameters != 0)
{
int *parameters = this->parameters;
this->parameters = new int[this->maxNumberOfElements*2];
memcpy(this->parameters,parameters,sizeof(int)*this->maxNumberOfElements);
delete [] parameters;
}
if (this->siblings != 0)
{
int *siblings = this->siblings;
this->siblings = new int[this->maxNumberOfElements*2];
memcpy(this->siblings,siblings,sizeof(int)*this->maxNumberOfElements);
delete [] siblings;
}
this->maxNumberOfElements*=2;
}
this->numberOfSets++;
int id = this->numberOfElements++;
this->parents[id]=id;
//Initialize size/ rank/ sibling.
if (linkOption == LS) this->parameters[id]=1;
else if (linkOption == LR) this->parameters[id]=0;
if (compressionOption == CO) this->siblings[id] = -1;
return id;
}
//! Unions \a set1 and \a set2.
/**
* \pre \a set1 and \a set2 are maximal disjoint sets.
* \return Set id of the union.
*/
int link(int set1, int set2)
{
if (set1==set2) return -1;
this->numberOfSets--;
int superset = link(LinkOption<linkOption>(), set1, set2);
//Collapse subset tree.
if (compressionOption == CO)
{
int subset = set1 == superset ? set2 : set1;
int id = subset;
while (this->siblings[id] != -1)
{
id = this->siblings[id];
this->parents[id]=superset;
}
this->siblings[id] = this->siblings[superset];
this->siblings[superset] = subset;
}
return superset;
}
//! Unions the maximal disjoint sets containing \a set1 and \a set2.
/**
* \return True, if the maximal sets containing \a set1 and \a set2 were disjoint und joined correctly. False otherwise.
*/
bool quickUnion(int set1, int set2)
{
if (set1==set2) return false;
this->numberOfSets--;
return quickUnion(LinkOption<linkOption>(),InterleavingOption<interleavingOption>(), set1, set2);
}
//! Returns the current number of disjoint sets.
int getNumberOfSets() { return numberOfSets; }
//! Returns the current number of elements.
int getNumberOfElements() {return numberOfElements; }
};
//find
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::find(CompressionOption<PC>,int set)
{
int parent = parents[set];
if (set==parent)
{
return set;
}
else
{
parent = find(CompressionOption<PC>(),parent);
parents[set]=parent;
return parent;
}
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::find(CompressionOption<PH>,int set)
{
while (set!=parents[set])
{
int parent = parents[set];
int grandParent = parents[parent];
parents[set]=grandParent;
set = grandParent;
}
return set;
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::find(CompressionOption<PS>,int set)
{
int parent = parents[set];
int grandParent = parents[parent];
while (parent!=grandParent)
{
parents[set]=grandParent;
set = parent;
parent = grandParent;
grandParent = parents[grandParent];
}
return parent;
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::find(CompressionOption<R1>,int set)
{
int root = set;
set = parents[root];
while (set!=parents[set])
{
int parent = parents[set];
parents[set] = root;
set = parent;
}
parents[root] = set;
return set;
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::find(CompressionOption<NF>,int set)
{
while (set!=parents[set]) set=parents[set];
return set;
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::find(CompressionOption<CO>,int set)
{
return parents[set];
}
//quickUnion
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
bool DisjointSets<linkOption,compressionOption,interleavingOption>::quickUnion(AnyOption,InterleavingOption<NI>,int set1,int set2)
{
#ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
if (parents[set1]==parents[set2]) return false;
#endif
set1 = find(set1);
set2 = find(set2);
if (set1 != set2)
{
link(set1,set2);
return true;
}
return false;
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
bool DisjointSets<linkOption,compressionOption,interleavingOption>::quickUnion(LinkOption<NL>,InterleavingOption<IR0>,int set1,int set2)
{
#ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
if (parents[set1]==parents[set2]) return false;
#endif
int root = set2;
int set = set2;
int parent = parents[set];
parents[set]=root;
while (set != parent)
{
if (parent == set1)
{
parents[root]=set1;
return false;
}
set = parent;
parent = parents[set];
parents[set]=root;
}
set = set1;
parent = parents[set];
while (true)
{
if (parent == root) return false;
parents[set] = root;
if (parent == set) return true;
set = parent;
parent = parents[set];
}
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
bool DisjointSets<linkOption,compressionOption,interleavingOption>::quickUnion(LinkOption<LI>,InterleavingOption<Rem>,int set1,int set2)
{
int r_x = set1; int r_y = set2;
int p_r_x =parents[r_x];
int p_r_y =parents[r_y];
while (p_r_x != p_r_y)
{
if (p_r_x < p_r_y)
{
if (r_x == p_r_x)
{
parents[r_x]=p_r_y;
return true;
}
parents[r_x]=p_r_y;
r_x = p_r_x;
p_r_x = parents[r_x];
}
else
{
if (r_y == p_r_y)
{
parents[r_y]=p_r_x;
return true;
}
parents[r_y]=p_r_x;
r_y = p_r_y;
p_r_y = parents[r_y];
}
}
return false;
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
bool DisjointSets<linkOption,compressionOption,interleavingOption>::quickUnion(LinkOption<LI>,InterleavingOption<IPSPC>,int set1,int set2)
{
#ifdef OGDF_DISJOINT_SETS_INTERMEDIATE_PARENT_CHECK
if (parents[set1]==parents[set2]) return false;
#endif
int set = set1;
if (set1 < set2)
{
set = set2;
set2 = set1;
set1 = set;
}
//!Use path splitting to compress the path of set1 and get the root
set = parents[set];
int parent = parents[set];
int grandParent = parents[parent];
while (parent!=grandParent)
{
parents[set]=grandParent;
set = parent;
parent = grandParent;
grandParent = parents[grandParent];
}
parents[set1]=parent;
int root = parent;
//!Redirect all nodes with smaller indices on the path of set2 to the root
set = set2;
parent = parents[set];
while (true)
{
if (parent < root)
{
parents[set]=root;
if (set == parent) return true;
set=parent;
parent = parents[set];
}
else if (parent > root)
{
parents[root]=parent;
parents[set1]=parent;
parents[set2]=parent;
return true;
}
else return false;
}
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
bool DisjointSets<linkOption,compressionOption,interleavingOption>::quickUnion(LinkOption<LR>,InterleavingOption<TvL>,int set1,int set2)
{
int r_x = set1; int r_y = set2;
int p_r_x =parents[r_x];
int p_r_y =parents[r_y];
while (p_r_x != p_r_y)
{
if (parameters[p_r_x]<=parameters[p_r_y])
{
if (r_x==p_r_x)
{
if (parameters[p_r_x]==parameters[p_r_y])
{
if (p_r_y==parents[p_r_y])
{
parameters[p_r_y]++;
}
}
parents[r_x]=parents[p_r_y];
return true;
}
parents[r_x]=p_r_y;
r_x = p_r_x;
p_r_x = parents[r_x];
}
else
{
if (r_y==p_r_y)
{
parents[r_y]=parents[p_r_x];
return true;
}
parents[r_y]=p_r_x;
r_y = p_r_y;
p_r_y = parents[r_y];
}
}
return false;
}
//link
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::link(LinkOption<LI>,int set1,int set2)
{
if (set1<set2)
{
parents[set1]=set2;
return set2;
}
else
{
parents[set2]=set1;
return set1;
}
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::link(LinkOption<LR>,int set1,int set2)
{
int parameter1 = parameters[set1];
int parameter2 = parameters[set2];
if (parameter1<parameter2)
{
parents[set1]=set2;
return set2;
}
else if (parameter1>parameter2)
{
parents[set2]=set1;
return set1;
}
else
{
parents[set1]=set2;
parameters[set2]++;
return set2;
}
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::link(LinkOption<LS>,int set1,int set2)
{
int parameter1 = parameters[set1];
int parameter2 = parameters[set2];
if (parameter1<parameter2)
{
parents[set1]=set2;
parameters[set2]+=parameter1;
return set2;
}
else
{
parents[set2]=set1;
parameters[set1]+=parameter2;
return set1;
}
}
template <LinkOptions linkOption, CompressionOptions compressionOption, InterleavingOptions interleavingOption>
int DisjointSets<linkOption,compressionOption,interleavingOption>::link(LinkOption<NL>,int set1,int set2)
{
parents[set1]=set2;
return set2;
}
} // end namespace ogdf
#endif
|