[go: up one dir, main page]

0% found this document useful (0 votes)
71 views26 pages

Backtracking and Branch Bound

ADA Notes

Uploaded by

bhadiv612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
71 views26 pages

Backtracking and Branch Bound

ADA Notes

Uploaded by

bhadiv612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 26
Section- O Rack Paacking-- BadePrackng eofaoed He most —S ae dechwigur Jer Fnching Hy all (som) -soluHins 40 Some Compudationa) preblina Hod Gncremudally buds —condidatis to He Athans 2 discard each paattal condi date ‘e! (backitada”) ay toon ag et chéfermines Hat ve comt possibly on Completed te 0 valid —sdlulfon. Td done by tenting a pasted coud dite 1! watt Comblamuly (etn Functor). When the contiont ; a “ba sncling Jundion’ — ace we Patliah candidate by daly * badetindk ” Ths tp dart technique Hany a] Ra rcs me abe using Gaeta yp Hat all Ha Solution ~-tatidy o sot a} Conitrou Fer any problun, Haze coutiands con boo ddvided to 2 Cathegorven'- Ww Drape Co Eepliat: nfliort _conibaaints am tHe uly Hot welch of He tubla in He solution ape ol I sodidy He citteion tench Ghbot cowhaid are -turles thick testeh each 2 to deka on values them a. gihen eh Fa Roth He combaintz ya) be Uroleuterd mote cloaily th prsblims bored on Backteacleriag. Now, dt deotn Hue olelinidonu. Gn Backtadury , selution hace o] any Probln & soprseted buy “Hen Gaganizatn pokese : / Ths He is cabled sorte space hee - Vv Banc terminolepies used Un Ades space Ther & oblen comp wn exam ay as U) Live node! A noda whch hos boon = ae orate 2 all af whore childin hove ~ net gt pertiakel: w) E-hode To Dre node ushese child, ——— are curtectly ung “J uy, | oP adi.. (nede So 3) Dead -nede!- E-nsde , @ when leibled by Kooning function GO called Dasa Heda: (Wy) Rey node? A taht or ath tolled — Arsuer nrevche: u @ Geral Muthed ackTratlury~ Hou le gE aie wolsd ra Sacktinckiey Db wed. do dobre al) He prabler based on Vea aplemch "6 baare] gentra) wothed af Ba clin teerp Bb qe bolow: gest Backdsacke (1c) a Loy (each efi} eT (2610-.., fed) olo 1 Hf (Bic (v0, sefe) --- Lie)) +0) Han Toy Gt xb]. afk) a path to OU CS tr nods) den rottle (elise) ; Sf (keh) tun Ba dobiack (ti); Backtrockinp aswest Hu search of cy ede in Depth Punt ardor. Psi (acy eg lesa. SethcTbndls'yt on a eddy Dow gde 4 ££ Outen problems both, WD) Y_Qusen probleme Ths problem atlas ee Het pnt Gta No Con be plamd an Same colun cy well CO om Hho Aame danger om Wh chessbord- = +. Th 2 ) EEE “ea ain Ui) HM Gum aut be blaad Po baceteedcy - See 4 Chuttn, Han > w a w) a) © oa eae EEE a sh Dad tolubs Ques Pru node = 7,453 | cl. < + Tote ‘Ss | 3 &] a 5 a — SA a 2 gl | aA ms ew 0 ) e gF Sheets space eee for 8 Qusun. Pecbln. 374 CHAPTER 7. BACKTRACKIN Algorithm Place(k. i) 7, Retums true if a queen can be /,ith-eolumn, Otherwise it retiy global array whose first (Kt 4/ Abstr) returns the absolute vu Ht placed in th row and 8 false..2 isa } values have been set ne of r forj = 1 to k-1do if (2) = 9) // Two in the same column 8 (Abs\z[j] — i) = Abs(j— k))) 10 // ori the same diagonal Y fe then retiirn false; return true; 7 Em for eter, Algorithm NQueens(k,n) 4, Using backtracking; this procedure prints all 1 Possible placements of n queens me ee n 1 2 3 vA {/ chessboard so that they ate nonattacking | { . i 6 for i:= 1 to.ndo 7 8 9 10 n 2 13 rr 15 } ie Place(f. i), then (6 = n) then write (zl rn); else NQueens(k+J,n); Algorithm 7.5 All solutions to the n-queens problem ' raph Colerrng poeblen!— Let & be 4 ahh ee ee Zin be yell sider Ther we ave te dcoet lull He meds of can be Colored tn such “HE canbe Been bom the thady dtd B4 Gols ore veaded tor tuffiue-d fo Fh all Re radu of Joh [ma ‘ nh a Tst colors 1 , only pay sd Yt t OL Op wes gan 23 vreles 4 Aull ombtils ahh. ee veel eee KING 74. GRAPH COLORING 1 Algorithm mColoring(E) 2 // This algorithm was formed using the recursive backtracking 3 // schema. The graph is represented by its boolean adjacency 4 // matrix G[l:n,1: n]. All assignments of 1,2,...,m to the 5/7 vertices of the graph such that adjacent vertices are 6 ic 8 9 // assigned distinct integers are printed. is the index // of the next vertex to color. 0 repeat 10 {// Generate all legal assignments for 2] A M1 NestValue();'7/ Assign to [hj a legal color. \) 12 (c]k] ="0) then return; // No new color possible = 13 if (=n) then —// At most m colors have been uu /7 used to color the n vertices. 15 write (a{1: n]); wa 16 else mColoring(k.+ 1); 17. } until (false); 18 } adja- ement of the Algorithm 7.7 Finding all m-colorings of a graph {Algo- aa 1 have t from sively. Below repre- = sexist a2 2=1, 1, and Jat by Se opm ie the /| [\ )\ (I runded 4686686060 Figure 7.13 State space tree for mColoring when n =3 and m = 3 mplete colors ryalue 5 of mand k. , @ 384 CHAPTER 7. BACKTRACKING 1 Algorithm aoa 2 //2{1),... ,2{k ~ 1] have been assigned integer values in 3 // the range [1,m] such that adjacent vertices have distinct 4 // integers. A value for 2k] is determined in the range 5 // [0.m). x[k] is assigned the next highest numbered color 6 // while maintaining distinctness from the adjacent. vertices 7 // of vertex k. If no such color exists, then z{f] is 0. a 9 repeat Vv 0 ho rt fkh:= (gif) + 1) mod (m #1); // Next highest color. R if (2[kj = 0) then return; //‘Alll colors have been used. OA 13 for j :="1 to ndo 14 { // Check if this color is a 15 // distinet from adjacent -calors. ay 6 if ((Glksa] 20) and (2]é}\= "4 > 17 // Xf (k,3) is and edge and if 18 // vertices have the same color. 19 then break; 20 } 21 if (j =n +1) then return; // New color found 22 A } until (false); // Otherwise try to find another color. 23 Algorithm 7.8 Generating a next color 7.5 HAMILTONIAN CYCLES: Po dhs, Let G = (V, £) bea connected graph with n vertices. A Hamiltonian eycle {suggested by Sir William Hamilton) is a round-trip path along n edges of G that visits every vertex once and returns to its starting position. In other words if a Hamiltonian cycle begins at some vertex v1 € G and the vertices of G are visited in the order 11,09,...,Un41, then the edges (vi, ¥441) are in E,1 Sin, and the y; are distinct except for vy and va41, which are equal’) ¢ graph GT of Figurg 7.15 contains the Hamiltonian eycle 1, 2, 8, 7, 6, 5, 4, 3, 1. ‘The graph£2 of Figure 7.15 contains no Hamiltonian eyele There is no known easy“way to determine whethgr given grapK contains a + Hamiltonian cycle, We now look ata backtyacking algorithaef that finds all the Hamiltonian ples in a graph. The gyaph may he dirgefed or undirected Only distinct pfcles are output a Myo as on Page (364, 396) 4 vratkool . hone 386 Gi: | | ! | G2: Figure 7.15 Two graphs, one containing & Hamiltonian cycle > tundton 1 Algorithm NextValue(k) 2 //a{1:k—1] isa path of k—1 distinct vertices. If z{k] = 0, then 3 // no vertex has as yet been assigned to zt]. After execution, 4 // 2[k] is assigned to the next highest numbered vertex which 5 // does not already appear in {1 : k~ 1] and is connected by 6 // an edge to a{k — 1]. Otherwise 2[h] = 0. If k= n, then 7 { / in addition z[k] is connected to 2} 8 g as repeat a ‘ -2[h] = (x{k] +1) mod (n+ 1)3 // Next vertex. 2 if (2[h] =0) then return; 13 if (Glr( — 1), a(t] #0) then 4 { // Is there an edge? 15 for j= 1 to k—1 do if (2{j] = «|k}).then break; 16 // Check for distinctness. 7 if (j=) then // If true, then the vertex is distinct. 18 if ((k thachin Lrabsoele 40) 5 ola » gira] eset aes ex. a: Ul) -@ 32 Sey) =-E3 Let nue) Serr W) Consider 96) 54 duct oie dt com be ket oy net? oo otis 4 or e1F 0. Caleutato ayom by tak M4 £ X1=0 ———-A V) poy veel LI] V0 4 In 4 as Tats) 32 seyret A) -3% J Lewepdond: when 22 0 z! py! o\oslk ac. Odhu 2 (c)> -38 who XMu=4 Kerb, Mel, Me), G20 Aol wi) ocom whey y9>0 EB)= -38 udr)~ —3P Px Same ee Sake or trot Oo qcu)= Sis QCy)ye 3b ag UW) Det object & cheten beey Av mye 1 & M20, we howe “4Ode-sk ot Qoa)> -n: Sn ty Mb) 738 Whee oe) by tow dha Ela) whew 20. co Men AL). values object is Conndlusd. Ofe use Db ul) = 32 “ret me AU) = -38 —) pL bal ; UQ)= 3 de» ei: 3 uls)= 2 ee 4u)= Me loots. ual ae wey = U le Alert ule" Cer “os Slee uli)>— ee Saar aw far dG Cl, 1, oY) & kab an He bag & a. 38 Blea cette, oeole coliulodan teesrubaeude ge. Nar Aer xust, see He paths Keop xe, pape bree Brel laa 202) Fay 20. ale “Taavellng Salesbortn Piolo. ug ree ® Bindi es Wy, EP be He gobh Veet aes dhe cat of Age 24 craze A ido g we Withed the dn ef orwell), we cam autune Yet way tro eta ends ot tare vette 4 3 Cost tw are caloulebs 8¢), 20) ig ule) ane 40) can be obtained by usey Ledguced Cort rnoctatx. (RCN) let a be the ROM Roy node &- ket 6 be He child f R Such Hot ane odge 9) conapodpinedlu dy ede Raduee ha malt BH 20% (col) de argluced yf i Crchaing atleast — ome geo + al) He Aemaiwl elie A np = ato cod) lf ery dot + coluum D deduced: For acducly woduy 4, teshionet nie Value un arey, An 2 &ebtract Page ity lia Aros this win valine each Awe ean 4 in vale 2 lo" Upp ee ne Ge oe yes Se a2 Wea > 3 ine 5 ft sade (0S, eae oleehore Lot yee os Bae steps & fo don 4 = 18 oo IMefeaso I. (st oo poke Nos, ty each Ao dev bb athart sau to ge eo ve yro eee Ps ui oe cwhbtact Column 4 £4 ve oes ms, steps | eo lo fon id ess aed ae 12 0 Pag eee lreeeaneeeee 3) seduced Mabilx Ca USED as. gy Aili CACM) SUV 0 heey, Soe tind Le cost sly oddiny all whith ate chosen — Jor Aublinel''y oem Bo hk Slum Cole So Pa fap attegees taveh labs [= ae Hens easbeat allt He tm ontppet hat howe tertth otter 20 BE mone, the Aimed tor will be Yow duydh > 2s Noo fo thd He path om TSP Adeta shee ta, thy REN wel we wil aby Uy jot t (B) @ stent Pe er D The 2 sicb3 = Br path ACI, 2)-u rede to L- (a av x t= 354 Io _ te ook, =.35 oS sdehye 2 SOs Ror bath eC (tea)? lon Jo Q s ss BAe: fl ged nei |= U) st aow ad aed cole - 00: CV i (Bic! ole oc (6) Check in all sows 2 abl coluns ome Yoro Shold ekot - 4 wt . aaah Th thy column 4G net —hawry pis So dake wit vale os We cubtiad Hi, tro Colun 1 we4al (Wot |- as PROM: : Hees Sor godr (1,4): Wy on aia bee a 0 PU Noe cot: 2+ 60406 nok a f Fka > Wr phiwik Bae ae tr apmanpetia> ots wo w0- ) ais a (spa O cheek yee Ge cack Colton & dro: 0200 0 00 foal [Fo Etand foe 3 > Avduced 6 3 ~m 0 ig} J M2 co Joo OO te E Now, Solve ‘oy uself. 4@r Hod for path, om vs dou wns + Calun" Go Ole: am donp alee bath ti, WA). ) do mmo a cluun ap mottlx ab path (4) as a0 @) Wi? ® v feb) = 2° ee ==5e 12. 02 co \a\ 0 jo) 300 lm | 2 aloe) “00 [ro aw | s FT pote Radio g bya e Cl s byt to make Jélo: @ CO Wlao [2d] 20|feolo hottie = BAAIL~FD ©) | | ®M(c0], el aiele stl) Laat ered 8 3are do fox Pupemcet= 9. te Pah 1,4, 2,3 2 He madly 00 0) Wad 20 20 0 00 0000 C0 00 00 OW 0 0 M00 O wo 0 2m £ Jor Yan Vy 4, % © a 683888 Sy © anit Cover to 0 au not git om retest dove Choe a are

You might also like