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

UNIT 3 chaplo-

BAcRTRACkING

The Gientnal Melhod

Gacklacking is one oIhe. impodart algorithm


desian technique.
Maa
M n problems which deal wth seamching
a sel o- solutiom C81) aR for an opimal solution

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

X1, X, Xs, Mu) =(?, 4,), 3).

Gencnag Cacklsacking solve 3 probjems.


Enumnctton pioble - fhis , we Can sokve.
all he possi ble feasble solutions.
find h t h e Om
ectsion Probems - we Can

feasible soluton not

thin we can find whethr


Opthmi zadion problemy - In

hee Enst a but solution.

slote space tee Sn backackana, okile solving


ven paoblem, a Tee b ConsBucted bond on Jhu
chonce) moade. Such a tyee oth 4he posssble Solutong
Cne called a state space tree.
T
thhe
e ee ovgani3ation o he. soludi on space

sefned ô o a siaBt Spao 4r.


Each state space vee Cont oi Some nods.

A node. which has been 8enuated,


Live node
not Jet been enenate d y
but tts children have
Caolled a live node ",
e-node t Ae node whoe children ane

e-node.
nenated called a
Ccuvnent bei
Seod node A node ohich s alread Erpanded

and hre no e for futuste.

Gounding funchom ane used to Kill ve node


thout 8enenating t chldren.

Reph fst node Jennodon ith


Coundina
uncko m caled Badeaachng.

Applicakonm at Backtacknq
N-ueen's robem
"
9 SUn of Suhsets
Graph colo in
Hamtomon jcles probleua.
8-QUeens Problem

Bocklhacking a claseic appkcadon t o find


Solukion to he 8-uee poblem.
8-Guees psoblem is o Placng& uee on an
Sx8 chboand such hot none o thewm aleck one another
no 4wo ane nhe same Tow, column, or dogonal)

Simslanla, he n-ueem proble placen


nueens
On an nxn chwbband.

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

Called vvial solution it, one aud ow


sondion)
n:2 9x chwb0and 2 gueems in the covwesponding
col ol2
dhusbo and

yo
..No Solution fo 2*2 chusboand
3x3 humboCn d
c C3

No solulion for 33 hbood

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

Hee hwu no- piopo Position


3
to place 3 . Soth at
wnll U Backtrack to pevious
ueene

no
noPnopo Post-tion to &u
So-hat 30 dh Cackhack to
PeVious Quun e Qz. *,
13ain

Back4oack -to previou un 2


6hadso compleled all possibk Sols.
s0 8o wth .

Thoe a solson.

co
:. solution Ve cdor =(X, , 3,Xu)
3
plaud in colz 7u ylaud in tod4
Hoe Mm image he Omothun Solution.

beven of ,4,1,3= 3,1,4, 2

Hst also Queenm


3
o not undn e alack.
3 3 2
2

4,1,3)
-Quecv Soluson SPace ree (wAh AHacks

1+4 +Cuxs) +(lx3xa)+(uxax2x)


65 Nodes
14T).
If we 4ake. 8-Quees Problem, Jet

14 (8-s).
Jor Queen's, te Jet
n-1
4 Cn-).

6-ueens problemMin BackBrack»*


place 8-ueen on an
T
8x8 chessboand
So hot no two "addac" 1h
at , no two o thenn
ane on he same To Same Coluumn or Same
diajonal
3 4 5 6 8
To-Jres o Constsaimts
Gxplicrt constraint
Tpct 3

S1 2,3,4,5,6, 3,83 5
colu

118. 6

The expltett ConsBaita


uS in formulation
On. 2L2,5,4,5,6,1,83, 8x8 chs boand

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

COm be on he same. diqgonal

hn
Ths Tealizadion Scduces he 91ae of-Ihe solutron
Space o m 8 tupls to 8. tuplus

To est whetho he Queem ane on he


Same diogond, h To
p h(ol

Lehe osion of St queen b Cad


O,
e have obseve h ad,
he
element
on same oliagonal 4hat
rom h Uppor left to he kow Vizht ha
he some Tow -

column valun.

> evo eemun on he Same diagon ad hat


Evo
Bpuh fuom he uppon vght b the loo
lt has
he Some O+Column valu

2ueem liy on Jhe Same ologonal i


3

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 &

Soudion s (A Xa, u s ,Ae, K4 ,A8

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

3o anode at leve be the st child corespordi


o 1 omd he Yght ckild conespondin to
=o.
h e Bounding funcdion we Une Br1,h- )=
true

nd

The state space tree Com be drawn vsing he O


tai
4ollown technique Velu S u m
f
al
Jiven
no

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.

ineludud 2 0 Nof îneluded

20

to4a 6 ugt So 7eved ee includigg the root


Heigt 6 .

To-ta Paths ane

Tf hwe one

hme Couuming proc becau his

Exponnial. So-that b by ad backlsackp


e wil tg o ksl he noclesf k oe not
so bound funcdion.
w: 5, 10, l2 ,13,15, 18
5 4104 12 +13 +15+18= 73
Z;

5,2,68 O2,68

15, 3,58

15,4, 46
27, 4, 46 u0
28,5,33 15.5, 33

40,33 23,5, 33 oKU 28,6,1830Y616


Solvtoni.

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

18, 3,37 | , 3, 34 1 24,4,34


13 0 X
0,4,47L,1

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

Sc+Bet 3c t >ca total no. f subsets of-


Set A
huut 2 tl

Lt e have a set having n ud

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.

Grennate lst child. Note: s+wl] <m Sne 6y4 4e.

fCStw[k) -=m) thn rte (C1: ) :/subsB found,


Th ne s no eco stve call hore a wo, 1<sh
e (s+wCK + (k+<m)
hen SumOfSob(5+CkI, KH,Y- LKI)
anenate vight ckilol omd Evaluate B
4((s+x-s[]2m) omad (3+[kt1]<m) thn
o
SurnOf Sobl s, k+1, v-wlKI);

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

wLo Called m-colorabilitt decision paoble m .


chromalic Numbo (m)
The m
coloubilita ophamisat on ask f8l the smallesr
rdege Cn) 451 wkich he naph G ca be cobwted.

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

- node roph omd all Possible


3-Colovin
Tme Compe O(n)
condttom
6Hate space tree whout
o
R

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

Sepeat No new ColorThen shop


Nextvaluu()
/XC3O s,

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.

Avaw fo J 'detnmined in +he 8omge o o 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

4 (CGCA) +) and 603-- xC)


hun leak
Adzacont
Makr 3
hen etun

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

h inimum Volue o the Colbs.

Cach gion ob mank a


Ventex
dxau an dge Ho
nuzhboaiag Jiom
Ham;toian cls problem
Let G-(V,E) ba connecled giaph with nvoice.
A HOmiltontan cJcle a ound mp Path alor
nedges of Gthat visits Eoch omd EVUVntex
Enactlt once Except the slan Snding verlet a
Setws to the stain Vortex.

- G

m h Jvaph Gi, contaim


HamiHonian jele e) , 28,4,6,5, 4,3,1.
u-3-1-2-8-7 -6 -5-

Aditulabio

Point

n hs oph G conHais o Hamgltonfan


Cgcle Be coum G have Jvncion poiwrt/Adsculat ion
on
5 2 -4 -3-2
vStted twsce .
G3 alo have Axdicularon
Point so Cng also docso't
CorHain HamiHoman
xaph
G13 O---0
YsHed
4wice

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)

HailHontan Cjcle peths AeC-F- C-D


asu A-B-F-t -D-C
A-6-F- - c- D 4) A-D-c-E- F -6
2 5 A-D-¬-F-8- c
5 HamilHontan dcles
Algovidhn Henltonian

NevtValue C) ;

# Ck==n) hun rte ( [1)


ew Hams Honian(r+);
3 undil Gtal);
3
Alzorithm Nextalue (k)
epeat
x3 03+1) iod (n+1); hxt vortx
fGx3 O Jhen otun

(GCk-3, xK1J +o) hn


if
a s he a dg2
fo l to K- do t (CJ ==x[k])-4hun bieak:

Cken) or((kn)
Ahen n n
3
3 o! Cfabue)

You might also like