Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

UNIT-2

Bisjoint Set Opeafions:-


The diajoint set data Strucure caolled as

Union-find data struciune Omd moge-find set.


set hat when the Set paribioned
>The dsjoint mea
The Vaviows OPnations Co be
thdo he dissoint Ats.
Porformed
on the dnjoint 6ubuks.

Sf we have a set oNelements Loich One

aubats, amd we have to keep


perttioned to funthen
Each element n a pantraulor
sock oConneceivit o
Subut oConnectvi oSobruts tth Each othe.
TO do t h tape ooponakions icientg, we an ue

Umon- Find Data sBruetune.


In hs, we n add new sets, we Can muse

Omd we can also find he vepYeSentative


he Sets ,

set also ud Ho fmd out toheho he


membet o a

elements ane n he Same set (oD not Elficient.


be defined a the
The drsioint Sel con

Subads hue thene v no Common element blw The two


Sets.
Sets 0ole. epresented by paa wne disaoimt sets,
fSaud S One two seb aud 1tá, hen
he no Covwon elemert fox S nd
do not have an
The disioint sebs ane 4ho
Common element.

o SI = {1,7,8,93 S , 5,Jo3
Then we cau 50hat S1 aud s2 ane
Awo dwicint sets.
isjoint se4 OPtadions
D onton
Find

isj0int Set UmON


F S md Sa 0te
Hao disjoint sets,theiru
Uton S US consis o al he claus x
Xin Si ) uch 4hat
XAuch that
S3.

Algor Hhm
Srmple Union (i, s)
PCi3: mot'e O node
3 teunt noae
node
S1 1,8,13 So
2,5, 103 -

SUS2 1,2,5,4,8,1, 1o
Find
Find: Given
he
elenmntI, find he set
Contaii
Simple Find t
Algor thm Stomple Find C)
ohile(PC] 2o) do
PC
e tunn i

3
91 ,7,8,93 S2 25,Io 3 S3 l3,4,63
Then Find)= S3 Find(5)-Ss2 Find-1
Sel Representation
The Set will be Sepvesented as he tree stucture
phene all childven will store the addrus o Panent/
voot node. The voot nade will 9tove null at the
Place oPanent addre.
Sn 4he iven se o elements an eleunt a be
selected oh the Yoot node8enenall we select he
St o 4he Foot node.

S1 1,1,8,13 S2,5,10 S3 34,3


be e pvesented as
Then thuL nts Coam

(10)

S1 S2
ieiOint Umon

Toaom dgoint set union bo seb


two see
S amd S can 4ake. any one oot amd make it
Sub-rec ohe o-hen
union o SOmd SCam be

Yepre sented s -

10

(lo
S1 0 S2

Find
Penflm find opnakion, along wth the
xee
Yee Structme we needo maintain he name o
Each Set. So, we couiye one more dota shuckune to
Store th set namt.
The data stuctme CcnBains 22 A et name

fielcs ) Poioten to root.

SeNome Pointr
S1
S2

S3
Tn pesenting UnMon amd Find alaoithms
e an ove he Set names aud fdentf sebs Sust+b
oot otye epresenti thum.
4he
TO esesent h e Seta, we Ue an a u o of

elements whene n b -he maxin um yalue


1 to n

Amonghe
elements o all Sets. elenuut d set

An index values epvesent he nody aud

Enbri epresent the panent node.


he
the
FoY he Yoot value he entwll be

EThe 0una epresentation. a o sets ae os bela:

ndet CI3
Pavent
- 3 23 11 e|
Analgsss o SimpleUnkon (i, ) amd SimpleFind):

AAhough the ShmpleUnnon Ci1) aud SmpeFind G)


o8ane to to state, thur Pofo mane chanacteistic
0ne not o o d .
Considot the sets

Then we want -o rfom he Seuence o


Openatiom ke Union (,2), onson (9,) -

Union n-, n)
Gand Seuenca o Find ) , Finda) - - - Find ) .

The Seauence o Union Opoadions ane epresenteol


txee a below

(n-

(n-2

Since, he time aken fal a Union


Constant
he n-1 Sequence O union Can be procmed in
iwne O;

Foeuence oFind OpatBons ioill ake


tane ComplexitoO ) O0).
we Can impYovethe
pofoman ce. onion and
Find avonding he Cveation odegennate tee ba
oule foY
aplgina eighting Onion
LWeighing ule for union
no: o nodes in he tvee wth root I
he
han he nmbrn in he yee h 4he. root i, then

make he pcnent o iothouwie male i' the


panent o .
lel the set Union C1,9
3

omon C, 3) Umon ( n)

-
n

To implement weizhtin dule e nced to kno


hao man
ho nodt ane there in Eve ree.
mon
To do tkis
To we main{ain "count" field n the

Yoot oevey 4ree


Tf h e ot hen count Ci Equah to
no: onodt n tre th voot i.
Since all nodu othudhan oots have +Ve
numbt in panent (P) field, we waintain Count
can

in P field o the Toot Meqative nombo.


Algorithm weizhledUmon Ci,3)
sets wth voots i amd i, i#i Usins he weizhtd
union ule
PLiJ = -count LJ aud PC] =
-Coont C].

tump: = PCiJ+ PC3

if CPCi >PE]) han

h a sewon nodes '

PCi3 3;
PC3 temp,

e
h a fewen nody
PCA3 i
PCi3 temp
3

collopsing rule for find


38anode 6n the poth fs om o
Tcot and PC1+oo4[], then Set rLj to
Soot C3.
Le Consider the ree ceated
b ueigbted Union)
on he Ssquence. o <8.
Union C,), union C3, 4), Orion(5, 6) ad
onon C7,8).
unon (,6)
Union (2) Umon (3,4) umon48)

nion C,3 umon (5,7


43
5

omon s)

N proco he ,Find () Ronakiob- 8) Findo - . -

Find(s)
Find
TSimpleFindc) w d cach Find () eguirey
30hng up 3 panent ink fieldn for a btal o 24 moves

ehen Collapsina find Used he Find@) eguiNeS


80auP 3 link amd csebn lanks. each oyemaiing
7findy reguiye oing UP od one nk feld. Then he total
Costis nou onla 13 moVts(3 Boingup + 3 esets + emaining finds
7
Algorthom collapsing Find
Find voot o the. 4see Containingekment i
the
yule to Collapse all nodes from
e he collapsing t o oo+-

whtle (PCx]>o) do YP[Y]; Find oot.

uhle(i+)
from element i to he
Yeset he Panent hode
oot
S: PCi3
PC3 Y;

3
Houd Can we delecl a cacle in graph ?
Busing Jsaph houw we detect a cacle wth
he helr o using disient ets,

U , 2, 3, 4, 5, 6, 1, 83 2
un:Vneal
Set Hoe each vutex
5
labelled wh some weszht.
Ths i Unmvensal set wth s Ventice, (6-
leby Consido each tdge one j one 3
Omd f3lm he sets.
'

letvontice S1- , 8
23
Coth belong to he Univns al set.

Pfalm he Unon Openah on b I2. Add he


elementsI42 in a Set S1 amd emove 4hee

2 elements fromhe onivnsa set.

U M3, 4, 56, 7, 83

S ,23
B vsin saneiocedune,
Let S 3,4

U 5 ,6, 1,8
Sa 9, 4
Let 5 6 Ytice
U
sk, 7,83
Ss5,63
Let 8 vohc

U 83
S4 7,83
S 1, 23 Sp 3,43 3 5,63 S 1,83
Next he tdge C, 4)
Ventex 2 in 91 3 Both aie in nent
Ventex 4 in S2
Sths Sothat Perform
h e n -fom
Onion opuratio n
a new 9et.

S523, 3,4
l edge C,5
2 in
s53
s5 el:yorent Scd, forfom
5 in $3 unjon -then
Sc21,, 3,4, 5,63
Letedge (,3) both both ane in Same Set
So m a cJcle. we oil not Consido -thi Ventex
Net dge C,) d|onent sts S 4
we l toform Umon
operation, un 4he nes Setii
S7 21,2,3, 4, 5, 6, 1, 83
The a dge (5,7)>both o sam sct so Jele foneol uil not gonsicen
How
Cn we, dcpxe Sent lhe sels 8aph iCal?
U- ,9,3, 4, 5, 6, 1,®3

Let he 1d kdze C,)


C2 5,6)
Panent (

chidd

4 Goth 2, 4 ae in dale se ,so


Pton form Umon Opuabion.

Sdh Ponf8m Umon


C3s dit So

need to
bo4h ane in Same st, so no

Jherte a &le Hence oe have


Ponfm union. ' ,
deecded a cle
sothat
68 dllant st
Same St so hux
a cacle
Hou
Hou Can we. delect a cJcle h4he help of au ase
The Previous araph contains 8Vetic08
The
Re psesent all vetcs tn a Supgle amo

Tndice Sepre sent 8 Ventices.


Each fnde contains - |value meas Panent sbl

|---1 - 8
2 3 4 5 6

Fist, ede C,2)


aone
Tn he above avo both 1 4
paent itael. So make vertexa he child
oVonex(1. . se add 1 otindex 2 s
22 child of . amd we an add -2
a the tndex 1 Ctotak & no: of nodes)
- - -

2 1
56 8
2 no: 2 3
nod
dild of1
Ponent
SO Ne value

C3,4)

4 2 73 5 6 8
2 no: 0
porud o y
noou
66 n
-meam pasenl

o ;
of
n o d l e

Pa1ent
of
66

2
-23-2 5
45

8
8 2
no: of
nodu

- 2 3-2
2 3 5
1

Hne Paunt of 2
Paunt of 4 3

2,4 have dilaent Paolert, so e make

Votex 3 b he child o Votex 1.


We can adol D ot he ndex )
We can add-u at th tnder () as

Contaah Vertices

-2 5 2

2 3 6 8
,5) Tn he Obove aroo

p a n e t 0f 2

sPole itse
55 Cotai -2 Value meas fonent itse
Sothat e have to decide whether O oD would
beCome ponent
Since
Since wesght of O 4 egkt of G) -2
opola
OPPla union
openation a ckib (1)

-6 3 5

3 5 6 8

We add -6 at the ndex a two move nodes

Oie added to node O

sage Cy
Ponent tsel Panert of ()od (ale
Posnent J Same, Sothat thou b
Sorinaton of Cacle f we
fnclude he tdge (i3)
C6, ,6 auut of
Panent Of,
Paient of » Pount tse
weight of O -6>igt of 2
chld child of .
becau

we add ) beconu child of O.


at he index
we adol -8 at the index O a the wugkt of
no be Com 8
he 3raPh

51 7
3 5 6

()

5,) Panent of both the Vi ces are Same.

th a Jcle.

Tll
Tl hoo,
no we have Saw dout the weghted onion

we we tufom he vmon opnatiovn accordana to he


wengt 4he Ventices.

The highen weighteol Vet-tex become a parent


Th ouon child.
The disadvanBage ohis (weighted union)" 4ha-
Some nodts Hake moe time. to veah its poient

Tmabove 83aph, Sind faned of .

In idde s ant a thoe

(6
To Ovoncome hs probem, e e Collapsing find.
Colapsin find
Consider he above xamfle o
Once we he fanent of verdex which %O
knouo

inen 1e darecelz add verto.o he vode:O.


e ill adlso opdote he avnay.
an , add 1 a h index6
becouw he Ponent Of 6 now 1,
The Protey of diecl koking a node
o he dvect ponent of a Set knoun a a
collopsg find. Simla, e canlink 8 od 4 to he
hode .

(3

You might also like