Daa Unit 2
Daa Unit 2
4) Colapsial Fincd
Sets
Dsipint Con tain
Con eain (om
olves n't Common 1Simple Onion
the sets
Jf such type o Seks a the uneton of sets
then To sbtain
elaments Sets one tree aas
Disjoint the
sootnocle oF
known as cet
set
o ther tre e
n=10, the elements Can be child node of the
Ea: When the
E into 3 dis/oint sets
paatitioned
are
2 disjoint set
S ,7,8,1 F S ; and Sj
thenS;US; al euments of 1j,.
S S,2,1o3
s uch that x is in Sj
be repsesented in a tree
The Gets Cn
oomat (
Sat S
n SDS
S S
These at te possibla tree depresen tatiens
Df be seks
Dispint Sets Openabons
The flouing opeaationg can be erfoamed S,US2
on dis joint Sets 2A2boi
) Pijpint et Union e) Simpla briom
) Simple Find
Repres enbakhon ofs
thisarvay. vepresent te
Data Struckuse
elament of
thot con tain eunent
tore nodas
roi The axay, elument gives the parent
Pointer of the Correspon dinq Esee nodus
E 2 . PL:3 -el27
Simpu Union algevithm
tvea
algovithn ycot ohon
Jn this.
Each Toot noda has a pointer to h nodA
hild f r o t s tsee
tree
t becomas
set name
set to dateSmine ohih setan
element i Curently n, we ollbu o Algo SimpluDrion(i,i)
parent nk ts the vcot node f th
tee
Aray Represontation o S,, se: whse and ase DonodaS F
differenttrees
23 S 2.
PI--S31 ST Ez
w han we Pesrorm
the tvecs , ad S2
Simpla COnionlt, S)
en
S3
S
S The 3oot node of S,
tTeebecomms
mot rocde PS
tha
2 Find.
daterminas the voot node e the kra
FrindCt)
the folas
process the folewi elament 1
wproces containin
Now
of
oUnien 6pesahon
Algs SimplaFind (i
Uvnion(J)
Uhion(i,i )
Tule fa
Neighted u) Csllopsing fhd
3
no. onodes tre oith
in tha s a noda on path emi
th
the
than the oof nodo
nodas t
no.
toTs oot and P t roti) He
TDo i s ess
thetre oith xoot J , then
make set PLj) root[G]
thaise mak i t
the patent of Alao Colopsingfird (i)
paent o
hilel PC] >t) do
Algo welyhtedcnion(i,))
aneL: cCunt [7
PL:- Cout[i1 -PL
Ferp- Ptil+P L3,
F(Pt:1> PLi1) then SPLT1
PCi-s
S
PL1-temP
sen
else
- ree
a)Initiad Heke
2
2
follbwin Unisn 3
b H e i g h 2
Tyee Onio S,)
Backtvacking:
all
all the
the
and Dynamic progsamming
Jn Gyredy whieh we selet one
solutions among
possible
possible
Problem State
Evexy noda in the tree dafines problem starte,
Soluion Space:
A1L tuples that satisfy he explieit
Constoaints
ints dafines a po1sib le salution space for the
neda C
C, C3
Dead nade
is a qenexafed nodo i.e, e ther not to 62
Fo any given problem, tr UT all possible these Queens on aX chess boerd in such aua
in the
te
SolTie ns that no Sucans should be placed
Brute foree paroblems have mul tiple Solstons Same go (column(Biaqonol postbion
nthis appoah, we try to ind o u saluhons The Ezplicit constvaints Sas 9 uons are to
to solve the Baekoaek be plate On a x hessboasnd in 4s
py obum By using
9 u
constraints
aoe no Quns Can be he Explist eonstyaints says at 8 uauns
The Jmpliut Can
bn
be heSolukion vetoy, etoy, wha t h e Srnplicies constrai nts ave ne 2Queens sho4
utx2,3, 24
eh wkich uan is plor.i e placed in $ame Tow(coumn diagonal
humbey
i s column
g 3
32
out io veckor are ; is the column nuwnbe
on uwhich ueen i)is placed.
Notposble .Algovithm
Nob posbl
Algo baueen (k,n)
foT i to n dlo
f (k=n') Ben
printC L1.n3
else
Ngenk,n )
fY-1 t k - i do
Comsidey a 8x8 chess b0axd
let he be 8 Qusens ( L 1 : i) o abs(1L5)-i) abs(j-k
The Objecive is to place tha seburhfalue
chess boad in such
Quees on
on
an .8 XS
an 3
rcbun tue
should be placed a tJay that no 2 Quans
Same fcolumn[diagone
m3 m3
me 2
3
V3
State Spoca
2
Boundin
tuneho
hs prob lam is
pmblr /e Sw o
Subsets
tormplauity oF
Theime
3
set S n sbjccks
3.3.3
Given
tt 3 + 3.3 3.3.3 and a postive
N L eighes n
3 3
We have to find
tind a sbs et
3-1 inteaeo m
3uch hat
Su o Sudsets
) S cS
>qiven 5Et abjeka
2) Sum of Subset f elamen ts
A ,each intexnal. naddaomn) time isSpent set uinq
y ne valuts detesmina tha ch;ldsen 8) Find all Subsebs e f given
30
Sum o Subse poums.T m
Covresponding to g colosina
}nce, tDa Eme is boundoo by 6 o ,i1,13,(,18 3
Sabse is incueded t else 0
mn S.m'n=E
nm m- t
o(nm" venset Soution is
6]
State spac ee SoUwtion Spos
Soubion 0,0,03,i0,1,1,0,03,
0,0,1,0,0,13
subset as a S udaon.
:/ steph f subset s .
subset antil we
not feasible
fnd most sui tabu
backtyack threug
then
walut
ith ho solauhion.
then sop
s Add teumant.
Add d eknnt
s,t
,
27 Adodelknt
5,1o,2, 3 Jnfeasibu col"
s,to,12,15
s,lo, 12, 1s
5,to, 133 28 Add u elerent
Infeasib sobbron
es,to,3, 43
5 , ,13, 18 6
Feasbe dolszon
&lo, 1Sy (30)
,1o,5 Gsoph oleving
Soens inkeqr, itis
s,12,17 ae feasib Let Gbe a vaph and M be t y
be calored in
,3 f i n d hethey
nedes of can
qvoph is
A ho
diauo n
n o Pan in such wa that
2 edges COSS each othey
vepeat
heztvalue ()
F U Jo) than vehuyn
F Ckn) thun wnite(zn)
else
mcologinq (kt1)
until (fe)
Alo hexhelue ( )
3.. 1[thas been asigndintege valuds in
hange 1 om S-7 adjatent vesties have distind
integes A vae for z[k] is Astesmined n Bhe
Dm 7[k) is assig ned next hishect rno. ud
hieh main tairing distinction fom the adjacent
Vesbees of Vatek.4 mo Swch oby ezist han
*