Disjoint_sets data structure
Disjoint_sets data structure
Disjoint sets
ece.uwaterloo.ca
[email protected]
Outline
Definitions
Therefore, 1 ~ 2, 2 ~ 3, etc.
Disjoint sets
6
http://www.morphet.org.uk/ferro/s6mono.html
Disjoint sets
7
Implementation
Implementation
Implementation
To take the union of two such sets, we will simply attach one tree to
the root of the other
Implementation
Implementation
Implementation
return i;
}
Disjoint sets
14
Implementation
Implementation
Example
Example
Example
Example
Example
Example
Example
Example
Example
Optimizations
Worst-Case Scenario
As we are always attaching the tree with less height to the root of
the tree with greater height, the worst case must occur when both
trees are equal in height
Disjoint sets
27
Worst-Case Scenario
Thus, building on this, we take the union of two sets with one
element
– We will keep track of the number of nodes at each depth
1
1
Disjoint sets
28
Worst-Case Scenario
Next, we take the union of two sets, that is, we join two worst-case
sets of height 1:
1
2
1
Disjoint sets
29
Worst-Case Scenario
1
3
3
1
Disjoint sets
30
Worst-Case Scenario
1
4
6
4
1
Disjoint sets
31
Worst-Case Scenario
And of four:
1
5
10
10
5
1
Disjoint sets
32
Worst-Case Scenario
1
6
15
20
15
6
1
Disjoint sets
33
Worst-Case Scenario
Worst-Case Scenario
Best-Case Scenario
In the best case, all elements point to the same entry with a
resulting height of Q(1):
Disjoint sets
36
Average-Case Scenario
Average-Case Scenario
215 =32768
This suggests that the average height of such a tree is o(ln(n))
– See reference [1] for a detailed analysis
Disjoint sets
38
Average-Case Scenario
n 1 if m 0
A(m, n) A(m 1,1) if m 0 and n 0
A(m 1, A(m, n 1)) if m 0 and n 0
The first values are:
A(0, 0) = 1, A(1, 1) =3, A(2, 2) = 7, A(3, 3) = 61
Disjoint sets
39
Average-Case Scenario
75016692826025095077077821195043261738322356243760177679936279609936897519139496503335850715541843645685261667424368892037103749532842592713161053783498074073915863381796765842525803673720646935124865223848134166380806150570482905989069645193644001859712042572300731641000991698752426037736217776343062161674488493081092990100951797454156425120482208671458684925513244426677712786372821133153622430109182439124338021404624222334915355951689081628848798998827363044537243217428021575577796702166631704796972817248339284101564227450727177926939992974030807277039501358154514249404902653610
5825409373114653104943382484379718606937214444600826798002471229489405761853892203425608302697052876621377373594394224114707074072902725461307358541745691419446487624357682397065703184168467540733466346293673983620004041400714054277632480132742202685393698869787607009590048684650626771363070979821006557285101306601010780633743344773073478653881742681230743766066643312775356466578603715192922768440458273283243808212841218776132042460464900801054731426749260826922155637405486241717031027919996942645620955619816454547662045022411449404749349832206807191352767
98674781345820385957041346617793722853494003163159954409368408957253343870298671782977037333280680176463950209002394193149911500910527682111951099906316615031158558283558260717941005252858361136996130344279017381178741206128818206202326384986151565645123004779296756361834576810504334176954306753804111392855379252924134733948105053202570872818630729115891133594201476187266429156403637192760230628384065042544174233546454998705531872688792642410214736369862546374715974435494344389973005174252511087735788639094681209667342815258591992485764048805507132981429935
99114632399191139599267525763590074465728101918058418073422277347213977232182317717169164001088261125490933611867805757223910181861685491085008852722743742120865248523724562486976622453848192986711294529455154970305859193071984971054141816369689761311267440270096486675459345670599369954645005589216280479763656861333165639073957032720343891754152675009150111988568727088481955316769316812728921430313768180164454773675183534978579242764633541624336011259602521095016122641103460834656482355979342740568688492244587454937767521203247038030354911575448312952758919
3989368087632768543876955769488142284431199859570072752139317683783177033913042306095899913731468456901042209516196707050642025673387344611565527617599272715187766001023894476053978951694570880272873622512107622409181006670088347473760515628553394356584375627124124445765166306408593950794755092046393224520253546363444479175566172596218719927918657549085785295001284022903506151493731010700944615101161371242376142672254173205595920278212932572594714641722497732131638184532655527960427054187149623658525245864893325414506264233788565146467060429856478196846159
3663288954299780722542264790400616019751975007460545150060291806638271497016110987951336633771378434416194053121445291855180136575558667615019373029691932076120009255065081583275508499340768797252369987023567931026804136745718956641431852679054717169962990363015545645090044802789055701968328313630718997699153166679208958768572290600915472919636381673596673959975710326015571920237348580521128117458610065152598883843114511894880552129145775699146577530041384717124577965048175856395072895337539755822087777506072339445587895905719156733
http://xkcd.com/207/
Disjoint sets
40
Average-Case Scenario
Optimizations
Let’s play Lotto 2/20: Pick a random number from 0 to 19, say 7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 20 9 10 11 12 13 14 15 16 17 18 19 20
int array[N];
Implementation Summary
We now have two exceptionally fast operations: both find and union
will run in Q(1) time on average, and O(ln(n)) in the worst-case
scenario
Disjoint sets
46
Next, a second algorithm may take sets which are close in proximity
and attempt to determine if they are from the same person
Finally, a third algorithm may take more distant sets and, depending
on skin tone and other properties, may determine that they come
from the same individual
– To ensure that you do not randomly remove the same wall twice, we can
have an array of unchecked walls
Disjoint sets
56
Next we select wall 7 which joins the disjoint set C and the disjoint
set identified by B
– C has height 0 and thus we attach it to B
Disjoint sets
64
Next we select wall 19 which joins the disjoint set K to the disjoint
sent identified by L
– Because K has height 0, we attach it to L
Disjoint sets
65
Next we select wall 23 and join the disjoint set Q with the set
identified by L
– Again, Q has height 0 so we attach it to L
Disjoint sets
66
Selecting wall 4 joints the disjoint set D and the disjoint set identified
by J
– D has height 0, J has height 1, and thus we add D to J
Disjoint sets
71
Finally we select wall 23 which joins the disjoint set P and the
disjoint set identified by B
– P has height 0, so we attach it to B
Disjoint sets
76
You may also note that the average depth is 1.6 whereas the
average depth of the worst-case disjoint tree is 2:
Disjoint sets
78
char M[2],A,Z,E=40,J[40],T[40];main(C){for(*J=A=scanf("%d",&C);
-- E; J[ E] =T
[E ]= E) printf("._"); for(;(A-=Z=!Z) || (printf("\n|"
) , A = 39 ,C --
) ; Z || printf (M ))M[Z]=Z[A-(E =A[J-Z])&&!C
& A == T[ A]
|6<<27<rand()||!C&!Z?J[T[E]=T[A]]=E,J[T[A]=A-Z]=A,"_.":" |"];}
$ gcc maze.c
maze.c: In function ‘main’:
maze.c:1: warning: incompatible implicit declaration of built-in function ‘scanf’
maze.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
$ ./a.out
30
._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
|_._. ._| |_. ._._. . . | . ._| |_. ._._._|_._. | |_. |_. | |_. | | | ._|_. | |
|_._._. |_._._| . ._|_| . | ._._._._._._._| ._._._| | . |_. | ._|_._._. | | | |
| ._| | . . . . |_._._|_| |_| | | |_._. | | . . . ._|_| | |_. ._._._| ._._. ._|
|_._._._|_|_|_|_| . . . . | | ._. ._._|_. ._|_| |_| ._. . . ._._. |_. . ._|_. |
| ._| . |_. . . |_| |_| |_|_._. |_._._._| ._._|_. |_._|_|_| |_. |_._. |_| | | |
| |_._|_. ._|_| . |_| | | | |_. ._. ._. ._._. |_. . . ._. |_._|_. |_. | ._. | |
|_._. |_._. |_. |_._._| | | . . . | ._| ._|_. | |_| |_._|_| | ._._| |_._| |_| |
|_._. . ._._._| ._. . | ._._| | | |_| ._|_. | ._|_. ._._._._|_._._._._. ._._._|
| ._. |_._._._|_. |_|_|_. ._|_| |_._| . . ._|_._._| ._| ._|_._._._. . ._._. ._|
|_._|_| ._._._| |_. |_. | |_._._| . | |_|_._._|_._. | ._. . ._._. | |_| | |_| |
| | | |_. . ._. ._| ._|_._| | ._. |_. |_._. . . . . | | ._| ._. | ._._._. ._| |
| . ._| ._|_. | ._| | ._. ._. . |_|_._._. | | | | | ._|_._| |_. | |_. |_._|_. |
| |_._._| . |_|_| | |_. |_. |_|_. . ._| |_| |_|_| |_._| ._| ._| |_| | . ._._._|
| | | . ._|_._._._._| | ._|_._._|_|_._._. |_. | |_. . ._| | . |_| ._._| |_. ._|
|_._. |_._. | . |_. ._| ._. . . ._._._. | | ._|_. |_| | | |_|_|_. |_. | ._._._|
|_. . . . |_._|_. . |_._| | |_| . |_._. | |_|_._. | ._._| | ._._._. | |_._| | |
| |_|_| |_._._. |_| ._. ._._| ._| ._._| | | . . . ._._._._|_| | | ._| ._._| | |
| ._. . . ._|_._._|_. | | |_._|_._._|_._|_| |_|_|_. | | . | | ._. | |_. ._|_. |
|_|_. |_|_. . |_. |_._|_._._. ._._. . . ._|_._._| | ._._| ._|_. | | | |_. |_. |
|_. . ._| ._| ._._._. . | | | |_._. |_|_| |_. . . |_| | |_| . ._|_._. . | ._._|
|_. |_. | ._| . | | | |_|_. ._._._|_._| . . |_| | . | ._| ._|_| |_._. |_|_._. |
|_._._|_| ._|_| ._._| |_. | . ._._._|_._|_| |_._|_| | ._|_._._. ._. |_| | |_. |
|_._._. |_| |_._._._| . | | |_|_._. | . . ._| | ._| . ._._|_._. | |_. . ._| ._|
|_. ._._|_. | |_. ._._|_. |_. |_. ._. | |_|_. ._| |_|_. | |_. ._| ._. | ._._| |
|_._._. | . | | |_| |_. |_. |_|_. ._|_|_._. ._| ._. | | | | | ._|_._| |_._._| |
| . | ._._|_._. ._._|_. ._._| ._| . . |_. | | ._. | ._._|_. |_._. ._| | | |_. |
| | ._| ._._. . ._| | ._._._. | |_| | | |_._. |_._| ._. ._._._._._._|_._. |_. |
| | ._| ._. | | ._|_._|_. . . | | |_| ._. |_._._. |_._|_. ._|_. . ._| . | ._._|
| | | ._._|_|_|_._. ._._|_|_|_. | | ._. |_._| | |_|_. | |_._. ._|_._._|_| ._| |
|_|_|_._._|_._._._._._._._._._._._._._|_|_._._._._._._._._._._|_._._._._._._._|
Disjoint sets
80
The ECE 250 web site has a Q(mn) algorithm for generating an
m × n maze:
http://ece.uwaterloo.ca/~dwharder/aads/Algorithms/Maze_generati
on
The actual maze generation code is quite short:
Disjoint_sets rooms( m*n );
int number_of_walls = 2*m*n - m - n;
bool is_wall[number_of_walls];
Permutation untested_walls( number_of_walls );
Summary
References
References
Wikipedia, http://en.wikipedia.org/wiki/Disjoint_set_(data_structure)
[1] Cormen, Leiserson, and Rivest, Introduction to Algorithms, McGraw Hill, 1990, Ch.22, pp.440-
461.
[2] Weiss, Data Structures and Algorithm Analysis in C++, 3 rd Ed., Addison Wesley, Ch.8, pp.315-
337.
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.