Professional Documents
Culture Documents
ADA - UNIT-2 - Chapter-1 - SETS REPRESENTATION
ADA - UNIT-2 - Chapter-1 - SETS REPRESENTATION
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
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.
(10)
S1 S2
ieiOint Umon
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
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
Amonghe
elements o all Sets. elenuut d set
ndet CI3
Pavent
- 3 23 11 e|
Analgsss o SimpleUnkon (i, ) amd SimpleFind):
Union n-, n)
Gand Seuenca o Find ) , Finda) - - - Find ) .
(n-
(n-2
omon C, 3) Umon ( n)
-
n
PCi3 3;
PC3 temp,
e
h a fewen nody
PCA3 i
PCi3 temp
3
omon s)
Find(s)
Find
TSimpleFindc) w d cach Find () eguirey
30hng up 3 panent ink fieldn for a btal o 24 moves
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.
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
chidd
need to
bo4h ane in Same st, so no
|---1 - 8
2 3 4 5 6
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
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
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
51 7
3 5 6
()
th a Jcle.
Tll
Tl hoo,
no we have Saw dout the weghted onion
(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
(3