You are on page 1of 78
Bess Se cies ~e - 9 x © Olie cieerhenrDniioneint pat UNITI INTRODUCTION TO ALGORITM DESIGN WD ipceom PH Cm o Goa ree Menta 8 Be a 40 2 EN Scanned with CamScanner Bess BT cies ~e - 9 x Ole cieeeheerDsiionseint pat ae £0 © Mare Algorithms © A finite set of instructions or logic, written in order, to accomplish a certain predefined task. © Algorithm is not the complete code or program, @ Itis just the core logic (solution) of a problem. © Can be expressed either as an informal high level description as pseudo code or using a flowchart. WP Bete tosn Tee vencaay A Bm a SOM Scanned with CamScanner Bess ST cies ie - 9 x © Olie cieerheer niin pat ae £0 © Gasenee Characteristics of an Algorithm Input Analgortm shoul hive 0 or more wal dtined inputs ‘Output ‘An algorithm should have |r more well defined outputs ‘Unambiguous ‘Aorihm shoul be dear and unambiguous Finieeness Algorithns mus terminate ater anit no. of step Feasibility Should be ease withthe arable resources Independent Analgorit should have step-by-step rections which should be independent of any programming code. BP trem = DH Geom wee venom A Be ae ee BOM Scanned with CamScanner Bess ST cies ie - 9 x © (One Cini dairsintat a *) ¢ 0 © eee Pseudo code Ic is one of the methods tha could be used to represent an algorih Itis not written in a specific syntax Cannot be executed Can be read and understood by programmers who are familar wih diteran rograning engages ‘Transformation from peeudo code to the corresponding program code easier. Peeudo code allows to ince control structures such as WHILE, IF-THENAELSE, REPEAT-UNTIL, FOR, and CASE, which are dab ray high eel lage: E Pimms DH esac Oeeoa A 2c Menycy 9 BM ae oe aan Scanned with CamScanner Bess HT 0 cies ie - 9 x Qe LOO Memmi: © Olie cieerheerDaiioneint pat * Difference between Algorithm and Pseudocode ‘Afinte setof ington or loge, a gener way of describing an algorithm Vien ode, to accomplish waht using ay specifi roramming ‘ai predefined sk languages etaios jst te core gi (oluion) of is an ule of progam, writen na om pote which can emly be converted it el programming statements Easy o understand the logic ofa Con be read and understood by programmes problem who ae friar wat dierent programming Tengu. ‘Cant express ether at an informal Itisnot wien na sos ya: allows to high evel description as pscado code include contre sucesso as WHILE, ‘orusing Mowat. P-THEN-ELSE, REFEAT-UNTIL, FOR, and (CASE, bic re rest ia may igh level ize wencn BR a SPAN HD Typerere rosea Scanned with CamScanner © pneu res 2 - 9 x © Ole cyeerdenrDaunionsint pst UNITI INTRODUCTION TO ALGORITM DESIGN WP ipo: PH Cm ao Goa ree Menta 8 BB 00S Scanned with CamScanner wise 6 oar canoes Ofte cyeenenDoanonsnt pat cere) PD Wpereretoscneh - 8 x ae) + 0 0 Gea Why to design an algorithm? ‘© General approaches to the construction of efficient solutions to problems ‘They provide templates suited for solving a broad range of diverse problems. ‘They can be translated into common control and data structures provided by most high-level languages. ‘The temporal and spatial requirements of the algorchms which result can be precisely analyzed ee oe | Scanned with CamScanner Bess Boia canoes © Olie cieerheeDaanensint pst HED Typeneretoseac ae) 4 0 0 eee Algorithm Design Approaches Based on the architecture @ Top Down Approach @ Bottom up Approach ae Scanned with CamScanner Spx neesieas Bonin cenoe x © (On CHaereniSoiesinit pat ae) ¢ 0 0 eee = wntttpat Algorithm Design Techniques 1, Brute Force To solve a problem based on the problem's statement and definitions of the concepts involved. Easiest approach to apply Useful for solving small - size instances of a problem. © Some examples of brute force algorithms are: Computing a” (2 > 0, a nonnegative integer) by multiplying a*a*...%a Computing n! Selection sort, Bubble sort Sequential search Bice QP en ao Dena A Bem ae te MM my Scanned with CamScanner Spx neseees ee Ole Ciera Deanonsint pst = wntttpat Bicone QDHCsaOeeee 5 x ae) ¢ 0 0 eee 2. Divide-and-Conquer & Decrease-and-Conquer Step I Split the given instance of the problem into several smaller sub-instances Step 2 Independently solve each of the sub-instances Step 3 Combine the sub-instance solutions. © With the divide-and-conquer method the size of the problem instance is reduced by a factor (e.g. half the input size), © With the decrease-and-conquer method the size is reduced by a constant. Scanned with CamScanner Twc vain AOR w ae Bess res 2 - 9 x © (On CHaern/Soieninit pat ae) 4 0 0 eee = nittpat Examples of divide-and-conquer algorithms: ‘© Computing a” (a > 0, na nonnegative integer) by recursion © Binary search in a sorted array (recursion) © Mergesort algorithm, Quicksort algorithm recursion) © The algorithm for solving the fake coin problem (recursion) Tiwe wencoay BR a SSN WD Typeneretosea Scanned with CamScanner + Bess ee res 2 - 9 x Ole cyeerdesrDounionsiunt pst writ pt 3. Greedy Algorithms "take what you can get now" strategy © At each step the choice must be locally optimal (© Works wel on optimiziion problems © Characteristics |. Greedy-choice propery: A global optimum can be arrived at by selecting a local optimum. 2 Opeeal substructure: An optimal solution to he problem cons an optimal soluson osu problems Examples: eur gaming ree Sores dance nhs Grady asin othe Kappa SE Pimcmeoes GDH es sSOGesa Are Menycnety 9 BM a i diva 9B Scanned with CamScanner Spx nesees oe Ole cyeerhenrDounonsint pst = sntttpat WD Typeneretoseac - 8 x a*) 4 0 0 Gee 4.Dynamic Programming «Finds soltions to subproblens and stores them in memory fr liter use. © Characterses 1. Optimal substructure: ‘Optimal solution t problem consists of opcialflutons to subproblems 2. Overlapping subproblems: Few subproblems in tea, many recurring instances of each 3. Bottom up approach: Solve bottom-up, building a able of solved subproblems that are used to solve larger ones. © Examples: onacci numbers computed by iteration ‘Warshlslgorth implemented by keratons Awe vc Be cen me BA Scanned with CamScanner - 8 x © pseu Bomar cenoe x Ofte cyeenheerDoanonsunt pst Qe SO © Metra 5. Backtracking methods (© The method is used for state-space search problems ‘What is State-space search problems State-space search problems are problems, where the problem representation consists of alse) 1 cot ncton~ eats te cont of th pert opto) ‘ly fincon elutes tow dose a en se ote gal sate options) © The solving process solution is based on the construction of a state-space tree a (©The solution is obtained by searching the tree. ahha © Bamples: fe 5 6 WE) Sehcetoseach Geom nee weyanay 9B am a0 SM Scanned with CamScanner + Bess es 2 - 9 x Ole CyeerhenDounonsint pst QS OO Retoranne = sntttpat 6. Branch-and-bound ‘©. Branch and bound is used when we can evaluate each node using the cost and utility functions. © At each step we choose the best node to proceed further. ‘© Branch-and bound algorithms are implemented using priority queue. ©. The state-space tree is built in a breadth-first manner. © Example: B-puzzle problem, N queens problem 8 Soom a mare A Bm a oe Scanned with CamScanner Bess es 2 - 98 x Ole cyeerdanDaanonsiunt pst Qe LOO Mitemmme ize weno ee ae Sm Scanned with CamScanner Spx nceseees res 2 - 98 x © (One CHiern/Soieninit pst a*) + 0 0 eee = wnittpat Recollect -__ @ Different design Approaches/ Design Paradigms Brute force Divide and Conquer (Unit) Greedy Algorithms Dynamic Programmi Backtracking “Unies Unies WP Bete tosnh Tiwe wemcoay ww aed SIN Scanned with CamScanner Bess es 2 2 © Ofte cyeerbenrDaunonsnt st <5 ~ UNITI INTRODUCTION TO ALGORITM DESIGN WP ipcteoms QP H Cm & Goa ree Menta 8 BB a 40 2 ER Scanned with CamScanner © execu + Bonaprocno x ars - 9 x Ole cyeerhenrDaunonsiunt at a £0 © Mae Algorithm Analysis _ @ Analgorithm is said to be efficient and fast, if ic takes less time to execute consumes less memory space. ‘© The performance of an algorithm is measured on the basis of Time Complexity Space Complexity BP icmom Qi Cn ao Dx many AB ee ae OTM me Scanned with CamScanner Bess ee es 2 - 9 x Ole cyeerheeDoanionsnt apt ae £0 © Maram © Space Complexity ‘The amount of memory space required by the algorithm in its life cycle, ‘A fixed part For example simple variables & constant used and program size etc. A variable part For example dynamic memory allocation, recursion stacks space ete Space complexity S(P) of any algorithm Pis Se)=C+570) ‘Where Cis he aed put S(t variable par of the ago MD ineracrosae Ti wee wasyonay A Be ae MS Tm Scanned with CamScanner © pneu Gunman compote x ao 2 - 8 x © (One CHiern/Soienint pat ae) 4 0 0 eee © Time Complexity - T(n) “The amount of time required by the algorithm to run to completion, ‘T(0) can be measured as the number of steps, provided each step consumes constant time, HD Typeneretoseac eee | Scanned with CamScanner Bess 6 Snap ocnoes Ole Ciena Daanonsint pt 2 - 8 x ae) ¢ 0 0 eee Algorithm analysis © The worst-case complexity of the algorithm is the function defined by the maximum number of steps taken on any instance of size n. © The best-case complexity of the algorithm is the function defined by the minimum number of steps taken on any instance of size n. © Finally, the average-case complexity of the algorithm is the function defined by the average number of steps taken on any instance of size n. Biren QP Cn ao Dx many A Bem ae te OTM my Scanned with CamScanner © pune +B onaareceom x 2 - 98 x © (On CHierniSoieeintt pat ae) ¢ 0 0 eee Mathematical Analysis ~_ @ For Non-recursive Algorithms ~ «There are four rules to count the operations: ‘© Rule I: for loops - the size of the loop times the running time of the body * Find the running time of satements when executed only once {Find how many tmas each seatement if execited Rule 2; Nested loops «The product ofthe sizeof che oops times the running tie ofthe toy Rule 3: Consecutive program fragments The cota running ume the maximum ofthe runing me ofthe inal agen Rule 4: If statement (The running time is the maximum ofthe runnin times of if seme and ‘else ste HP yeheretosenn 2c Metra 8 Bee a0 ET Scanned with CamScanner © px nceseees + Bona ocro x ars - 9 x © (One CHeesn/Soienintt pst ae) 4 0 0 Ree for loops < msi) /11 = 0; executed only once: O(1) Hi sn; Limes O(0) His n simes O(0) 11 total time of the loop heading: HO(1) + Of) + (0) = O(0) sum=sum +i; /fexecuted n times, O(0) ‘The loop heading plus the loop body will give: O(n) + O(a) = O(n). iF |. The size of the loop is n (loop variable runs from 0, or some fixed constant, to n) 2. The body has constant running time (no nested loops) Loop running time is: O(n) .,,,. ¢ Eire DH ess Oooo Arc Menyeny 9 BM ae oe away Scanned with CamScanner Spx neesseaes +B onaeceom x ars - 9 x © (One CHeesn/Soienintt pst a*) 4 0 0 Gey Rule 2 : Nested loops sum = 0; for( i= O;i< nc i++) for( j= 0:) key) high=mid-1;, Teme ae me eel Scanned with CamScanner Biseseus 6 Gian oanoes © Olie cileerhenDaanonsint pt PD Wpeheretosener re: ae) 4 0 0 eee Worst case analysis The ey fenot nthe sear Let T(n be the numberof comparisons dane in the worst cave fran array of 2 9 For Then Tea) = 2+ 70/2) S24 24N) /[2Heraton 24242 Ten? Sedtteration + TEH/29 1] erton Note that k=logn, and that T(1) = 2. So T(n) = 2logn + 2 = O(logn) Dwc vain Ao Rw ae Scanned with CamScanner Bess 6 bap eanoes Ofte cient Daunionsint pt in ij temp; forliH0; isn; t+) { for(=0; j a+!) { a*) & 0 0 eee ce Scanned with CamScanner ees 6 Sip ocnoes Ole cyeendenrDounonsnt pt WD Typenere tose x Qe) + OO Germ: Worst Case: In Bubble Sort, n-! comparisons will be done in Ist pass, n-2 in 2nd pass, n-3 in 3rd pass and so on. So the total number of comparisons will be (n-1)#(n-2)+(n-3)+....4 34241 Sum = n(n-1)/2 Hence the complexity of Bubble Sort is O(n’). Best-case Time Complexity will be O(n), it is when the list is already sorted. (I cs Scanned with CamScanner Bess ee es 2 - 9 x © (One CHaeren/Soienint pat ae) + 0 0 eee Insertion Sorting Analysis inti, keys it ise: while(i>=0 && key < afi) { ale!) = af } alj+l] = key; 2 © Worst Case Time Complexity : O(0"2) © Best Case Time Complexity : O(n) © Average Time Complexity : O(n*2) 2 oiceoa: GDH Ce eOGGoa 20 Mey deuty 9 8 meg ca EA Scanned with CamScanner Bess Ce es 2 - 68 x Ole cileerhenrDaunonsiintt pt UNITI INTRODUCTION TO ALGORITM DESIGN HP Bete tosen oom ee women Be ad SB Scanned with CamScanner Spx neseees 3 6 Sian canoes © Olie cyeendenrDounionsint st Zim QP en ao cman ABE ae te MO me - 8 x ae) ¢ 0 0 ey Asymptotic Notations © Main idea of asymptotic analysis To have a measure of efficiency of algorithms That doesn’t depend on machine specific constants, That doesn't require algorithms to be implemented Time taken by programs to be compared. @ Asymptotic notations Asymptotic notations are mathematical tools to represent time complexity of algorithms for asymptotic analysis. Scanned with CamScanner Bess ee res 2 - 9 x © (One CHieren/Soienintt Spat a) ¢ 0 0 Gee Asymptotic Analysis @ Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation. © Notation Notation @ Notation Zim Qi Cn ao Dena ABR ae te MO my Scanned with CamScanner ~ esesws +e tnaaeon x tor ars - 9 x © (One CHaern/Soienintt Spat a) ¢ 0 0 eee e Big Oh Notation, O It measures the worst case time complexity or longest amount of time an algorithm can possibly take to complete. O(g{n)) = (4): there exist positive constants c and nO such that 0 <=(0) < cx(n) forall n >= 10} fi) = Os) EB oinmome GDH Cua Oeee a 26 Montane AB Sm a cd SEN mw Scanned with CamScanner Bess ee es ars © Olie cieerhenrDaunionsiintt apt a% £0 © Mary 0 <= f(n) <= cg(n) for all n >= nO} I f(n) = 3n42 Iff(n) = 3n+2_g(n)=n? g(n)=n Then Then 3n+2 <= cn? 3nt2 <= cn For instance c=1 For instance c=4 n=1,2,3,4,5,6... 3nt+2<=4n 3n+2<=n? n>=2 n>=S Hence f(n) = O(g(n)) Hence f(n) = O(a(n)) EB oinmome GDH Cua Oeee m Dw wary ABO aw SS RM Scanned with CamScanner + Bess ee es 2 - 98 x © (One CHaern/Soienintt Spat a) 4 0 0 eee (K ‘ ~~ @Q Notation: (Best Case) Q notation provides an asymptotic lower bound | 2 (g(n)) ~ [f(n): there exist positive constants cand nO such that 0 <= cg(n) <= f(n) for alln >= n0}. Jin) = 2m) rec Metra 8 Be a0 oe EE Scanned with CamScanner Spx neseues ee re ~e © Olie cileerhenDnanionsintt pt ax) « 0 <= cg(n) <= f(n) foralln >= n0}. iff(n) = 3n42 IFf(n) = 3n#2_g(n)=n? a(n)=n Then Then 3n+2>= cn? (n)>= eg(n) For instance c= For instance c= 3nt2>=n Hence f(n) = omega(g(n)) omega(n) logn log(logn) HW Piccreoe GDH Cn SG OGGERE AS 26 Meaty ceuty 5 8 Eva a 40 SO my Scanned with CamScanner Bess ee es 2 - 98 x © (One CHaeren/Soienint Spat ae) ¢ 0 0 eee & © © Notation: ©((g(n) = (Ho): there exist postive constants cl, €2 and nO such hae 0 <= elg(n) <= f(n) <= e2%e(0) forall n >= n0} fam = GM) 2c Metco 8 Be a0 EE Scanned with CamScanner © pneu + etalon x 2 - 9 x © (One CHaern/Soiesintt Spat a) ¢ 0 0 eee 0 <= cl¥g(n) <= f(n) <= c2*g(n) for all n >= n0} If f(n) = 3n42 ~ a(n)en Then cl¥g(n) <= f(n) <= c2*g(n) C,c2>0 and N>n, For instance c2=4 f(n) 3n#2 f(n) For instance c=! 3nt+2>=n Hence f(n) = theta(n) Siren QP Cn ao Temas ABR ae te HOM my Scanned with CamScanner Spx neesees Boman oanoe x = 2 - 9 x Ofte cyeenheerDoanionsnt st 1.1060 Order of Growth Function ogartinic|[tinear| N-log-N [quadratic] euble | exponential ‘O(n tog dog 2) m_| 0@?) oe”) 1 2 o 4 16 16 a 256| 65556 32,768] 4294.967.296 9)262.144] 184 x10) EZ Simmeoens GDH es cOeesa A BC ety city 9 GM eo arya Scanned with CamScanner Spee * Bomanocno x ars - 9 x © (One CHaeren/Soienint Spat a) ¢ 0 0 eee Pipher PH Cw 26 Mentrcney AB Sm a cd EEN mw Scanned with CamScanner © px nceseues i res ars - 9 x Ole cileerdenrDaunonsiunt pst a ~— UNITI INTRODUCTION TO ALGORITHM DESIGN BP ichrosen QP Cs & Hi Newrears 9 6 Bie aay MS RDA Scanned with CamScanner wines ee © Ofte cileerhenDaanonsint pat PD Wperereto seer x ae) ¢ 0 0 eee Recursion recall @ Functions calls itself © consists of one or more base cases and one or more recursive cases. ° ro =0 Kon +1 10) = F-1) +1 foralin > 0 roof Tinwmse AG me aame Scanned with CamScanner © px nceseees +B onapocnom x = ars - 9 x © Olie cieerhesrDounionsint pt a .10e= Mathematical Analysis - Induction —@ Consider a recursive algorithm to compute the maximum element in an array of integers. @ You may assume the existence of a function “max(a,b)” that returns the maximum of two integers a and b . Function FIND — ARRAY — MAX (A,n) Lif (w= 1) then 2 return (A[1]) a:else 4: return (max (A[n], FIND ~ ARRAY ~ MAX (A,n— 1))) S:end if HP detector Tinwmss A Gmm aan BoM Scanned with CamScanner Spx nessees +B onapecnon x = 2 - 9 x © Olie cyeerhenrDaanonsint st Qe SOO Riemann Solution (© Lecpi) stand forthe proposion dat Agen finds and returns the masini ieeger inthe locations Af} shrough Alo}. According, we have show tha (a) in) eve BASIS: When there tony one clemens inthe ara Le n=, then hs lente leary the maxanum elerent and ei returned on Le 2 We thus ye tha pt) INDUCTIVE STEP: Assume hat Algorithm fds and returns the maximum deme when here are wea elements nA Now consider dhe cae in which there are K+ I element in A. Since (k+1)> I Ln # wil be execne, From the ductive hypothesis we know thatthe maximum elements Alt through Ae le retumed low the msnumamantin Aether Ai] or the imaximum clemencin Af] hough Af] (3 ©). Thus ceturing the maximum of Agee city gh ram ere A repre pel Bp aplying hn principle of atresia ndction we can vont tne) ise, Algom is correc. HD dpetereto seach Hi newman - 8 Gm 440 Fo Lvs Scanned with CamScanner 0 © aerumeT 2 * fe Bess ee Ofte cileerhenrDaanionsiint pat @ Find the exact solution to the recurrence relation using mathematical induction method TQ) = 0 ro) =ar(@)+n 22 © Solution is T(n)=nlogn Tone Sama OM my Zircon QP Cn ao Scanned with CamScanner 0 cat Apt cen De © Olie cyeerhenrDounonsint st WD Typerere rosea @c = ars Es A) SO © domaine Solution @ Basis: at n = 1, both the closed form and the recurrence relation agree (0=0) and so the basis is true. ‘© Inductive step: Assume that T(r) = rloge for all 1s r= k. Now consider T(k+1) As per the recurrence relation, we have, met )+ Ue + Dy since ce + 1)2 2 ++ Oc + Des pr the inductive hypothe sine + Dllogtk + 1092) + + 1) = + leg + D-H aH = + Diogte +) © We can therefore apply the principle of mathematical induction to conclude that the exact solution to the given recurrence is ms rE ee Scanned with CamScanner © pneu i ree <2 - 9 x © Olie cileerhensDaanionsiint Spat — ~— UNITI INTRODUCTION TO ALGORITM DESIGN BP ivchrosen QP Cs & Hi Newrears 9 6 Bim acy MS EA Scanned with CamScanner + Bess ee re ars - 98 x © (One CHaern/Soiesintt Spat ae) ¢ 0 0 eee Substitution method ~~ @ Guess the solution. @ Use induction to find the constants and show that the solution works. E Pimms GDH es acOGeesa Ai Newnent <8 Wm 40 om ues Scanned with CamScanner © px neseees + Boneaoceos x - 8 x © (One CHaerten/Soienintt Spat a*) 4 0 0 eee Example y= {! ant ~ arian) n ifm 1 1, Guess: T¢n) = len n TC) Boer = 18 nignt n= 1= 7) Inia sep: Inde phat 7 = EIR Ea 16 = 42. For n= 4", k = logy(n), we have: 'ea4(n) T(n) = cn? S* i=0 Ts von AB See aa Me SEO my Scanned with CamScanner Spx neseues 6 nea eaten Ofte CieerhanDamniensiint pst = 2 - 8 x A) SO © termine: Applying the geometric sum Applying loga(n) T(n) = on? 3 (3) 1=0 with r = % leads to 3. leant T(n) = on? (4) = 1 %-1 E Simmons GDH es aGOeesa “Hinds ty AB Dw 40 fe ony MB) Scanned with CamScanner wines 6 Gneaecion Ole CileerhenDoanionsint pat PD Wpehereto seer = 2 - 98 x ae) ¢ 0 0 eee Polishing the result we get Instead of T(n) < dn? for some constant d, we have qn 2 T(n) = on?) % Recall loau(o) 5 3 1 ro=or 5° (3) To remove the log, (n) factor, we consider Tin) < ofS (Gs) < di, for some constant d. Ty voy AB See aa Me SE am Scanned with CamScanner Spee Boman canoes x 0 © aerume 2 * fe © Olie CieerhenrDamnionsiint pst Verify the guess by applying substitution method Let us see if T(n) < dh? is good for T(n) = 37 (n/4) + en?. Applying the substtuton method T(n) = 37(n/4) +n? me 3a (2) + on? 3 2 (Sexe) 3 (4418) 2 3 (4x32) 3 2 5 qe 20m, itd > Fe 16. an? WH Pigercoe GDH Cn SG OGERea Se High wines cy 5G Sm cg a) STAM Scanned with CamScanner Sp nees Bema coantie x 2 - 9 x © (One CHaerten/Soieninit pat ae) ¢ 0 0 eee Lets see another example Consider T(n) = T(n/3) + T(2n/3) + en. = O{nlog2(n)) Ss von AB See aa Me Im Scanned with CamScanner + Bess ee re ars - © Olie cieerhesDamnionsiint pst a .10ea= Lets practice © Consider T(n) = 3T(n/2) +n. Use a recursion tree to derive a guess for an asymptotic upper bound for T(n) and verify the guess with the substitution method. © T(n) = T(n/2) +r’, © T(n) = 2T(n- 1) + 1. Se Mgnvindsadny BSW 0 bo disney MB Scanned with CamScanner x Bonar ccno x © Ole cyleerhasrDaunionsntt pst WD Typeneretoseuc <5 | UNITI Co eae INTRODUCTION TO ALGORITM DESIGN Stamm A Rm ac Me a Scanned with CamScanner Spx nesees 6 Gia canoes Ole cyeerhenrDaanonsuntt pat Bice Qi en ao Speen ACER aa me a wy 2 * fe 0 © eum Master’s theorem method (© Master Method isa direct way to get the solution. The master method works only for following type of recurrences or for recurrences that can be transformed to following type. “T(n) =aT(n/b) +F(n) where a>= 1 and b> 1 and f(a) in asymptotically positive Function. © There are following three eases: |. Hff(a) < O(a"), then T (n} = 6 (9. 2 tFf{a) =0 (0), then T (n) = © (0 ogn), 3 f{n) > (a9), and f(n) satisfies the regularity condition, then T(n)=O(Ke), Scanned with CamScanner Bess es 2 - 9 x © (On CHaeren/Soienint pst ae) ¢ 0 0 eee Solve the problems © T(n) = 3T(n/2) + n® © T(n) = 7T(n/2) + n? © T(n) = 4T(n/2) + n2 @ T(n) = 3T(n/4) +n Ign Gee A Rm aa me eae Scanned with CamScanner WD Typenere rosea wi secaeas 6 Gace capone Ole cyeerhanrDaunonsiuntt st @®sc.can = 2 - 9 x ae) ¢ 0 0 eee? T(n) = 3T(n/2) + n? (0) =aT(e) + Ko) where a>= and b> | a=} b=2 fier 1. Mi(a) 9 (0,9), and f(a) then T (n) =0 (f(a) ‘Step 1; Calculate n°? = n'°*, Step 2: Compare with f(n) Since f(n) > n,* ie.n?> n'5® Step 3: Case 3 is satisfied hence complexity is given as ‘T(a) = O(f(a)) = 0 (n*) ies the regularity condition, Sy vay AB See a Me Im Scanned with CamScanner © px nceseees +O oncaecron x - 8 x ae) ¢ 0 0 eee Ole cyeerhenrDaanonsiuntt pat T(n) = 7T(n/2) +n? ‘T(a) = aT (ab) + (n) where a>= I and b> 1 a=7 0 b=2_— f(ny=nt |. If f(n) < O(m!*,9), then T (n) = © (n4,") 2 If f(n) = 6 (n'"%,) , then T (n) = © (n!%,*logn). 3. Iff(n)>a (4,9), and f(n) satisfies the regularity condition, then T (n) = © (f{n)). Step 1: Calculate n'°¥,2 = n'%t,’= n2#° Step 2: Compare with f{n) since f(n) = I and b> 1 ana be2 fen 184) (0,9, ano) stisies the regularity eondition, then T ()=0c™, Step 1: alelat =n Step 2: Compare with fn) // Since f{n)=',"*log'n ‘Step 3: Case 2issatistied hence complexity is given as T(a) = 0 ({(n}logn) = (a*logn) Boinmome GDH Cua Oeee a DBC Payanay 9 BB 08 oe Scanned with CamScanner

You might also like