0% found this document useful (0 votes)
17 views

Disjoint_sets data structure

Uploaded by

heythemmennouni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Disjoint_sets data structure

Uploaded by

heythemmennouni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 83

ECE 250 Algorithms and Data Structures

Disjoint sets

Douglas Wilhelm Harder, M.Math. LEL


Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada

ece.uwaterloo.ca
[email protected]

© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.


Disjoint sets
2

Outline

In this topic, we will cover disjoint sets, including:


– A review of equivalence relations
– The definition of a Disjoint Set
– An efficient data structure
• A general tree
– An optimization which results in
• Worst case O(ln(n)) height
• Average case O(a(n))) height
• Best case Q(1) height
– A few examples and applications
Disjoint sets
3

Definitions

Recall the properties of an equivalence relation:


– a ~ a for all a
– a ~ b if and only if b ~ a
– If a ~ b and b ~ c, it follows that a ~ c

An equivalence relation partitions a set into distinct equivalence


classes

Each equivalence class may be represented by a single object: the


representative object
– Another descriptive term for the sets in such a partition is disjoint sets
Disjoint sets
4

Implicitly Defined Relations

For example, big-Q defines an equivalence class of functions which


grow at the same rate
– We choose a single function to represent the class
e.g., we represent all functions with quadratic growth with n2

Another example: partition the numbers from 1 to 20 according to


the relation a ~ b if a and b share the same factors of 2:
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}, {2, 6, 10, 14, 18}, {4, 12, 20}, {8}, {16}

These equivalence relations are implicitly defined by the relation


Disjoint sets
5

Explicitly Defined Disjoint Sets

Alternatively, a partition or collection of disjoint sets may be used to


explicitly define an equivalence relation:
– a ~ b if and only if a and b are in the same partition

For example, the 10 numerals


1, 2, 3, 4, 5, 6, 7, 8, 9, 0
can be partitioned into the three sets
{1, 2, 3, 5, 7}, {4, 6, 9, 0} , {8}

Therefore, 1 ~ 2, 2 ~ 3, etc.
Disjoint sets
6

Explicitly Defined Disjoint Sets

Consider simulating a device and tracking the connected


components in a circuit

This forms an equivalence relation:


a ~ b if a and b are
connected

http://www.morphet.org.uk/ferro/s6mono.html
Disjoint sets
7

Operations on Disjoint Sets

There are two operations we would like to perform on disjoint sets:


– Determine if two elements are in the same disjoint set, and
– Take the union of two disjoint sets creating a single set

We will determine if two objects are in the same disjoint set by


defining a function which finds the representative object of one of
the disjoint sets
– If the representative objects are the same, the objects are in the same
disjoint set
Disjoint sets
8

Implementation

Given two elements a and b, they are in the same set if


find( a ) == find( b )

What find returns is irrelevant so long as:


– If a and b are in the same set, find( a ) == find( b )
– If a and b are not in the same set, find( a ) != find( b )

We will have find return an integer


Disjoint sets
9

Implementation

Here is a poor implementation:


– Have two arrays and the second array stores the representative object

– Finding the representative object is Q(1)


– However, taking the union of two sets is Q(n)
• It would be necessary to check each array entry
Disjoint sets
10

Implementation

As an alternate implementation, let each disjoint set be represented


by a general tree
– The root of the tree is the representative object

To take the union of two such sets, we will simply attach one tree to
the root of the other

Find and union are now both O(h)


Disjoint sets
11

Implementation

Normally, a node points to its children:

We are only interested in the root; therefore, our interest is in storing


the parent
Disjoint sets
12

Implementation

For simplicity, we will assume we are creating disjoint sets the n


integers
0, 1, 2, ..., n – 1

We will define an array


parent = new size_t[n];

for ( int i = 0; i < n; ++i ) {


parent[i] = i;
}
If parent[i] == i, then i is a root node
– Initially, each integer is in its own set
Disjoint sets
13

Implementation

We will define the function

size_t Disjoint_set::find( size_t i ) const {


while( parent[i] != i ) {
i = parent[i];
} Tfind(n) = O(h)

return i;
}
Disjoint sets
14

Implementation

Initially, you will note that


find( i ) != find( j )
for i != j, and therefore, we begin with each integer being in its
own set

We must next look at the union operation


– how to join two disjoint sets into a single set
Disjoint sets
15

Implementation

This function is also easy to define:


void set_union( size_t i, size_t j ) {
i = find( i );
j = find( j );
Tset_union(n) = 2Tfind(n) + Q(1)
= O(h)
if ( i != j ) {
// slightly sub-optimal...
parent[j] = i;
}
}
The keyword union is reserved in C++
Disjoint sets
16

Example

Consider the following disjoint set on the ten decimal digits:


Disjoint sets
17

Example

If we take the union of the sets containing 1 and 3


set_union(1, 3);
we perform a find on both entries and update the second
Disjoint sets
18

Example

Now, find(1) and find(3) will both return the integer 1


Disjoint sets
19

Example

Next, take the union of the sets containing 3 and 5,


set_union(3, 5);
we perform a find on both entries and update the second
Disjoint sets
20

Example

Now, if we take the union of the sets containing 5 and 7


set_union(5, 7);
we update the value stored in find(7) with the value find(5):
Disjoint sets
21

Example

Taking the union of the sets containing 6 and 8


set_union(6, 8);
we update the value stored in find(8) with the value find(6):
Disjoint sets
22

Example

Taking the union of the sets containing 8 and 9


set_union(8, 9);
we update the value stored in find(8) with the value find(9):
Disjoint sets
23

Example

Taking the union of the sets containing 4 and 8


set_union(4, 8);
we update the value stored in find(8) with the value find(4):
Disjoint sets
24

Example

Finally, if we take the union of the sets containing 5 and 6


set_union(5, 6);
we update the entry of find(6) with the value of find(5):
Disjoint sets
25

Optimizations

To optimize both find and set_union, we must minimize the


height of the tree
– Therefore, point the root of the shorter tree to the root of the taller tree
– The height of the taller will increase if and only if the trees are equal in
height
Disjoint sets
26

Worst-Case Scenario

Let us consider creating the worst-case disjoint set

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

And continue, taking the union of two worst-case trees of height 2:

1
3
3
1
Disjoint sets
30

Worst-Case Scenario

Taking the union of two worst-case trees of height 3:

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

And finally, take the union of two worst-case trees of height 5:


– These are binomial trees

1
6
15
20
15
6
1
Disjoint sets
33

Worst-Case Scenario

From the construction, it should be clear that this would define


Pascal’s triangle
– The binomial coefficients
 1 m 0 or m n 1
 n 
   n  1   n  1 
1
 m      0mn 1 6
 m   m  1  1 5
1 4 15
n!
 1 3 10
m !n  m ! 1 2 6 20
1 3 10
1 4 15
1 5
1 6
1
1
Disjoint sets
34

Worst-Case Scenario

Thus, suppose we have a worst-case tree of height h


– We need the number of nodes and the average depth of a node
– Using Maple
> sum( binomial( h, k ), k = 0..h );
> sum( k*binomial( h, k ), k = 0..h );
we get:
h
h
 h  h
   2 h n
k
 k   h 2 h  1
k 0  
k
k 0  
– Therefore, the average depth is
h 2 h  1 h lg( n)
h
 
2 2 2
– The height and average depth of the worst case are O(ln(n))
Disjoint sets
35

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

What is the average case?

Could it be any better than O(ln(n))?


– is there something better?

To answer this, I created a program which, given the integers from 0


to 2n – 1 continued to randomly choose numbers until all entries
were in a single large set
– For each n, I did this multiple times and found the mean (average)
height
Disjoint sets
37

Average-Case Scenario

The resulting graph shows the average height of a randomly


generated disjoint set data structure with 2n elements

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

The actual asymptotic behaviour is O(a(n)) where a(n) is the inverse


of the function A(n, n) where A(m, n) is the Ackermann function:

 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

However, A(4, 4) = 2A(3, 4) – 3 where A(3,4) is the


19729-decimal-digit number
A(3, 4) =
20035299304068464649 7907235 156025575044782547556975141926501697371089405955631145308950613088093334810103823434290726318182294938211881266886950636476154702916504187191635158796634721944293092798208430910485599057015931895963952486337236720300291696959215610876494888925409080591145703767520850020667156370236612635974714480711177481588091413574272096719015183628256061809
1458852699826141425030123391108273603843767876449043205960379124490905707560314035076162562476031863793126484703743782954975613770981604614413308692118102485959152380195331030292162800160568670105651646750568038741529463842244845292537361442533614373729088303794601274724958414864915930647252015155693922628180691650796381064132275307267143998158508811292628901134237782705567421080070065283963322155077831214288551675554073345107213112427399562982719769150054883905223804357045848197956393157853510018992000024141963706813559840464039472194016069517690156119726
9823378900176415171900511334663068981402193834814354263873065395529696913880241581618595611006403621197961018595348027871672001226046424923851113934004643516238675670787452594646709038865477434832178970127644555294090920219595857516229733335761595523948852975799540284719435299135437637059869289137571537400019863943324648900525431066296691652434191746913896324765602894151997754777031380647813423095961909606545913008901888875880847336259560654448885014473357060588170901621084997145295683440619796905654698136311620535793697914032363284962330464210661362002201
7578785185740916205048971178182040018728293994344618622432800983732376493181478984811945271300744022076568091037620399920349202390662626449190916798546151577883906039772075927937885224129430101745808686226336928472585140303961555856433038545068865221311481363840838477826379045960718687672850976347127198889068047824323039471865052566097815072986114143030581692792497140916105941718535227588750447759221830115878070197553572224140001954810200566177358978149953232520858975346354700778669040642901676380816174055040511767009367320280454933902799249186730653993164
0720492238474815280619166900933805732120816350707634351669869625020969023162859350071874190579161241536897514808261904847946571736601005892476655445840838334790544144817684255327207315586349347605137419779525190365032198020108764738368682531025183377533908861426184800374008082238104076468878471647552945326947661700424461063311238021134588694532200116564076327023074292426051582811070387018345324567635625951430032037432740780879056283663406965030844225855967039271869461158513793386475699748568670079823960604393478850861649260304945061743412365828352144806726
6768418070837548622114082365798029612000274413244384324023312574035450193524287764308802328508558860899627744581646808578751158070147437638679769550499916439982843572904153781434388473034842619033888414940313661398542576355771053355802066221855770600825512888933322264362819848386132395706761914096385338323743437588308592337222846442879962456054769324289984326526773783731732880632107532112386806046747084280511664887090847702912081611049125555983223662448685566514026846412096949825905655192161881043412268389962830716548685255369148502995396755039549383718534
059000961874894739928804324963731657538036735867101757839948184717984982469480605320819960661834340124760966395197780214411997525467040806084993441782562850927265237098986515394621930046073645079262129759176982938923670151709920915315678144397912484757062378046000099182933213068805700465914583872080880168874458355579262584651247630871485663135289341661174906175266714926721761283308452739364692445828925713888778390563004824837998396920292222154861459023734782226825216399574408017271441461795592261750838890200741699262383002822862492841826712434057514241885
6999427233160699871298688277182061721445314257494401506613946316919762918150657974552623619122484806389003366907436598922634956411466550306296596019972063620260352191777674066877746354937531889958786628212546979710206574723272137291814466665942187200347450894283091153518927111428710837615922238027660532782335166155514936937577846667014571797190122711781278045024002638475878833939681796295069079881712169068692953824852983002347606845411417813911064856023654975422749723100761513187002405391051091381784372179142252858743209852495787803468370333781842144401713
8688124249984418618129271198533315382567321870421530631197748535214670955334626336610864667332292409879849256691109516143618601548909740241913509623043612196128165950518666022030715613684732364660868905014263913906515063908199378852318365059897299125404479443425166774299659811849233151555272883274028352688442408752811283289980625912673699546247341543333500147231430612750390307397135252069338173843322950701049061867539433130784798015655130384758155685236218010419650255596181934986315913233036096461905990236112681196023441843363334594927631946101716652913823
7171823942992162725384617760656945422978770713831988170369645886898118632109769003557358846244648357062914530527571012788720279653644797240254054481327483917941288264238351719491972097971459368875371987291308317380339110161285474153773777159517280841116275971863849242228023734419254699919836721921312870355853079669427134163910338827543186136434901009431974090473310144762998617254244233556122374357158259333828049862438924982227807159517627578471094751190334822414120251826887137281931042534781961284401764795315050571107229743145699152234516431218486575757865
2819756484350895838472292353455946452121583165775147129870822590929265563883665112068194383690411625266871004456024370420066370900194118555716047204464369693285006004692814050711906926139399390273553454556747031490388602202463994826050176243196930564066636662609020704888743889890749815286544438186291738290105182086993638266186830391527326458128678280660133750009659336462514609172318031293034787742123467911845479131110989779464821692250562939995679348380169915743970053754213448587458685604728675106542334189383909911058646559511364606105515683854121745980180
713316361257307961116834386376766730735458349478978831633012924080083635682593915711313097803051644171668251834657367593419808495894794098329250008638977856349469321247342610306271374507728615692259662857385790553324064184901845132828463270926975383086730840914224765947443997334813081098639941737978965701068702673416196719659159958853783482298827012560584236558953969030647496558414798131099715754204325639577607048510088157829140825077773855979012912940730946278594450585941227319481275322515232480150346651904822896140664689030510251091623777044848623022948
8966711380555607956620732449373374027836767300203011615227008921843515652121379215748206859356920790214502277133099987729459596952817044582181956080965811702798062669891205061560742325686842271306295009864421853470810407128917646906550836129916694778023822502789667843489199409657361704586786242554006942516693979292624714524945408858422726153755260071904336329196375777502176005195800693847635789586878489536872122898557806826518192703632099480155874455575175312736471421295536494084385586615208012115079075068553344489258693283859653013272046970694571546959353
6585717888948623332924652027358531885333709484554033365653569881725825289180566354883637437933484118455801683318276768346462919956055134700391478768086403226296166415606675081537106467231084619642475374905537448053182260027102164009805844975260230356400380834720531499411729657367850664214008426964971032419191821212132069397691439233683747092282677387081322366800869247034915868409911530983154120635661231875043054675369832308279664574176208065931772656858416818379661061449634325441117069417002226578173583512598210807691019610522292638797450490192543119006205
6190657745241619191318753398404934397682331029846589331837301580959252282920682086223033258528011926649631444131644277300323779227471233069641714994553226103547514563129066885434542686978844774298177749371011761465162418361668025481529633530849084994300676365480610294009469375060984558855804397048591444958444507997849704558355068540874516331646411808312307970438984919050658758642581073842242059119194167418249045270028826398305795005734171148703118714283418449915345670291528010448514517605530697144176136858238410278765932466268997841831962031226242117739147
720800488357833356920453393595325456489702855858973550575123512953654050284208102278524877660357424636667314868027948605244578267362623085297826505711462484659591421027812278894144816399497388188462276824485162205181707672216986326570165431691974265123004175732990447353767253684579275436541282655358185804684006936771860502007054724754840080553042495185449526724726134731817474218007857469346544713603697588411802940803961674694628854067917213860122541950381970453841726800639882065632879283958270851091995883944829777564715202613287108952616341770715164289948
7953564854553553148754978134009964854498635824847690590033116961303766127923464323129706628411307427046202032013368350385425360313636763575212604707425311209233402837482949453104727418969287275572027615272268283376741393425652653283068469997597097750005560889932685025049212884068274139881631540456490350775871680074055685724021758685439053228133770707415830756269628316955687424060527726485853050611356384851965918968649596335568216975437621430778665934730450164822432964891270709898076676625671517269062058815549666382573829274182082278960684488222983394816670
9840390242835143068137672534601260072692629694686727507943461904399966189796119287505194423564026443032717373415912814960561683539881885694840453423114246135599252723300648816274667235237512343118934421188850850793581638489944875447563316892138696755743027379537852625423290248810471819390372206668947022042588368958409399984535609488699468338525796751618821594109816249187418133647269651239806775619479125579574464714278686240537505761042042671493660849802382746805759825913310069199419046519065311719089260779491192179464073551296338645230356733455880333131970
8036545718479155043265489955970586288828686660661802188224860214499997312216413817065348017551043840662441282280361664890425737764095632648282525840766904560843949032529052633753231650908768133661424239830953080654966187938194912003391948949406513239881664208008839555494223709673484007264270570116508907519615537018626479745638118785617545711340047381076276301495330973517418065547911266093803431137853253288353335202493436597912934128485497094682632907583019307266533778255931433111096384805394085928398890779621047984791968687653998747709591278872747587443980
6779824968278272200926449944559380414608770641941810440758269805688038949654616587983904660587645341810289907194293021774519976104495043196841503455514044820928933378657363052830619990077748726922998608279053171691876578860908941817057993404890218441559791092676862796597583952483926734883634745651687016166240642424241228961118010615682342539392180052483454723779219911228595914191877491793823340010078128326506710281781396029120914720100947878752551263372884222353869490067927664511634758101193875319657242121476038284774774571704578610417385747911301908583877
8901523343430130052827970385803598151829296003056826120919509437373254541710563838870475289505639610298436413609356416325894081379815116933386197973398216707610046079800960160248230969430438069566201232136501405495862506152825880330229083858124784693157203232336018994694376477267218793768264318283826035645206994686302160488745284243635935586223335062359450028905585816112753417837504559361261308526408280512138731774902002495527387345859564051608305830537707325339715526204447054295735383611136775231699727402929416742044232481138750756313190782721888640533746
9421384216992886294047963530515056078812636620649723125757901959887304119562622734372890051656111109411174527796548279047125058199907749806382155937688554649882293898540829132512907647838632249478101675349169348928810420301561028338614382737816094634133538357834076531432141715065587754782025245478065730134227747061674424196895261316427410469547462148375628829977180418678508454696561915090869587425118443583730659095146098045124740941137389992782249298336779601101538709612974970556630163730720275073475992294379239382442742118615823616131788639255309511718842
1298508307238259729144142251579403883011359083331651858234967221259621812507058113759495525022747274674369887131926670769299199084467161228738858457584622726573330753735572823951616964175198675012681745429323738294143824814377139861906716657572945807804820559511881687188075212971832636442155336787751274766940790117057509819575084563565217389544179875074523854455200133572033332379895074393905312918212255259833790909463630202185353848854825062897715616963860712382771725621313460549401770413581731931763370136332252819127547191443450920711848838366818174263342
949611870091503049165339464763717766439120798347494627397822171502090670190302469762151278521956142070806461631373236517853976292092025500288962012970141379640038055734949269073535145961208674796547733692958773628635660143767964038430796864138563447801328261284589184898528048048844180821639423974014362903481665458114454366460032490618763039502356402044530748210241366895196644221339200757479128683805175150634662569391937740283512075666260829890491877287833852178522792045771846965855278790447562192663992008409302075673925363735628390829817577902153202106409
6173732835984940666521411981838108845154597728951645721318977979074919410131483685446396169046070301075968189337412175759881651270007612627891695104063158576375347874200702220510708912576123616580268068158584998526314658780866168007332646768302063916972030648944056281954061906852420030534631566218913273090696873531816410945142880366059952202482488867115544291047219291342483464387053685086487490991788126705656653871910497218200423714927401644609434598453925367061322106165330856620211889682340057526754861014769936887382095845522115719234796868881608536316158
6288015039594941852948922707441082820716930338781808493620401825522227101098565344481720747075601924591559943107294957819787859057894005254012286751714251118435643718405356302418122547326609330271039796809106493927272268303541046763259135527968383770501985523462122285841055711992173171796980433931770775075562705604783177984444763756025463703336924711422081551997369137197516324130274871219986340454824852457011855334267526471597831073124566342980522145549415625272402891533335434934121786203700726031527987077187249123449447714790952073476138542548531155277330
1030342476835865496093722324007154518129732692081058424090557725645803681462234493189708138897143299831347617799679712453782310703739151473878692119187566700319321281896803322696594459286210607438827416919465162267632540665070881071030394178860564893769816734159025925194611823642945652669372203155504700213598846292758012527715422016629954863130324912311029627923723899766416803497141226527931907636326136814145516376656559839788489381733082668779901962886932296597379951931621187215455287394170243669885593888793316744533363119541518404088283815193421234122820
0309503133410507047601599879854725291906652224793197154403317948368373732208218857733416238564413807005419135302459439135025545318864547962522602517629283743304651023610575835145507394433396102162296754614157811271970017386114942795014112532806212547758105129720884652631580948066336876701473107335407177108766159358568140982129677307591973829734414452566887708553245708889583209938234321027182241147637327913575686154212528496579033350931527769255058456440105521926445053120737562877449981636463328358161403301758139673594273276904489203618803867549557518068900
5853292720149392350052584514670698262854825788326739873522045722823929020714482221988558710289699193587307427781515975762076402395124386020203259659625021257834995771008562638611823381331850901468657706401067627861758377277289589274603940393033727187385053691295712671506689668849388088514294360996201296675907922508227531381284985152690293170026313632894209579757795932763553116206675348865131732387243874806351331451264488996758982881292548007642518658649024111112730135719718138160258317850693224400799865663537154408845486639318170839573578079905973083909488
1804060935959190907473960904410150516321749681412100765719177483767355751000733616922386537429079457803200042337452807566153042929014495780629634138383551783599764708851349004856973697965238695845994595592090709058956891451141412684505462117945026611

75016692826025095077077821195043261738322356243760177679936279609936897519139496503335850715541843645685261667424368892037103749532842592713161053783498074073915863381796765842525803673720646935124865223848134166380806150570482905989069645193644001859712042572300731641000991698752426037736217776343062161674488493081092990100951797454156425120482208671458684925513244426677712786372821133153622430109182439124338021404624222334915355951689081628848798998827363044537243217428021575577796702166631704796972817248339284101564227450727177926939992974030807277039501358154514249404902653610
5825409373114653104943382484379718606937214444600826798002471229489405761853892203425608302697052876621377373594394224114707074072902725461307358541745691419446487624357682397065703184168467540733466346293673983620004041400714054277632480132742202685393698869787607009590048684650626771363070979821006557285101306601010780633743344773073478653881742681230743766066643312775356466578603715192922768440458273283243808212841218776132042460464900801054731426749260826922155637405486241717031027919996942645620955619816454547662045022411449404749349832206807191352767
98674781345820385957041346617793722853494003163159954409368408957253343870298671782977037333280680176463950209002394193149911500910527682111951099906316615031158558283558260717941005252858361136996130344279017381178741206128818206202326384986151565645123004779296756361834576810504334176954306753804111392855379252924134733948105053202570872818630729115891133594201476187266429156403637192760230628384065042544174233546454998705531872688792642410214736369862546374715974435494344389973005174252511087735788639094681209667342815258591992485764048805507132981429935
99114632399191139599267525763590074465728101918058418073422277347213977232182317717169164001088261125490933611867805757223910181861685491085008852722743742120865248523724562486976622453848192986711294529455154970305859193071984971054141816369689761311267440270096486675459345670599369954645005589216280479763656861333165639073957032720343891754152675009150111988568727088481955316769316812728921430313768180164454773675183534978579242764633541624336011259602521095016122641103460834656482355979342740568688492244587454937767521203247038030354911575448312952758919
3989368087632768543876955769488142284431199859570072752139317683783177033913042306095899913731468456901042209516196707050642025673387344611565527617599272715187766001023894476053978951694570880272873622512107622409181006670088347473760515628553394356584375627124124445765166306408593950794755092046393224520253546363444479175566172596218719927918657549085785295001284022903506151493731010700944615101161371242376142672254173205595920278212932572594714641722497732131638184532655527960427054187149623658525245864893325414506264233788565146467060429856478196846159
3663288954299780722542264790400616019751975007460545150060291806638271497016110987951336633771378434416194053121445291855180136575558667615019373029691932076120009255065081583275508499340768797252369987023567931026804136745718956641431852679054717169962990363015545645090044802789055701968328313630718997699153166679208958768572290600915472919636381673596673959975710326015571920237348580521128117458610065152598883843114511894880552129145775699146577530041384717124577965048175856395072895337539755822087777506072339445587895905719156733

Thus, A(4, 4) – 3, in binary, is 1 followed by this many zeros....

http://xkcd.com/207/
Disjoint sets
40

Average-Case Scenario

Therefore, we (as engineers) can, in clear conscience, state that the


average run-time is Q(1) as there are no physical circumstances
where the average depth could by anything more than 4
Disjoint sets
41

Optimizations

Another optimization is that, whenever find is called, update the


object to point to the root

size_t Disjoint_set::find( size_t n ) {


if ( parent[n] == n ) {
return n;
} else {
parent[n] = find( parent[n] );
return parent[n];
}
}

The next call to find(n) is Q(1); the cost is O(h) memory


Disjoint sets
42

Lotto 6/49 problem

How do you randomly select 6 numbers from 1-49 without ever


selecting the same number twice?

int array[49] = {1, 2, 3, 4, ..., 49};

for ( int i = 0; i < 6; ++i ) {


size_t index = mrand48() % (49 - i);
std::cout << array[index];
array[index] = array[49 - i - 1];
}
Disjoint sets
43

Lotto 6/49 problem

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

Move the object in the last entry to its

1 2 3 4 5 6 7 20 9 10 11 12 13 14 15 16 17 18 19 20

Now pick a random number from 0 to 18, and so on


Disjoint sets
44

Lotto 6/49 problem

This is an easy way to randomly permute a list:


– The randomly chosen entry is moved to the last position

int array[N];

for ( size_t i = 0; i < N; ++i ) {


array[i] = i;
}

for ( size_t rng = N; rng >= 2; --rng ) {


// pick an entry from 0 to rng - 1
size_t index = mrand48() % rng;

// swap it and the last entry--it's okay if index ==


rng - 1
std::swap( array[index], array[rng - 1] );
}
Disjoint sets
45

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

Application: Image Processing

One common application is in image processing

Suppose you are attempting to recognize similar features within an


image

Within a photograph, the same object may be separated by an


obstruction; e.g., a road may be split by
– a telephone pole in an image
– an overpass on an aerial photograph
Disjoint sets
47

Application: Image Processing

Consider the following image of the author climbing up the Niagara


Escarpment at Rattlesnake Point

Suppose we have a program


which recognizes skin tones
Disjoint sets
48

Application: Image Processing

A first algorithm may make an initial pass and recognize five


different regions which are recognized as exposed skin
– the left arm and hand are separated by a watch

Each region would be represented by a


separate disjoint set
Disjoint sets
49

Application: Image Processing

Next, a second algorithm may take sets which are close in proximity
and attempt to determine if they are from the same person

In this case, the algorithm takes the union of:


– the red and yellow regions, and
– the dark and light blue regions
Disjoint sets
50

Application: Image Processing

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

In this example, the third pass may, if


successful, take the union of the red, blue,
and green regions
Disjoint sets
51

Application: Maze Generation

Another fun application is in the generation of mazes

Impress your (non-engineering) friends


– They’ll never guess how easy this is...
Disjoint sets
52

Application: Maze Generation

Here we have a maze which spans


a 500 × 500 grid of
squares where:
– There is one unique solution
– Each point can be reached by
one unique path from the start

Ref: Lance Hampton http://littlebadwolf.com/mazes/


Disjoint sets
53

Application: Maze Generation

Zooming in on the maze, you will note that it is rather complex


and seemingly
random

Ref: Lance Hampton http://littlebadwolf.com/mazes/


Disjoint sets
54

Application: Maze Generation

Finding the solution is a problem for a different lecture


– Backtracking algorithms

We will look at creating the


maze using disjoint sets

Ref: Lance Hampton http://littlebadwolf.com/mazes/


Disjoint sets
55

Application: Maze Generation

What we will do is the following:


– Start with the entire grid subdivided into squares
– Represent each square as a separate disjoint set
– Repeat the following algorithm:
• Randomly choose a wall
• If that wall connects two disjoint set of cells, then remove the wall and union
the two sets

– To ensure that you do not randomly remove the same wall twice, we can
have an array of unchecked walls
Disjoint sets
56

Application: Maze Generation

Let us begin with an entrance, an exit, and a disjoint set of 20


squares and 31 interior walls
Disjoint sets
57

Application: Maze Generation

First, we select 6 which joins cells B and G


– Both have height 0
Disjoint sets
58

Application: Maze Generation

Next we select wall 18 which joins regions J and O


Disjoint sets
59

Application: Maze Generation

Next we select wall 9 which joins the disjoint sets E and J


– The disjoint set containing E has height 0, and therefore it is attached to J
Disjoint sets
60

Application: Maze Generation

Next we select wall 11 which joins the sets identified by B and H


– H has height 0 and therefore we attach it to B
Disjoint sets
61

Application: Maze Generation

Next we select wall 20 which joins disjoint sets L and M


– Both are height 0
Disjoint sets
62

Application: Maze Generation

Next we select wall 17 which joins disjoint sets I and N


– Both are height 0
Disjoint sets
63

Application: Maze Generation

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

Application: Maze Generation

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

Application: Maze Generation

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

Application: Maze Generation

Next we select wall 12 which joints the disjoint sets identified by B


and I
– They both have the same height, but B has more nodes, so we add I to
the node B
Disjoint sets
67

Application: Maze Generation

Selecting wall 15 joints the sets identified by B and L


– The tree B has height 2 while L has height 1 and therefore we attach L
to B
Disjoint sets
68

Application: Maze Generation

Next we select wall 5 which joins disjoint sets A and F


– Both are height 0
Disjoint sets
69

Application: Maze Generation

Selecting wall 30 also joins two disjoint sets R and S


Disjoint sets
70

Application: Maze Generation

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

Application: Maze Generation

Next we select wall 10 which joins the sets identified by A and B


– A has height 1 while B has height 2, so we attach A to B
Disjoint sets
72

Application: Maze Generation

Selecting wall 31, we union the sets identified by R and T


– T has height 0 so we attach it to I
Disjoint sets
73

Application: Maze Generation

Selecting wall 27 joins the disjoint sets identified by J and R


– They both have height 1, but J has more elements, so we add R to J
Disjoint sets
74

Application: Maze Generation

Selecting wall 8 joins sets identified by B and J


– They both have height 2 so we note that J has fewer nodes than B, so
we add J to B
Disjoint sets
75

Application: Maze Generation

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

Application: Maze Generation

Thus we have a (rather trivial) maze where:


– there is one unique solution, and
– you can reach any square by a unique path from the starting point
Disjoint sets
77

Application: Maze Generation

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

Application: Maze Generation

For fun, the following C code generates mazes of arbitrary length:

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,"_.":" |"];}

It does not use disjoint sets...


Disjoint sets
79

Application: Maze Generation

$ 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

Application: Maze Generation

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 );

for ( int i = 0; i < number_of_walls; ++i ) {


is_wall[i] = true;
}

while ( rooms.disjoint_sets() > 1 ) {


int wall = untested_walls.next();
int room[2];
find_adjacent_rooms( room, wall, n );

if ( rooms.find( room[0] ) != rooms.find( room[1] ) ) {


is_wall[wall] = false;
rooms.set_union( room[0], room[1] );
}
}
Disjoint sets
81

Summary

In this topic, we have covered disjoint sets


– Equivalence relations and the definition of a disjoint set
– An efficient data structure
• A general tree
– An optimization which results in
• Worst case O(ln(n)) height
• Average and best cases Q(1) height
– A few examples and applications
Disjoint sets
82

References

[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++, 3rd Ed., Addison
Wesley, Ch.8, pp.315-337.
Disjoint sets
83

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.

You might also like