Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2K views136 pages

Shivani ADA

AdA CS-402 guide book shivani for rgpv syllabus

Uploaded by

akshatsethi27
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)
2K views136 pages

Shivani ADA

AdA CS-402 guide book shivani for rgpv syllabus

Uploaded by

akshatsethi27
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/ 136
ANALYSIS & DESIGN oF ‘AleoniTHM Contents UNIT: Algorithms, Designing algoct ‘Asymptote notations... Heap and heap sort, = Introduction to divide and conquer technique, Analysis ‘design and comparison of various algorithms based on this technique, example binary search, Merge sort, Guick sor, ‘Strassen's matrix mutipicatio, ws, Analyzing algorithms, UNIT Study of Greedy strategy, Examples of greedy method ike ‘lima merge pattems, Huffman coding, minimum spanning ‘toes, Knapsack problem, job sequencing with deadlines, single source shortest path algorihm. nn S10 Nt: Concept of dynamic programming, problems based on this ‘2pproach such as 0/1 knapsack, mattstage grenh, relly sign, Floys-Wershall IQR nna (10910136 univ: Backtracking concept and its examples like 8 queen's Problem, Hamitonian cycle, Graph colgningprabiem e...(37t 187 Introduction to branch and bound method, examples ot branch and bound methodlike traveling salesman problem ~ eens : rll 7101) Meaning of tower bound theory and lis use in solving, algebraic problem, Introduction to parallel algorithms...(180to 184] unir-v Binary search trees, height balanced troes, 2-3 trees, Batreos.— nnn ere 86102 Basic search and traversal techniques for trove and graphs (Inorder, preorder, postorder DFS, BFS), NP- COMPLE NSS enn ~ 2241028] nS 1064 ‘ALGORITHMS, DESIGNING ALGORITHMS, ANALYZING © AtcoRtrins ANN QI. Define an algortim, Ans, Any well-defined computational procedure ta ake some valo,or seu ales, sit snd produces some vale, ost of valu, te eget ‘Sealed an algorithm, 80, in ofher words, we can sy that shat ransom input into the output i ke 02 Isthereany difference among: Sequence of computational steps wn aan algoritim. algorithm, pseudocode and program? Epa (RGR, June 2017) onli Difference among algorithm, preudocode and program ae as flows ©) Analgorihm is well defined sequenced coletion of ite st of Instustions for solving a pater problems Pscudocodes an approach of writing alsritiminthe programing endl tags (exactly in programming lnguoge is not neces). ‘A rogram is sequenced collection of iastruston writen in «particular sage (called programming language like, C+, Java ete) which ‘mands computer 1 perform speci asaske ‘esa ll Bseuocode or program can be called algorithm but vce Aang (i) Program canbe called as prewdocods bt vice-versa not tr. in sequen’ Aleoritims are writen only is sequence, pcudocode at writen fetence and by using some predefined keywords bu program ae writen eoectUse®: using some predefined keywords end a i fll sme les Sem by grammar of programming language soc) *leeith can be thought of a rough ids of any produc, Preudocode ' blueprint ofthat produ and program the fal produ. 4 honest Aro Som) (22 Dis bporn characters fe gor, dn alot «Ea tof natns St, towed access iat Bron, wehave ome se orca SET elowed bya he algeins “0m Analg sl ke 250 of me gun o io tee ea ped i). Oat Anal hu pod est ono (i Deftones -Eachand vy nsrsion tenet a teresa a menbiponn (ir) Finiteness ~ An algorithm must be such that if we trace out itt ints, en ral case grim erate ater ie on ore (0 genes —tn sit ey itt us bee vases etn cd ou nay epee go's pad ‘Speer astenugh atch operon dtr aie es miiretese or cranple usin, we a about iri son ol a (@)fn0, then l= 1 (if a> 0, thennt = ng 0 Ie we wet algoitm for finding factorial of ay number thea it wl {allow all the above specified eri in following manner @ Inpat Acuber 0 whose factorial isto be lune. () Oupue— A ruber, ie ator oPwumber (dup Deftteness ~ Each isttion is definite the function be done is to just mulply the number a with oe Fess than that of a Iemeans— pla). (40) Fineness Aigoritr wil trminat when 7 will become 220) ot (©) ifetiveness— Each inseuton, (0 1)is very basic a ‘an be done using peel and par er 04. How can the algorithms be designed ? ‘Ans. The tem designing algorithm means creating an lpi ths sccomplishes particular ask. Creating an algorithm isan at which a ever be ily aerated Ont 5 ‘There are many ways to design algorithms. We have many design reciniqus that have proven ote uel in that they hae often ied seo {Sfonthms These design stregies make it eay fo davise new and tte ‘cits For example, some of esignspprotchesarc"Divide-md Cone’, ‘Dynamic Programming”, et. “Keommon approach 1 solving. problem sto partion the problem into caller pars, find solutions forthe parts, andthe combine the sohiton fot ‘fear ino a solution forthe whole (QS. Whar do you mean by performance analysis of on algorithm ? explain (RGR, Dec. 2014 Ans. The efiieny of an algortum can be decided by messing the peformance ofan algorithm. The performance ofan algorithm can be Ineasued by computing two fictrs ~ (i) Amount of time required by an algorithm to execu (ime compet) {@)Armountof storage requited by an algsthn (space comple). (06. Explain the various criteria used for naling algorithm. (ARV, May 2015) o What criteria are used during the analysis ofthe algoriom ? (GRY, Dec. 2017) Ans. Ther are many cfitra wpa which we ca judge algo, as by + inigingfoowing things (0) Des it do what we want todo? (i) Does i work comeety according to the iia specifentons ors? ip 8 ere documentation that detrbes how to use and how it works ? tng YA BOs create in sch way ht hy pct og (the code readable? ‘The above criteria reall vitlyimpontan when i comes to writing ‘tar mot pei for lrg ayes - above criteria fr judging algorithms are important, but wo most impetus above cre fo judg lg © ‘Space complenty (ji) Time complexity. 07, Define algritm, Discuss howto analyse algoritons es (GRY, Jane 2016) Ams Algorithm — Refer to Q.1. 6: pra Desi At (9, Sm) “Aaayzing a lpr reas analy fan slain, Ati omar ostinato nscpriciyeccctathesnaarsed __fostune a imetinan seit wn ona wt he ‘When a lei exec, cies some ofthe esos ng ey neve de, Sina nsahinga at parce ss amt canal rong ni (CPU) prom operate eof alan. hing ie oot i wi fn ear immer nh meds nd una Yo old the prog and ag] wets nirmaion oot pristine das Ana ise oaeng conmaniitnn nih, of lop gle, bot most ens compen) #44 Homaton may be Foes Stone on alg eu ht we wan ome (iy Averages ~ heaven ring tine of git, ‘is sot easy fo measure, Sometimes get mata sila Han vet tou on eign 0 pat requted mesure ie Mahenssltot rudy ctade s Roughly 8 ofa a bal a he worsens ‘combinatorics lena probabil theory algebaiedestery,andte abi For example if we randomly choose» nurs and apply ineton- 10 identify the mos signiienat ems in fom. sort, we have fo calculate average time to determine where sebony Because he behaviour ofan algorithm may be diffrent foreach posi Alk=w-~--1 ~ Ip 4inser element Af]? tn an averee-cat, half of the input, we need a means for summarizing tat behavior in simple, eh cements im Ali---ei 1) are leas thn Af) and hal the lens ae understood formula (pewter In sch @ case We hve wo check only hall of he elemens of he Dating the analysis ofan algorithm our one immediate goal ito dg SOY ALL ad TSO ‘means ofexpesin that issipeto waite and manipulate, shows the mporta nia, ‘haraceatisof enalgorhm'srercorerequirenents, and suppresstedind Gently, average case ranning time ts out tobe aqua fincton ‘eae of be inpu sig, Le, similar to a worsteae nang time or example the ime taken by the Insron-srt procedure depends ‘heinput. Arson bndres of umber takes loner thensoring ive nam Ssconly, inserton-sorcan tak diferent amounts of inet sr toi sequences ofthe sme size depending on how nes sr they lta (iy esecase~ The bese ening ine ofan grin 2 Jower bound on the running time for any input. mn Tne besecase we have fo do minum aunbe of comparisons ‘cverticaiah went te mteamenemrimylg mean Wes ear eae a FO ee at ‘led al array Aft 4i~ |] is sorted in increasing order and element Ai] is ee Se at a nae meter soot et Ry rps i a yan {o predict whether the software wll eet any elceney const that ex nal analysis ofan algorithm deserts how walla alprithm ee in the Hestease, nthe worstcase and i averages (8. How sa alors analsed ? What do you standby averages and worstcase ofan abortus? (RGRY, Dec. 2011, Jane 201 Ans. Algorithm Analysed = Rofto Q7. Daring the analysis of an algorithm we check the algorithm frie ‘itferen inputs, Depending on the Ge taken by a algorithm to ex iret ipat, worstease,averagecate, and best case ate messed ‘umber of comparisons in above case, ai bee, 23. Esplin how time complesity of en ego can be oleate Give suiabie example. (GPK, Dec. 2012) or How sine complet ofan algorithm ix calntted ? Explain with stb example, (GRY, Dec. 2013) recat Tite complet of an algorithm ste amount of compute tet ‘25a oan to completion, The ime TP) aken by a program P isthe sum of ‘somite and run ime. Als, 2 program copie one can be exceed ny mes without recompiling. The compile ine doesnot depend onthe instance charts. urn time is denoted by estan characteris). We sul once in comparing , @ Worstense~ The worst-ase runing tne of an gsi s ‘upper bound onthe running tine for any input. Knowing ives us gt that healgorithm will never take an/longer We need not ake some eh pv about the runing time and hope tat never gee mueh Were. «8 Anoyas& Desig of Algorth (BE, 1V-Sem) For calulatng the run time of program we have to calculate ‘akea in sition, matiplcation, subtraction, division, comparison, load scr) santa on So, we cool aban an expression for f(a) as follows ~ {jad CADE) + C,SUB(a)* C,MOLAM) + CyDIVEa) Hore, 2 denotes the instance charctersic and C,, Cy Cy C de ‘ime needed fora adn, multiplication, subtraction, ind vision. ‘Determining the number of addition, multipication, subtraction ion 2 oe have another way ofnd time complexity. We count the ‘umber of operations, We ean count the numberof progam steps fo fi eution tine which iseasier than hat of erie one. The number of steps ny program depends on the kind of statement, Different statements have different counts Forexample, (0) Comments count ero eps (Assignment statements are counted stone, as they do ot ny ote loot. Gi) For iterative statements such as for, while, and repeat sfaements we count the steps eny for contol pat. For example, Tori = to do hile () do (a) In cave of while statement, expression has ie] count hat is coun for sp. (©) In case of for loop, we have count eqal to Sum of counter for and ‘Reninng excoution forte for sslement havesep count of fan "namin cep ea increases asthe number of inputs increases In thi e88, the mune of willbe compued asa function of the numberof inputs alone. ‘When we want to know how the computing ime increases as ‘magnitude of ane ofthe input increases then we have to fad the number ‘eps asthe Fueton oft magne ofthis put aoe. So, in above way, step counting wll elp a in finding ime compl 010. Wha are te Factors which affect the running tne of an gris) rate (RGPY, Dec. 20) Ans. Many fctors affects the runing time of an algorithm or prose] Some of them ae listed below a Pan As cade progam renin micore mH ap CPU Unizaion1C.CBU isalexdy ized by somecrber processes thea raring ime of algorithm wil intense, {V0 Bound ~ Sometimes VO bounds ke disk readwite aio speed et the ening ime (io) Overhead Due to many Functon Call ~ There could be huge sveckcsdon runing function cllismademuliplesines or smal inction 0) Recursion ~Recursif can ease otof overhead which neesses the runing time ofan algorithm, (0 Disk Layout ~ For mip disk, RAID might work beso faster than NAS, SAN of other storage layout (ot) Contention =1€ tere are multiple threads, they ae synchronized to scons common resources, then there can be contention forthe resources tnhich causes thread t wait (6) Bhieey Synchronization IC tera deadlocks, te agi oe progtin wil top working or the two thes involved in deadlock wl stop. ‘hsinteasesranning ene of any algorithm. 0.1 Ho recursive goin ar anal ? Anal the exciton ine af rcv agorith for tower of Hana; problem. (GRY, Dec. 201) cds. The steps tbe followed while analyzing tsursive lg areas sows (Decide the input sis based on parameter (dent slapd’ baie operons. 8) Check how many times the bare operations is executed. ‘There ermine te execution of basi operation depends upon th input Se Bd wor avrge ad et es fr pt of em th se ation depends upon worst cae, average cae nd est ese then hat as 10 be aalyood separately. ae () Set up the rcurence sation with ome ntl coditon and sspesing the basi operation. tga, SoH the recurence o sas determine the rer of oth lc solving the recurrence we wll she forward and backward substion Imthod. And then correctness of formula can bo proved with the belp of nate induction method. tins TOMETY OF anol — There ae thee pees namely X,Y and The five Of ieentdamters ae placed on peg X. The arangeent of he ts tha very salle i pacedon he rps disk, he pole, of Hann sates that move te dss om peg oper Hing pes Y oft 40: Anais & Basin of Agatti (BE, IVSem) ‘sonar. The conditions are — (i). Only the top disk on any peg may be moved to any othe pe (GA larg size disk shoud never be place on smaller one. Algorithm 14 Tower of Hanel Alport TOH(, X,¥,2) { ite fi wite"The pes moved from X 16 2") ) che t TOH(n~1,X,¥, 2}: limove top n~ 1 dks bom X to ¥ using ‘TOHIa~1,¥.2, X): move remaining ik from ¥ to Z wing ) fone disk hs tobe moved Dy then ) “The tower of Hanoi has 2 ine complet 0G" = 1) 00. (0.12, trite an algorithm for calelation th fu Fbonace! number 8 (og n) tine (GPW, Sime 2012) Ans, Te leith to compute the fa Flonsel umber flows Algorithn 1.2 Fibonacci Number Alport Fibonses (I) ¢ (et) then wit (fs, ee ¢ fat =1; f2=0, fori = 3 ton i f= fat + 2 = fal fal = ‘eit (Ey } ) | oatral mumbers = (0, 1 2, ‘ASYMPTOTIC NOTATIONS (218 Wha do you ndesund iy te om emps 2 Whats the sgniffance of asymptote notations ?(R.GRY, Dee 2017) Ans. The wor asymploties means the study of fnctions of parameter a avtecomes lager and lager witout bound, Asymptotic eens of Tm x concemed When the input sis are age enough to make ely aero arom of the running ine relevent. That is, we are concerned (tow the running ne ofan algorithm inceases wih the sn of the apt Jeike tm asthe 20 of the inptnereases witht bound. Foal very sll taps lgrthy thats asymptrcally more efficient wl be the Bes coe. “Ihe notations we use to describe the asymptotic running ine of an lao ace defined i terms of functions whose darins are he st Of 3}, Such notations are convenient for desebing the worst-case runing- time froin Ti), which usually defines ony an integer input sizes, ‘Ween ase asymptotic notation in varity of way For example, the naion is easily extended fo the domain of real numbers or aeravely, rested ow subset of the natal numbers 1.14 What lth use of asymptotic notations? (RGR, June 2015) Ans. Asympote notations ae used o desert he Hinting betavoar of funtion when the argument tend toward «patel vale, ual in "erms of simpler funetions, Gener, we use asymptotic noiation 388 cocvenient way to examine what can happen ina anton inte worse of 'n the test case. Various asymptotic notations ae most used fo cloning the performance or runing dine ofan algorithm 215. Whats espe notations ? Explain each (GRY, Jane 2011) ‘Or Describe about various apex of expe notation sed algrithn nats. aa (GRY, Des. 201) or Whats nce of a notations? Esplin by hing epee AN Len TY ee 213) or "hat are te different asymptotic notations wed ? Expin ” (RG Jane 2016, Dec. 2010) or What isan asympute notations ? Give the eifferet noon 30 fads. (GRY, May 201) *Prexent che complexity of algorithms. 12. Ansys & Design of Apart (BE, 1V 4 (0) bo 6-O@)u6n465 fora {© 20m 50 a 2008 20 (8 iw fas tl teria (© tore 10-6 OG) 1D? 1000-6. 101} forts 108 (0) 67249802) 4569 20+ 8 <7 2 fr BA, (@) 30237 O(0) adn 3 <0 brn 2 Gh) Lom + tn 2 Oe as et 24.2 Tt orn > 2 () 3ns2O)as3n+ 20 mle an Sbegul OC fred contin Ca all n> (On +2000, Note Wo we te) = Of, rem hing sto eens sul) an stp per bone) 3h ‘Sort tghtan er bnd e Foal ales fo lh on, val of finton io} rtebor Tehigohaosoah diietmening dentin co die asst itl Sone often ante sadetows. (oO) none cent comps tin, (8 0) tine conpeng tine (©) Onde ude opting (@) O(@°)— Indicates a cubic computing time," (©) 0()—ndcas especie. (0 Oto) nds optim computn tie 7) Ke) = Om Fig. 11 Inuition Behind O-Notatio nes Unt 13 (i Omega (0) Notion — Te faston lo) = 0) (ead ws ctotnistmeg 40 Se ne ote esa ml ea 0) 2 C0, fall n> just 5 O-noision provides an aot wpe bode atm von pve an mpone oer Bond Tors fnaton swe ent yO) he tf cons vo) Ue): ter ee poe const any 20h tO Ca) Searels nd - ‘For example, the function 3n + 2 son 2 fag) ota + 6 100 fr — 121. 10n? +4 + 2 = O(n?) as 10n? + 4n s-oNe S Pain eens Fe 12 Te liuton betind © otto shown in 12 Forays ofnto the ight ofa, te alec) estoe 8 (ip Theta) Nonaan Te fae of £6) = 0 (6) ad 8 “torn sthea of gore) lace ess pov coms Cy Cy td ‘such that Cyg(n) < f(n) < Cye(n). Anton belongs oth fa) the ext poste contants and ssh hat canbe nice betwen Cg) nd Gg ites ape soggy Mithoush OCein)) is 40) Out) sels 13 ales inv plow of function fn) and g(n), where f(n) =e. Peralta of tte sight omg the vale of f(a) lies ator above C60) amor van Cyg0) In words, for all n 2 Mg, the fctn 9 cal ee) eins ctr set, we write ‘onsant We sya, go) i an asymptotically ight bound fr) 14 Analysis &Dasin of Agortim (@E, 1V-Sem) ‘The funtion a +2 ~O(a) as 3n-+2> 3 foralln2 24nd 39 +2 forall 22,0 C,=3, C4, and my = 2 3 £3 = O(n), 102-4 n+ 62 20 ~ (29, and 10 logn +4 = O(log) theta notation is more precise than bot the igo and omega notat ‘The fnction fn) = (0) iff en) s both an upper and lower Bound of (16, Give the definition of Big “oh” with example, GRY, June 2016) Ans Refer 10 QS (9), 17, Define 2 norton. Ans Refer 10 Q.15 (i) 0.18. What are the differences between bith (0), omega (4) and ‘them (notations? (RGR, June 2017) Ans, Refer t0 QL. 0.19, Why do we use asymptote notations inthe snd of lgorthns rely describe the commonly wed asymploc notation. (RGN, Dee 2018) Ans Refer t0Q.14 and QU. Q.20. Briefly describe the asympuotic notations nied inthe study of ‘atgorthms Also write the relational properties ofthe asymptote nai (GPK, Non. 2018) (GRY, June 2015} Ans. Refer to O15. ‘The relational properties of rea numbers ppl to apt ebm ‘Suppose that a) and (a) are asymptially postive. ‘Transiiiy — a) = ©fg(m) and a) = OCH) imply Ro) =A), §) = Ota) aod ata) = OC) imply a) - Orh(n), a) = 2¢0) and gin) = AH) mply ia) = CH), £8) = ofa) anata) = of) imply fo) = ohn), a) ~ ofa) and aa) ~ a0) imply fa) = ob). Reftexvity — 4) = ec), 4) = ffm, fa) = ae). . ‘n) = (g(a) if and only if gin) = @cAiny. eet Tyce tty =x, ee aa ae pec a or a ee eT a ae in) = a2 b, fa) “feta = 2 = fa) = o(0)) “. (0.21, Define ale “oh"t “0” notation. ns Tv asyptotic upper bound provided by ometation may or may ‘ot bomympioticlly tight. The bound 2a = o(e?) is asypttcaly tat, but {he ound 2n = ol) is not ews notion to denote th upper bound that isnot esmptaialyh, ‘We mally dene oe) (itk-oh of ga) the st ‘{a() = ff): for any positive constant C> 0 there exis constant > Och that 0 Ha) = gta) fr all> mh Forexample, 2n= of) bt 2 oh Tis fnetion —Ka)= of tt un f 2 Seu Example —The fnction 3a 2 = of) Since tin, 2832 ane 2-ctnboe0 3a +2 a(n og og 62 aa este msn nana sini Ti is tat nfo) Cg bon! OR) = Cu) os sme >t Sa and) Can ont se doable 8 Dope et BE, San) ‘Cation esta, fc Snca Kc) Scone! ini same) ccs nay tm 0g Set (02% Deine ee omer “0 aon fins by nos eontonisioS-otationas-ctatn 0a weet Sober tte toad tar oer ag hate mariag ean il ny fe) eta. Formally, We define wt) (itl-omep of gf”) a eat (= (0): for any postive costat > 0, te xin count ot vc 0 Call nM) Forexample, =0(0), but L#0(0") ais, ‘Thereation f(a) ofa) implies that im £00) ithe imi exit, ‘Thats becomes acini lage relative tg) asm approaches iy (023. Write in brief about the significance ofthe following notations) D0" (yO ii)" h) 0" () ‘@ (R.GPY, Dec. 2013) Ans, Refer 19.15, Q21 and Q22. (0.24. Explain te variation ofthe rate of growth of common comptng| Junctions. Ans. Tan complet of an algorithm is generally ome fneton oft lastance characteris. This function is very important in determing how the tine eguirements vary a8 the instance characteristics change. Als, the complexity function can be used to compare two algorithms P and Q tha perform the sime tsk: Now suppose tha algorithm P has complexity (0) ‘nd algo Q as complesity O(a. Here we can assert thet algorithm Ps fie than algorithm Q for suficienty Ineo. To coafim the validity ofthis assertion, obser that the computing tie of Pi bounded from above by ef for some constant cand for allm 2 my whereas that of @ is bounded from below by defor some constant dand all n,m 2p Aa en
1) and (Li) C 2B GOODHDODGHO6H9 @ ( Heap Sice 7 (0) Heap Sice 8 ig. 15 Creation of Heap Fig 1.16 show the an ‘20 Anais & Design of got (BE, 1V-Sem) sation of heapsert on the array’ ~ Prob. Mastate the operation of 1/5, 13,2528, % 17, 20.8.4) ae (RGR, Now 20 rows the creation of heap for the given list of keys — © 8S o> (a)Heap Sie! —(@) Heap Size? (0) Heap Si fog (0 Heap Sie 4 (Heap Size S Q Q Q@ O-Q YD OOO OOO (Heap Sie 6 Q Q © Q-Q QB OOOOOO0OO (a) Heap Sice7 Q Q -Q @ Q @ 6060 6060 QOOE © Oo (0) Heap Sie 8 ( Heap Ste? So. Fig, 1.17 sh 3 Fig. 1.17 Creation of Heap unt 34 INTRODUCTION TO DIVIDE AND CONQUER TECHNIQUE, "ANALYSIS, DESIGN AND COMPARISON OF VARIOUS ALGORITHMS BASED ON THIS TECHNIQUE, EXAMPLE BINARY SEARCH, MERGE SORT, QUICK SORT, STRASSEN'S. MATRIX MULTIPLICATION aac (0.32. What is divide and conguerstategy ? Wrive and explain the atgoritno of divide-and-congner (RGPK, Dec. 2013) “ans. Consider a problem P associated witha set S. Suppose is an algorithm which partons § into smaller sets such thatthe solution of the problem PYor Si tedaced tothe solution ofP for one or more of the smaller fees Then Ais ealled a dvideand-conguer algorithm. In othe words, vide-nd-conguersteatoxy suggests spliting the input into distinc subsets, <<, yielding ksubproblems. These subproblems must be solved sopaatly and, then 2 method must be found to combine ‘beoltons into a solution of the whole “The dividesnd conquer praigm involves thee steps at each evel ofthe Divide Divide the problem into number of subproblems ‘Conquer —Congue the subproblems by solving them recursively Ifthe subproblems sizes are small enough, just solve the subproblems in a Straightforward manne. ‘Combine — Combine the solutions ofthe subprblems into the solution forthe original problem. In eases where main problem and subproblems are of same type, then dlvide-ad-congusr prinipe is ntualy expressed by a recursive algorithm. “Three examples ofthe divide and conquer algorithms are (@) Binary seare algorithm (Gi) Mergesort algorithm Gi Quicksoet alg, Algorithm - By conto abstraction we mean a procedure whose flow ‘of contol iseleae but whose primary operations are specified by other procedures whose pecise meanings ate Tet undefined. DAndC (algorithm 1.5) is inal invoked ax DANG), whee P isthe problem to be solved, SmallP) i a boolesn-valed funtion that determines whether the input, size imal enough iso, ten he answer can be compited without siting Tt its value i true means problem is small enough and ean be easily solved, Iie vei fase means problem isnot small enough ad it ahold be Pattoned into salle problems ‘22 Anais & Deslgn of Ago (.E, 1V-Sem) ‘Those subproblems Py, PayooPy re solved by recursive applications Danic. ’Atth end, Combine i function that determines the solution oP the solutions tothe ksubproblems. If the size of problem P is and the sizes of k suproblems ae Ayo spectively, then the computing time of DAndC is deste thevecurence relation at ror fi +) er 0 isthe tet compte anne ey FO isthe ine for dividing Pand combining the cluon byob “TU isthe tine for DAAC on any input fs The complexity of many dviesand-ongeer alors sve reeurencee of the form 2 sal -#TI,)+4()othervin} Te) an{ Bett >t Here, and b ae constants. ere, we consider that value of (1) s known, and nis defined 35 ofb Ge, BY. “This complexity can be found by solving above ecurence relation: ofthe method is subsdtuion. Algorithm 1.5 Divide-and-conquer Algorithm 1. Algorithm DAndCrP) tf cari 3. iE Smal) then return S(P) aon = ¢ 6. divide Pinto salle instanoes Py, PyooPye K2 1 7. Apply DAndC to cach ofthese subproblems 5 return Combine (DARdC(P,), DAndC(P)--. DANS %) mo) Ans, Divide and Conquer Technique ~ Refer to Q-32. net 33 Algorithm 1.6 Recursvely Finding the Mastinum and Minimum 1. Algorithm MaxMin(j, max, min) 2, [fa in] i global ary. Praeers i andj are integers, 3, 1181S} Sn. The effects to setmex and min othe 4. (largest and smallest vals in ij), cept. 5. 6 HG~}) then mex =n af Sour) 1. elseif (=j~1) then 1 Anoter case of Small) Bf 9 it (fe < ap then 10 { n, max =f mi 2, } 2, le 4 a 15 sax fit mia =a 16 ) oe 18 ele 19. ——_( IEP isnot mall, dvde Pinto subproblems. (1 Find where to spt the et 21 id= LG+ 25 22. 17 Solve the subproblems, 2. MaxMin, mid max, in); 24 MaxMi(mid + 1, j, as, in 25. 11 Combine the solutions. 26. ‘(wax < matt) then ax = mast 2. ‘fin > mint) thew min = mins 28 29) 234, What isthe dete stracteres nsed to perform recursion ? (RGR, Dee 2016) ‘Ans. Sack is usd in eeurson. Because the stack have the propery of lastin fst out (LIPO) which is required to stor the curent ste of fonetion ‘snd jum to new instanceof that fection. So that when the execution of new Instance get over, i jump back to ts previous instance Unt-1 95, 4 Anatals& Designo Alrite (BE, V-So) ‘Table 1.1 Three Examples of Binary Search on 14 Elements iy inary search cig 93. pln bey inary sare serie herb tell a ee Satie ny mieten aay =e ncn tapes he 7. eee ec problem of nding whether given element bb A eee an dons 8 el io Se bin nite en o. See ie pcocsuniomicaroeanaant i a Sh ee of done nde Set er “et Sree tagn reget pines Ol ee ee an Sata beeen ote wii i eet gi aL enerteeenions an wba See lp aaeeeedien eniaena zi Taipei neseeenputinardahensers a Grats linanethopcben Pn ted ed ree beeen Poe g sa ntaee Patan ve beet Sa (Gi) x> ay: In thie case the sublis fo be searched is ag. reduces 10 (= et mn 84 In this example the problem P ges divided ito one new Sup “This dvision takes enly (1) time. After a comparison with ay the remaining tbe solved (i any) can be easily solved by using divide ‘eonqur technique again. fq is always chosen such that clement (tht ie qn + 1)2), then the resting search algorithm is bry search, Here pont fo be note thatthe asier tothe new subj (038. Whats the complexity of Binary search algritin ? Ars. inary susch ecniqu bse tase when data in nary is ore in increasing numeral deo esuvaleily apts “The compleniy of any slur i messed by the pumber (9) of comparisons to lorate ITEM in DATA whete DATA cota scents "ni ech ch onpron ae sep Se rl Suppose) the maxima numberof comparisons reuied to ocate ‘TEM in DATA, where “ mart Sabotheanvero he ogaroblemB hein rary combi 220 nr (0.36. How divide and conquer technique can be applied fo binary fe) = ogg +1. ‘nthe worst ine complexity i approxitely equal tology. Inthe bs icp nb en 0) ' in the best eave, Wend hat he at em for hich we ae searching ‘she idle or af Be det sep * So inthe ist comparison we will elt wih requied locaton, ‘MID of dat segment Ste bet ssecompleiy i a ‘Als, he average ease comply in scressi search canbe given at tig Wie in evecessul seach complenity nal eat wee eet Also wrt algorithm for divide and conquer, (RGRY, June 2015, Ans, Refer to Q35 and Q.2. 0.37. Give example of binary earch fchnigne. we have the following 14 daa ites — ~6,0,7,9, 23, $4, 82, 101,112, 125, 131,142, 151 and ‘hem ina [1:14], and simulate the steps thst BinSearch goes through seach fr dient also. The vals lon high and mid esl oe 2 WN a exse is og cep real ohne ee ea ml 15 "Wand oan eB, ny ey git nd pesca ‘explains the traces of BinSearch on these thre inputs ‘unsuccessful search) can be given as ~ Ans, 428. Anajals& Dei of gor (@., WV-Som) ‘Successful Searches = Best Case | OI) Average Case O(log} Worst Case Oflog Unsuccesfal Searches = Rest Case flor a) Average Case flog] Worst Care flog} 9 nplnent neler for bay serch Discs nd since of Snarycaeh agai LG, Mey innit fe grt fr Bary Search Refe Te Compe of Bary Sch Art ~ Refer 6 O38 (248. nd explain an lei o erch on tem nar ode ont comer ire th complr Og). PY. De de Reero Q32 and O38 (24 vethedideantonuersolutnforbinesarchan icone. ‘ac ey vis eer 035 and Q38 (2. Eaplla dean cone tenigne, Desig Bec stot foray ere ‘man ae 200% “ne Dien Cone enn Ree0 22 eerste lgrt fo Binary Senshi 7 dso sexsi bias) sah oct, vite Dino a tg tain tyke Sek, 8), rit 1.7 Recursive Binary Search 1 Algorithm BinSrh (i 1,9) ‘Given an ara afi: of clement i nondecreasing. Order, 11-1, determine whether x spresent, and ‘Vso, return j such thatx =a else return 0. 2 3 st 6 if@=9 then 1rsmall(P) 7 8 ° € (= af) then return i ‘se rtum 12, W Reduce P iat a smaller sbpeobem. 1 mids (4 9p Tk. if (=e fi) hen retum mi 1S. else if(x~a fri) then 16, roti BinSreh mid 1, x); 17. ele en Bins (a mid, Is.) By (043, Discuss in Which condition binary scarch beter then tncar saree GP, June 2010) ‘Ans Binary earch sa ffcent searching metho. While sashing the emen using this meod th most esental tigi that the clement the ray should Be sorted one. The binary serch algoritim becomes rely ‘mor ficient when the number of emponetis onthe ti lrge Boney ‘cach is more ele thn linear serch becanse it requis le mb ot comperiage, Sequential seh or tncr sear) eaeyo write and ffciet for short lin, a disaster for long one. Img tying to find the nm as tcp telephone bok hy resding one name atime a the fonto the book. To find ny tom in a long sorted Hi, thae ae fr moe ecient methods, Ooo the ‘esis bony seach in which fst to compare them wth one in he ctr ofthe ist andthe etict ou tention to oul he int or Second bal ie is depending on whether the item comes before or afer the ental one Tn this way at each sep we rede the lena ofthe li fo Be seated by hal Jnenly twenty conan the mst wl eat ay reed ae 244. What are the limtation of binary search elgerithm ? Ans, The arth requires to ceitons— (©The ist must be sorted and (0) One must ret acces to the mide element in any subi. {This mics tht one mast eseatially use a sored say to hol the dt ‘But ecping dn in «sorted anay i nomaly very expentve when theve ae ‘many insertions and elton. n such stuns, accordingly ont may use 8 ‘iferot data sete, ike inked isto Binary search we, to toe te das, 45. tite a short note om mergesert (GRY, Dec. 2016) Ans. MERGING ~ Suppose irre list with «elements and Bis ¢ Sorted ist with semen ‘Te operation that combines he clements of and B ino singe sorted "st with =r elements is called merging. nays & Designo Aarti (BE. V-Ser) _ “The morgesort closely follows the divide-and-cong resort ~ The mergnsontcotld parodia Intivels, operates 2 ll Divide - Divide he a leans sevens © Be sort in uboogpences ofr cements ea “Conquer ~ Sor the two subsequences recursively Using MErBESor. Combine ergs th two ote sbsequence produce te sored am Fore, one thing iso be noted thatthe recursion “bottoms ou” when sequences to be sorted has length 1, in which case there js no work crete every subsequence of fengh 1 is already in sorted order. Merging of two sored subarays ean be done a follows — ‘Suppose nei given two sorted decks of cards. The decks can be mer as shown ing 1.18, ai =H co Be C © @ Fig. 18 Merging of Two Sorted Arrays In each step of merging two sorted subarays, two front ear ‘compared and the smaller one is placed in the combined deck ‘As in fig 1.18 (a) oat cards 22 and 33 are compared, but 22 <33 | fs placed on combined deck. Unt-1 99 In cach step we do the sme, in net stop we compare 44 and 33 and 44 > 33,2033 is placed inthe combined deck se shown in ig 1.18 () an 0 on. ‘When one of the deck is empey ll the remaining cards in theater deck are put atthe end ofthe combined deck. "And finaly we result with aso array. 0.66. White Mergesort algorithm and analyse your algorithm of dierent case. ‘Ans, Mergesot lgovithn i given a algorithm 1.8. Algorithm 8 MergeSort Algorithm 1. Algorithm MergeSor(ow, high) 2. (fallow : high is a slobal ray to be sorte, 53. //Swall(P) is tue i there is oaly one clement 41 srt I this case the lit is already sorted. ‘if Cow < high) then / If here are more than one clement. ‘ {Divide Pinto subproblems. % 1 Find where to spi the st 5 6 % 10 mid = [dow + high)2 1. Solve the subproblems, 12 MergeSor_low, eid); B -MergeSor(mid +1, hh): 14. Combine the sottions. 15 ‘Mergetloy, mi, high; ey my Algorithm of merging two sorted subaray given in algorithm 1.9. ‘Algorithm 1.9 Merging Two Sorted Subarrays, Using Auxiliary Storage Algorithm Merge(ow, mid, high) ‘altow : high) i global aay containing two sorted ‘subsets in flow : mil) and in afin +1: hgh. The goat ‘to merge these two ses into a single set resi ‘mallow igh. fs an asin lb any slows i= tows} = mid +1 ‘ile < mid) and G = bih) do i «0 avast & Dosign of Ago (BE, V-Sem) 1, fh] $f) then ne ¢ 12M ealhhe n+: BO) 1. ee sf i tm eo wo wy 20. ih> mid) then 21, fork:=} to high do mt 2. Bimal i a) 2s. eke 26, fork:=hto mid do on 28 Ml alk iit: wp) 30. fork = low thigh do afk] = BK: E,XA MBLs B in alphabetical order using mergesort GRY, Mey Ans, Mergorort Algorithm ~ Refer 6 QA. Ecioney of Mergesort Algorithm — The computer takes ti produce the output for a piven inp, scoring 1 the designed agorith| ! specifi task. The computing efficiency is more inpertant Tor an al iteneulate te otal ime taken by theconiputer fo perfor a task. Fore i eomputer A is 1000 times faster than computer B, msans compl process | bilion of instuctions per second, and computer B milion of instntons per secon. If we run the selection sort alg ‘heorderof growth ant andthe another sting algnrithm called the me algrithn’ withthe order of growth at n logan. The mergesor algorit ‘bemot efficient as compared te ay other algorithm, If we ran bth al {0 so I milion of vas in an input array, the mergeaoet agit efficient, i as Iss time and produce the output Unt 44 Let we have sort ta amy BAX) AM BLE Pass 1 | Pass 2| rass3 [REELMER] Fialy he ot data 6 AEE, LM, FX. (Q.8, Evaluate the time complet of mergesort “Ans In mergesont ou ecurence-based analysis simplifidifwe assume that the orginal problem size ba power of Wo La, a~ 25, Where nis toa ‘number of elements in ara, and kis some constant. ‘ach divide sep yields two subsequences of size exactly 2 Mergcsort on jst one element takes constant ne. But when we bave n> F elements we Brak down the running ime a fellows — Divide The divide step jst computes the mile of the subsea, which taker constant time, ie, @X1) when 2 = Conquer Were sle wombs sachs wich combs 27(2) te ing tine ni) ore ambine- Mere poo onan alone ary sti OD, 9c) 8 $0, the Woot cerning ine To ea soi en a shania We soive above complexity by sutton method so at find acts! complet (1) and (indicate sie constant vale, with @() some function of [et Cn. So, wo can write complesty faction Ta) as fllows — Put =a and Ofs)=Cn ww {efs}oen - 42. hosts & Designo Alon (8. IV-Sem,) Here, aand C are constant and 9 = 26 Toye n(3}+cn Substituting value of 13) ‘Agsin satiate vate (2) To)= afm Te) 2S) 3e0 (hmm set de reo (of) oe-en nina }one Woknow that n= 2 Subst value on ak Toya2t | FlekCae 271) + ken We know that when 1, Tne 8, Te) darken, c) a2 | Te) or. we can write Ta) Ofnogsn) ie, compleity asa function of logy So, the complexity of mergesortalgoithm can be given as — fniogs 1) 0.49, Explain mergesort algrithe wih example Ans, Refer © 46, ate have to sonata ay A= (5,254, 61. 3.25.0) by applying mergesor algorithm, ‘This ean be don in he flowing tps (©) Divide the aay in sv0 subarays of ie 2, b, 4 ‘We result with to unsorted subazays (65,2, 4, 6) and (,3,2,6) (Now, senin apply meresort for song subarays separately, ‘esting in ansorted barry 6, 2, 4, 6) (1,3) and Q, 6. Again eply merges on above oursubarays resting sored atarays of ength Ion (9) @ (@), (6), (D, BQ), and (6. Ge) Apply meeuing of subareys form new sored aay as shown ing 1.198. 0, n+ aC. boge NN LN ape aa pa 8 119 Mert ose Saber, (0) Hnatyve hed wy Om asee 44 Anat & Dosign of Agr (BE, IVSem) nes 48 9. Defeat ean merit aertin. GRY, Janey cfr to QS an Divide ~ The aay Alp. #1 prttoned (eseiges) ato eran ace vo nonempty SUPA AIProccnnnnnag) and AUG’? lene en ote oe acne eh mergesort algorithm one guch tht exh lee of Alp. tan or eq each cnet wf The ides is ompsted 2 pat ofthis partioning clement sequence s Of lg n), even when mis not « power of (RGR, Dec. 2 Ans; the nef he merging operation i proportional then computing time for mergesor is deserbed by the recurrence tltgn or Ala procedure ‘Conquer ~The two subarys Alpen) AR8 AQ + Boost re sone by rouse calls v0 quckson. ince the subarrys there in pace, no work i needed to a atla seoman ‘Combine TO)" lana/2)+en n>, ¢ aconsant combine them, The entre aay Alport) i now sorted, es yang at sound i ( 34 apn how app te die and cones sorig (miming eens oar Te laa a ee feos ‘c(t log (2) matting in he ecrence el Tin) 2(¢ (2) og 72) on $ enog (a2) +n = calogn en og 2-0 = on log— ent = cn logn Toss, Tia)= O(a tog) 052, What rth nitions of basic mergesort agro? 4 On compli we might rae consring merges is 2a loins. The ainda Toetons were nected Heat we tld ‘ssiombly ere to ted min place Bu epee coe eal mst sl ork rd ncn hea placed lao ‘ack no aow:highon cach alo mage An seating opr toassochte anew ef nfrmation wih ach ey. Thi Becher the Keys and any asocaed information lope it # toned at The ering ofthe sorta ia process by changing thle aes sno ‘cede moved al Ald tha cots onlin wil eral besa shan nents eco 0 es sac wl Bee. -Asote:cmpain we coud ie abot mers ihe ck space is nce ty th we of ren, Since mets pla sah {we aprons equalized subse the maximum dpth othe sack roportom! lg. The nal frac spe sen ned byte evn mame in which slr wa devise. The nee for sack ee] 4b ee and ate all oe ements inal. can be eliminated it we build an alot that works Stoop. 5 2.53, Writea short note on quicksort. (GRY, May 218) § & : ‘dns Thedvide-andconguer pprach can beusedto ave stn cient sevtng method quickaort In quicksor, the division ito te sabereys is riade 80 that the sored subarays do not need to Be merge ltr Ths it “ccomplihed by reseanging the elements al) sch tha a] ©] iv betwen {and m and allj between m + nd for some, . repeat w 1 repeat 2” isis 13. until (2 w 1 repeat 1 fiejns 16. unt af 59) 17. WG <3) them latrtange (5 18. june >; 19. sm) = ai ab 20) return js Algorithm 1.12 Swapping of Data 1. Algorithm lawrehange (1,3) 2. Exchange al with aj). omer ‘ume Complety of Quckor lg he rming ine of QikSo eprom wheter he prning is sannud or ubeloned ad his nton depends eh ame oe ening (9 Worstcse Pertoning ~The worse behaviour fr quick ose when th pani ote prods one ren Wi ‘zens nd on nth ony 1 pet. Lt seme a te tne farioning arte sever softest, See tong on 00) Te and) =) he xtecs forthe ning i ‘roy = Te) +00) Tocsta ieee obser aT) an hen ete — "Te)= Tr 1) +86) 2 ou) w1(§.09}-10 Fig, 1.20 shows a recursion tee for this worstcase execution of Quieksor Ths if the partion saximalywabalancod a very recursive step ofthe algoritty, the runing tine i (0°). (i) Restense Partitioning ~1 be paitonng procs rodces ‘07 regions of sie, QuskSor rus much ist. The ecuenc is then Tio) = 2T(92) + 90) lation Tea) = fog 0) hic has 4. Anaysie § Designo Alor B.E, 1V-Sam) ig 121 show the reeusion tree for thisbest-usr execution of Quik Fig 121 ly Batanced Partitioning ~ Suppose the parttioning algorit swaps produes a 0-1 proportions slits Then the fecurene relation ‘T(o)= TOW) + To10) +2 ‘ofthe running tne of QuekSar, where aie placed by for conven Fig 122 shows therecarson te for this recurrence, Not that every vl tho uce has costa until a boundary condition is reached a depth og log), and then th eves have coat at most n. The recursion terminate ep logg98 = og 2) J lessee Fig 122 nt 49 ‘The total cost of QuikSot therefore On log a). Ths, with» 940-1 onal split at every level of recursion, QuickSort in (alg te. ta lt even a 9940-1 split yields an O(a lg ») soning time, Therefore, the Jae tines fn 10g a) wherever the spit has constant proportionally. (0.56 Whats the running time of uichsor when al clement of array A have the sume value? (RGPY, Dee 208, June 2010) “Ans Hal lent ofthe aay Aha the same value the partion operation | ining =f] 2) + 0, ning ine ofthe Patton operation is O(n) And we uray omit clings fe ane and foos, hen tbe recur for raming time wil be Ta) = x(3) +0¢0), ve eet sch firm of reuences ad know it Tie) ~ Oe en) or sci avecuece, Lat prove this bound ‘sume ‘T(a) cs (fm nll than the given, then ra) <2(02.t52]+ 0(a) ~en ign —en + O(0) = on gn co Peace (37, What are the advantages and disadvantages of uicsor? Ans Advantages of Qulckort {@ Theres pod compromise etncen number of comparisons and eta movements {@) This method takes the advantage of inal ordering ofthe ist, (Gi) Average comparions az le. Disadvantage of Qulesort— (@ The behaviour entcly depends wpa the section of vot he item whee the it parton. (G Ips ot patting te tint approximately equa af, he pertormancedepades sharply (Gi) This etd tes exuson,Recuion is tine consuning {) The swaps ae defintely aetr than Selection sort swaps 05k How many average comparisons are required by the following sorting eigorthms to sort the oly k highest clement, out of lements? Bubble sort, quicksor, insertion sort, mergesort, heapsort. (RGR, June 2012) 0 anayets & Design of Agar @E, W-Sem) dns. Te bl 1.2 presents comparison of various sorting meta | ‘Table 12 Ager a ube sor a Insertion som | 04 kaart OED ‘Meas ‘DinToe, AT Tes Dino ny (039. Show thatthe second smallest of elements can be found nllogn) 2 comparisons ne worst case. (RLGEV, Jane “Ans Cosiee, tt i tt ui “Ths, we can hu teste set - conpartons. The eteapson chore haghsige), Woem pac scond ale inflge] compute, wal ops vf (26. Whar te pups of ravens mare matt wnt De dz he pag of Sn’ ati mui cel tne oly oan mao se op ‘Somber ura mln esc amaseete ste ‘minone) han etn oa Say na cae OO 241th str note on Sree’ milton, Compa it omental is nd sna Coa et neal ‘ear te or Explain th Susen's muliplicaton technique. (GP, June 2007, 208 2010, 201 Write down Sasens bron for muplction. op GE ec. 2006, 1) Discas Sasson’ algorithm for matt: mulation with (RGRK, ne tt 54 on explain Sirssen's matric mallplication withthe help ofan example (GPL, June 2017) or Explain Stassen's matric maliplcion geri (GRE, Des. 2017) on ite sor note on Sass mab malic CP, Now 2018 ane Let A nd 8 B80 A mais. The rofat main C> AB i sua ann mtr now ee foe aking teen SC erow af Andes ofan he ling em ow Coi= SAG BHD fora and bewseh and ‘owt compte € (3) ing hi formas we ne 9 mtipeons Sette cr lant ie ortega raileton ‘lit hich we tr ste coment med i (7) “ie ved-ones satay sags oaths way compte the oO) Bee Por snp astune thn n xt power of, Le the exis 8 scnncpane mepr sch tt 026 cars atv oe en ‘Souhovs ei colasof esc te sed th Rand Ba he ‘Siting meson ses pone af we, Spaeth ad 8 oe ee Fete sto ure ne et ean hv incens ‘+4. Tene prose AB can be onthe sve {he product of 2x 2 matrices — iF AB is [fe A] Be] [Eo 3] a Aa An, es oi [es Cn, » mise Cy = An Bu +AnBa) Ci=AnBa+AnBa i) ay = AaB) +AzaBar Cx =AniBin+AnaBa en = 2, the formulas i) and (i) ae computed using a multiplication ‘operton forthe eloments ofA and B. These elements are typeallyoaing Point numbers, For n> 2, the elements of C ean be computed using matrix

You might also like