Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Dynamic Programming and Backtracking Algorithms, Study notes of Design and Analysis of Algorithms

Two algorithms: the 0-1 knapsack using dynamic programming and the backtracking algorithm for the N-Queens problem. The former is based on a recursive formula and is used to solve the knapsack problem. The latter is a decision-based method used to find solutions for problems such as the N-Queens and the Graph Coloring problems. The document also includes an example of the Subset Sum problem and its solution using dynamic programming. The algorithms are presented in a concise and technical way, with little explanation or context.

Typology: Study notes

2022/2023

Available from 02/03/2024

saurabh-jaiswal-5
saurabh-jaiswal-5 🇮🇳

5 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download Dynamic Programming and Backtracking Algorithms and more Study notes Design and Analysis of Algorithms in PDF only on Docsity! 24) nN) 2023 ee Unit—@ 7 8: 0-1 knapsack using —_ dynamic Programing ——~ U v v Let cfs, w] denotes dhe value of He Solution for tteme a a i amd marxoimum weil Ww: Re js defined ca — cf, W] = © y th d=o or W=e = cLt,w] sik dso and Wi> W = max] Vi + c[ia,Ww-Wi], cfw-t, wh} 4 Ph d>o aud WisW © dn dhs W denoted the capacdy of Krabcady A\goxtthro — Synomic—o-I- knapsack: (v w,, w) fr Lb — oto WwW e[o,uje ° Be feb oe efiap=-* fe Leni ® W po wi cl then i VS +q in, d- wi] > efi | 1. dhen - ~~ » — ee, . efit] e— yb cle dew; ] » else efi) cfi-t,L] else fied] — ¢ft, n=4, Wes (Wi % Vs V4) = (3,4, s,6) (Wr Woy Wo, Ws) = {234 4,5) . PF golutiona cfi,w]: c[4,s] we pick Wa=S ewes w; < W cf4,s] = aon (6+ C[3,0], cps] 5 - maa 4 b+0 , 33 x F Ss Play's Well 4 WorShal) Algorithm fer - Bavtelh gh —* © This algorithm is used fo Solve al — boive Ghovle@ patho problem 07 directed prep G= (YE) : © This a}gowithm js based OP dhe dynami¢ Progen mm "4 approach ' © Let J, be — the ooeighct of a bhordeél for wi path fem vedo do 1 veto J al) iudermedi arte vertices are in ohe ge} q')2, no k5 ' Recursive fprrula — dj = Wa , if k=o | ain Sas 5 Jo + Mg] 9 P kot juder idiate vest - op where fb Is é: Construct he fiton ce prredlce ss 07" prada” _ Se Ppypypypp? mown do when kK=0 Fel, = NIL iB deg or y= 09 = J 8 LIF LEY aud wy < 20 for k>I nh, - ry 5 up te VD de )) “ =n, , ie a> dt 0% Note- O af rumber of — vertices ig on dhen we make nxn weuibo maticee + @® xf Je iar he we put=0 jo dhe diGent motrin * . ; (© whe — path does nok nice —beteween dd 4 we pot 4 g 9g «© -¢ ‘Iwgh opt MEL I wm ol. # wrt Na MTL 2 % 0 “ rs we, NIL wIL WA HR a 4 OM So + : NI oO oO 6 Oo NIL NIL MIL & verqicee =m =S so. we , po, a nl?) or, ) 7 EE EE Oe (kK) Me dy=d kd kg (kK). , ae ) K a = KJ Time combeisy sf bis a alert hin) a(n ; §: Batkbracking $: method » a method byl of dlecisiou uni] © Back bathing isa out —vortou@ ceqwenles fing dhe en oolubon + © Gauk-tvaskiog tig dbot ® Answee 4 eT ; ate aver thoge — Eolution for which © n f\ ' e thr ba th frm veo} lo leat voli const rail of thot by x0 hb] tm + > N= Cucer Problem hy have Quren The objective «of EMO problem, Fs to bce all dhe N queers on hit Chteaboord in euth way no. wo. queens fie OP m Ur ‘ Same sow, column Or cliagonal : ty- 4 dt 3 Spee JP ger £2,013 5 s'd queen can nok ke placed Goluptou backs trusk - Pah we EE, EE EOE __—_ —_ @ > Subbree two queens Ont placed at positroue they ( fu, iJ aud (k, vy ave Ow dhe Sau e diagpna)- only if (ia | Or Wy s kad J-j = k-t ; Ie j a or J-k = { jf) Therefore, two gueene lie on he game diagonal N-Cueea (is, ») 4) for jet ton do if Place (x,t) = Tree 6) 6 then kK] , 6 fe then for j= i ae, clo pat *(D §) ele N- Oueen (+ ly n) xithm co Alge Sur £ Sub (s, Ir, x) @ AH! @ i#F(s+ wh)=") ® for gad tok ® pint 23] © ele if [s+ wk] +wlkti] <™) SurqgOt Sub (S+ wlk], kt1, ®- w[k]) and (st wlhs+]<™) ) © @ iF((str- wikl 2”) © x[k] — © sumofSub (5 kt, 7 -wLKd) The jnitial r urn J) po ca} fs 8 oF Sub (0, |, 2m) where my assume that all dhe wejete ae in increas order sur of afd the Clements added y= The total k= That element which fs e be s = initia set Ore} Ourtion— Let WH £5, (0, 12,18, By 10] and We 380- nd al possible eubselg of Ww thot Sum WW Oo és using Qu OF &vb - (Drow de portion of Oe Gale Spall free Aah 8 generated {5/10/1SS 4S, 12133, {12,19f ph (S k, wo (o, 9 Tote) NeatVa lue (kK) { crepea + { fk} = (xI 11) med (mV) if (ak) = 9 hen ¥elurn if (GifxCK-1], * fp) thee for ge! +o k-] do if (xLa= xpi) lsseak; fo (aek) de // 1B bee, Her is hi@tinct P((ken) ov (Lk=n) oe dhen dhe . vevter oafefed, BI) +e) fren vel ; J Jontil (false): © : ' let Gr be a aap and m_ é a given fl basqj - ive integer. We wort to discover whether dhe podee of (4 can be clored jn dvgbe. 2 way that no two adyacent nodes have the come -telor ge onta mlm, Ore dsed- Tyg 16 Formed dhe m= colorabilty decision problem - ashy |@ The om pein optimization problem for the smele iucke gee pm for coleved - This integer Is geoph Gi Can be veford oes the Oromatic pumitt of He geth Gy bx Ra oF m(oloving (k) { cwhide (4) { Newt Value (k) ip (xk =¢) OW SNSSLSVES UUVUUUUUUUU UU Ur ooo xeturn Frey) print Xi): nj else raColoring (k+4) s J > Newt Velee (x) piled) afk] («f+ 4) mod (™+4) if (x[k] = 0) atlua for[ga i en) | if (olk.d] #° aud (xi =») break if (J= o¥4) ety Ba oo ¢ 3 12 a 3 3 o 6 (e 9] 9 S 8 oo 6 10 “8 9 8 S00 yn}? is) 14 9 ¢@ co g Sdution— for —weduced — ta med? that vow aud at teat Ta) eavk cond aire at dee one column +> ia, ae Os 4 eo 9 Ss O oo 8 ! &b o 3 w~) 6 0 2 A g p 6 ! 0. & ¥ co + oOo. 9 O Result 0.0 3 I ! ts Oth Ooh qQ 6 0 a2 A S) fo 6 ! o ° a wd of: veduted rabid “Leo - B4 345+ 3404S = 2F = ae & + 4 a 4 zy ny Ss QO. <a ad Boog 4 + 5 a8 sod Ba 2 ; 2-29 4 i = ¢ SB ggg 8B egg £ 88 w 2 fo 8 Tota) oO 5S eS Ove "© aRRARRRARARARARRAAADRALLS Ss + “_ o ab a ( g € (e