Professional Documents
Culture Documents
ADA UNIT-3 Chapter-2 Backtracking
ADA UNIT-3 Chapter-2 Backtracking
BAcRTRACkING
be solved usin
soiaia
satiginA Some con sBxaint Con
usiR
Gackhack
All he solutiom solved usn back tracking
must soadiafa two consBraints.
Explicrt con sBsaints
Tmpliett
Expct constras ne the vules which vestrict
each to ake on valus from agoven set
nezative eal
X 20 0SSset of all non
numbusg
0 oY l
explctconskaints dependh, on he paricular
instonce o he Problem bung Salved. All he tuples
hat Btiwtepltcit Cons-raits de-fines he Possible
Solutron space fo
The mpkcst Constaints ane fhe ruls that detmine
Lobich ohe -uples in he Solution space satisfre
he critoian fmcBhon.
Sf he solukon Expsessble. o an n-tuples
0ne Chosen from
(M1, A, M3 n) whore
SOme Sinrle set, hen wt CO app backtracksng
e-node.
nenated called a
Ccuvnent bei
Seod node A node ohich s alread Erpanded
Applicakonm at Backtacknq
N-ueen's robem
"
9 SUn of Suhsets
Graph colo in
Hamtomon jcles probleua.
8-QUeens Problem
me task do find a
placemet o +he 8-pue
on a 8x8 chess boand uch 4hat no two ueem can
alack each otho.
Le Mxn chunboand hen,
No -uen ae undon atack
No 2-9ueens anc m Same Yow, Column diasona!
n=l 1x
to place chub0and then
lone - 9ucen hos on
yo
..No Solution fo 2*2 chusboand
3x3 humboCn d
c C3
uxy chmboond
This 4-9uem problemn
4x xy chusboand C16 celk.
1,8,, 63, Q ane 4-uuns
No wo ueem Oneaced em Same OW
Scme column
Same digonal
a e have to ind all possible solutions
h e set ogant3atron ohe-queem soluBion
Spare follos -
Tm Backkrackiqa, we should alwa constuct
the 4ee in DFS (Seplh- First Secnch) manoor,
C C3
SGm Ow
no
noPnopo Post-tion to &u
So-hat 30 dh Cackhack to
PeVious Quun e Qz. *,
13ain
Thoe a solson.
co
:. solution Ve cdor =(X, , 3,Xu)
3
plaud in colz 7u ylaud in tod4
Hoe Mm image he Omothun Solution.
4,1,3)
-Quecv Soluson SPace ree (wAh AHacks
14 (8-s).
Jor Queen's, te Jet
n-1
4 Cn-).
S1 2,3,4,5,6, 3,83 5
colu
118. 6
148. Quum 2
1,®,3, u, s, 6, 7,8.
e solulion syace The Solulkon Vectox
Consislp 0f 8 8-Auple . (1, 2, 3,, s, 6, Zq ,28
Queen in sich cokumn
An
An tmplkct Consvaints fo hi problem ane hat
no two Osto Cam be-h same. (ie, all 9uecm
must be on diloem column) Omd no Hwo guee
hn
Ths Tealizadion Scduces he 91ae of-Ihe solutron
Space o m 8 tupls to 8. tuplus
column valun.
3
)C,1 (3,
2-32-3 1 so h wo points ane
diotonal
--13-
ttwo Ote not
Thu pogs
diogonal o ¬ach alhun
6 &
u,6,8, 2, 4, 1,3,5).
MiTO fnage so 5,3, 1,1, 3, 8, 6, 4
-i)0 0 69, 82
Thwy o h n0 0f nod in S-late space -tree
Hor&-ueens Prolem
Sum Subsets pxoblem in Backlyacking-
Suppo we Oe
iven ndistinct Positive
numles
Cte, weigta) amd we desive Ho find all Combination
o thes numbuv who sum M. This v known a
SUm oSvbadsb pooblem
An element o he
soluti on vect8l EHhu 1 or o
dependna on whelher he 1cighrtD; neluded not- or
nd
S + ,kA1, £h-e
Let W=5, 10, 12, 13, 15, 18 3 n6, M 30
Ftnd allJ Possble subuts of w h at Sum-o M
o Ahe slade space ree
C1 s, 10, 12,13,15, 18
n-6 M: 30.
20
Tf hwe one
5,2,68 O2,68
15, 3,58
15,4, 46
27, 4, 46 u0
28,5,33 15.5, 33
o7 30 15-0
o el he mode. 43,, 18
H te au
24,6,18
Cuing bourd node. -0
tunclon o kill node
21+030
i
2 3 S6 Move Possibla
Soutto ale theu
3 10 15 30
2 +18 30
5+1013 -30
-2 n-y(, , s, ) = (A, 1,13, 24) M3)
Stake space tree It cotains all possble Combinatios
allh poss;ble sojutiom
+ 11 +13 424 = 55
0, 1, 55
To,2, 4,0 12 0
,337
15 4, 24
,4,34 u 0 30
Soluton soution 2: 1 l t
KN
4 l 13 +o 31
3
PossbL soludion ao 11O
To Prove. ha he Si3 he Sel of al suhset
clemenls i
0f n
Tolas no: of Subsetn O-f a fsoide se (onlaimn
nelamut
BinomaExpanston Fomula-
2)
n-1 n-2
en- +nen
Le X=l, d=l
- +ncn-+n
co tne +let cn
Ai, 33 uet
A n damnts
Mcn
co
nco+ne + Ye2 +lcg +
Algovithm GumOfSub (5,k,1)
Fiod al subsets of 1 ] hat Sum lo m. The
/values o C3, 1si<, have alvead been delvmined.
/s
K-L
C:] *x[aJ aud 7 3. The vLJ' e
n nondecscasin der. i aumed h at hOjm
nd WLi2 m.
3
G Yaph Coloinq Poblem 1
be o +ve numbet
Let G be 1aph m given
r h m colou we have lo check ohether the nody
m such a woy h at no two
OG Can be colowne d
color, Jet on m' colots a
adsacent nodes have same
1 3
C
3
'chvomatic Numbur m=3
Popo colo o a
gaph means
assgmo
cdotb to tts Votsces Auch that adiacent vertices hae
have
snenrtcalos
Red Blue 1 P
Red
veca R
Blue
Blue eRed 8-Colova ble
2. cdonakbe Jaafh - 3vaph
Reo
Blue
3 Red
G
R 6ue
- colorable gioph
) m 3
R R
RARRN In Jhi
In i e ae not check adaceng/aar
0 o tnaoe
Conolitson. 13
3 x 3 x3 3 x 3 X 3 X3
+ 3+ 3%3
u nodus
u+
3 + 3 + S+3= 3 -1|
3
approxn.ade 4 nt
Congtant
Tme
Comyloi =
c C
problem.
Thip Exponential tim takina
U backtsackiq to solve h e
problemb
Backlad 81aph Colonng Algothm
Algovthm mColoria (1)
formed usto he backlxacking
ccumStve
/Ths al was
y ith boolean adiac
n epresented
Sche ma.he Jraph All oments df l,2,*.m to he
mavix G[l:n,1:n3.
such h a t adiacent vertic ane
vnh ce o he araph
integon ane prmttd.
R he îndex
distinct
aaned
to colov.
o h e next vetex
then
o t e (Citn])
e i mColrng (k+) s
3ootl Cfak);
NewlValue ()
Algot lhwn
IC1:-1] pa have ben
assijoed te gor valus
he donge t o
in m.
Yepear m=y
y
xTo)- (alo+1) mod 5
DK3 = DJ +1) mod Cm+
+) mod 5
F(LKI--o)hen nlan
-fosá- o n do
uelsl Cfabu),
3
Applicatiomm of nayh Colon
Map colori
This Backlackoopmoplomi3akion paoblem.
olp: chomathc Nombor.
Convet h Map xaph.
Contin93 his, we ne able to frodout
- G
Aditulabio
Point
D-@-0--Xx
0--0-0-
Vonlox , CL Callud
Pendant verttx
Gu
f Ang aph comas Pendaut
vortex, then. hot araph doent
Contain
ov Hamiltonfan cJcle.
ns Contain Hamittonian
Cyele
G15
Hou) mo no: 0f Hamylonian cJcles wu exut
6 a 3eaph ?
usi State space tree.
(D)
NevtValue C) ;
Cken) or((kn)
Ahen n n
3
3 o! Cfabue)