0% found this document useful (0 votes)
47 views37 pages

DAA-unit 1

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)
47 views37 pages

DAA-unit 1

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/ 37
UNIT -1 INTRODUCTION =} 1.1 Algorithm Q1 What is algorithm ? Ans. : Definition of algorithm : The algorithm is defined as a collection of unambiguous instructions occurring in some specific sequence and such an algorithm should produce output for given set of input in finite amount of time. Q.2 Define algorithm and describe __the : GP [INTU : Part B, March-06, Nov.-06, Marks 8] Ans. : Algorithm - Refer Q.1. | Characteristics : Algorithm should posses following Properties ~ 1) Input - The input of zero or more number of quantities should be given to the algorithm. __ /fnitially count is intialised to 0 ‘But in debugging technique only presence of error is pointed out. Any hidden error can not be identified. Thus in debugging we cannot verify correctness of output on sample data. Hence profiling concept is introduced. © Profiling or performance meastirement is the process of executing a correct program on a sample set of data. Then the time and space required by the program to execute is measured. 1.2 Pseudo Code for Expressing Algorithms Q.4 Write a recursive algorithm that converts a ‘string of numerals to an integer, for example °34567" to 34567. 05 [NTU : Part B, Dec.-11, Marks 7] Ans. : Algorithm StrToNum(char *c, double *i) t //oitially i is initialised to 0 if (1c == "\0'| | lisdigit(*c)) then retum StrToNum (¢ + 1, 1); th Q.5 Write a recursive algorithm that calculates and returns the length of a list. ER [INTU : Part B, Dec.-11, Marks 6] Ans. : Algorithm stringlen( char *p1,int *count) { = \0) then 4 ne (ou ali)" } jum ¢ sum+al0]; Write(sum);//evaluated result Introduction If there are N number of characters in the comparisons are needed to obtain the ope capital letters. count for Fewest Number - If the file is so ted and for the capital letter using binary search ina then in logN number of comparisot letters can be obtained. reeet See Largest Number - Comparing each letter for ensuring capital or not gives largest number of comparisons. It takes N number of comparisons. 1.2 Performance Analysis Q.8 Define time complexity. SGP [INTU ; Part A, Nov.-16, Marks 2] ‘Ans. : Time complexity is the amount of time taken by the computer to execute an algorithm. It is denoted by the asymptotic notations Q.9 Define space complexity. ‘GS [INTU : ‘Ans.: Space complexity is the amount of space required by an algorithm to execute. It is denoted by the asymptotic notation. Q.10 What are the two factors used for defining the space complexity ? OS [INTU : Part A, Marks 2] ‘Ans. : To compute the space complexity we use two factors : constant and instance characteristics. The space requirement S(p) can be given as : ——— [sm = e+e | where C is a constant i.e. fixed part and it denotes the space of inputs and outputs. This space is an by instruction, variables and rt A, Nov.-16, Marks 2] 1-3 Design and Analysis of Algorithms —— can measure the performance of an algorithm by | computing two factors. 1. Amount of time required by an algorithm to | execute. 2. Amount of storage required by an algorithm. This is popularly known as time complexity and ‘space complexity of an algorithm. Fig. Q.11.1 Analysis of algorithms Q.12 Why It is difficult to compute the time _ complexity in terms of physically clocked time ? Ans. : It is difficult to compute the time complexity in terms of physically clocked time, because in aoe Introduction a=10; rintf("%d",a); } For each statement except declaration statement, the frequency count will be - ‘The frequency count of above program is 2. Q.44 Obtain the frequency count for the following code. i=1; 14 then. As n= 15 the 2" exceeds 100n?. Hence n = 15, is the ‘smallest value satisfying the given condition. Q16 Arrange following rate of growth in ‘Increasing order. ®, nlogn, n?, 1, n, logn, nl, n° ‘ER [INTU : Part A, Marks 2] Joga, n, nlogn, n?, n3, 2", nt g Introduction 1.4 Asymptotic Notation | Q.18 List out the reasons for one faces whlle determining lowes Conngts®tht FETLINTU Part A, Nov.-16, Marks 2} Ans, : A lower bound of a problem is the least time complexity required for any algorithin which can be used to solve this problem. The problem with computing lower bound is that - The lower bound for a problem is not unique. For example - (1), (n), Q (a log n) are all lower bounds for sorting. Due to this - i) We always try to find higher lower bound ii) We always try to find better algorithm Q.19 Define time complexity. Describe different notations used to represent these complexities. S@ [INTU : Part B, May-09,13, Marks 10) Ans. : Time complexity - Refer Q. 8. Asymptotic notation is a shorthand way to represent the time complexity. Using asymptotic notations we can give time complexity as “fastest possible”, “slowest possible” or “average time”. Various notations such as @, © and O used are called asymptotic notions. 1. Big oh notation The Big oh notation is denoted by 'O’. It is a method of representing the upper bound of algorithm's running time. Using big oh notation we can give longest amount of time taken by the algorithm to ‘complete. Definition Let f(n) and g(n) be two non-negative functions. Design and Anaiysis of Algorithms 1-5 Let no and constant ¢ are two integers such that no denotes some value of input and n > no. Similarly ¢ is some constant such that c > 0. We can write f(a) sc gin) | then f(n) is big oh of gin). It is also denoted as f(n) € © (Bn). In other words f(n) is less than g(n) if gin) is multiple of some constant c. For Example : If (n) = 2n + 2 and g(n) = n? then f(a) = O (g(a) for n> 2 2. Omega notation Omega notation is denoted by ‘9. This notation is used to represent the lower bound of algorithm's running time. Using omega notation we can denote shortest amount of time taken by algorithm. Definition A function f(a) is said to be in Q (g(n)) if f(n) is bounded below by some positive constant multiple of g(n) such that fin) > ¢* gin) For all n> no ‘Ans, : Big- Oh notation ~ Refer Q. 19. Little oh notation - The little Oh is denoted as o. Its Introduction Definition Let f(n) and g(r) be two non negative functions ‘There are two positive constants namely c, and ¢, such that H cy $ gta) Sep g(r) ‘aa nouson Fn) 0) Fig. 0.19.3 Then we can say that fia) € © (gin) If f(n) = 2n + 8 and g(n) = 7n. where = n 22 Similarly f(n) = 2n+8 gin) = 7n ie. Sn <2n+8<7n Forn22 Here 5 and = c) =7 withng =2. Then —f(n) = O(n) The theta notation is more precise with both big oh and omega notation. F Q.20 Compare Big-oh notation and _Little-oh notation. Illustrate with an example. SGP [INTU : Part B, May-12, Marks 7] Anatyole of Aigorttems _____— pai on + oe = 7° + 50r? + 200 gw Let ft) fin) < 300° than some crossing point. Say n> 10. For n larger 4 ones? e Be culating little oh we have to pick up m such fe iy ceeatis rue So pickup nie. max{ 10) Hence for given fin) = 0 (8°) ‘the big-oh notation f(n) = OCF"). (0.22 What are the basic efficiency classes ? ‘Ans. : The classification of different order of growth nlogn | Some | | instance of input is | for the list of | using merge sort or quick sort. Performing matrix ‘multiplication. Desigmand Analysis of Algorithms A“? 6. /Output: The Fibonaco! number present at n location 7. if (a<=1) then 8. Write(a); 8. olse 10.{ 11 B=0f1=1; 12. for(i=2 to n) do aed 4 este; 1% effing, aay 17. Write(p; 18. } 19.} Analysis In above algorithm, basic operation is computing of next value in the Fibonacci sequence. ifn =2 then LHS. = f(n) + gin) = (Gn? ~n+4)+ (nog yn+ 5) = (3 (2)? -2+4)+ Qlog2+ 5) = (12-244)+(2+5) 2-2 RHS. = n? = cen? = 6)? with value 6. = 64 where c is a constant = That is LHS. < RHS. Hence f(n) + g(n) sc*(n?) ‘This proves that f(n) + gin) = O(n?) Q.25 Derive the function f(n)=12n?+6n is O(n’) and o(n). OS°[UNTU : Part B, May-08, May-09, Marks 6] ‘Ans. : The omega notation is denoted by ‘2’ This notation is used to represent lower bound of tlgoritha’s running time. Using omega notation we can denote shortest amount of time taken by algorithm. Hence we can write, fin) 2 ce gin) For all n2 no 7Then An) € 2 (gin)) Fisk rag We Pave to Bnd out values of 9 and ¢ - pa smite Mens goat - 0° -7 we coos €=3 ten c# 0) = 3°27 = Fee fn) 2 ¢* Btn). in f(n) = 2 (g(n)) ow we have fo prove that f(a) # 8 (Btn). je ¢1° 860) $ Hn) $ cy * g{n) is not true. yn=3,c, = Zand c = 3 then. 2+) < 12)? +6) s 3* (3)? 2027 < 116481 -. f(n) # O(g(n)) Hence fin) = @(g(n) is proved. 226 Show that —fy(n)x fa(n) = O(a4(n)x ga(n)) ‘where fy(n) = O(g1(n)) and fon) =O (g2(n)). EG [INTU : Part B, Sept.-08, Marks 6] Ans. : Let f(n) eGind acted Devin and Analysis of Algorithms Ra f(n) 2 gin) we can say, ffm) = Q gin) Case 2: To prove f(n) * © (g{n)) we have to prove that, £1 * BIN) S$ f(n) scp * g(n) is not true. fe) = nSlogn and gin) =n? Assume n= 4, c,=3, c) =4 then ‘The 1° 8M) = 3¢n3 23043 = 192 fim) = n3 logn = 4 (logy iain 2 *g(n) = dend OL = 256 te 192 < 128.256 is not true. ‘Hence * g(n) < f(n) < 256 is not true. * {(n) = © (g(n)) is not true. 28 wo 3 array 2 of array temp to b. Display array b. pranstor contents ) take O(n logn),time. Removing of The two on time. Hence this algorithm takes a )) and Tz (n) =O(g(n)) = O(n) 2 (n) = oH 1 i) J; (n)+ Ta (n) = O(max(fin), g(n)). - then show that Ta NTU : Part B, May-08, Marks 8] . F ‘ans. + Let there be some constant c such that | (a) $181) ~- (Q304) "similarly there will be some constant c2 "Such that T, (n) $ cp gy (n) forn2n, ..(Q30.2) Let ¢3= max) ,¢p) such that n2max{nj ,n9) ‘Then we can write using equations (Q.30.1) and (Q302) as: Ty(m)+T (n) $ cy 81 (n)+ cy B2(n) S (cy +2) (8) (M)+ Bam) S ¢3 (B, (n)+ 82 (n) S cg 2max(g, (n)+ gp (n)) forn2n, | Hence Tis)+T (m) € O (max (g, (n)+ gp (n)) is true. -10 Min — a[0} for (i 1ton) do { if (ali) > Max) then Max ~ a |i) else if(al\} < Max) then Min ~ a [iJ } Write ("The minimum element is", Min), Write ("The maximum element is’, Max) Mathomatical analysis : Step 1: The input size is n ie. total number of elements in array. ‘Step 2: The basic operation is comparison in loop for finding maximum and minimum. Two such operations are there i.e. one give by if and other one is by else if Step3: The comparison is executed on each repetition of the loop. As comparison is made for each value of n, there is no need to find best case, ‘worst case and average case analysis. Step 4: Let, C (n) be the number of times the comparison is executed. That means on each new value of i, the comparisons are made. Hence for i= 1 to n-1 times the comparison are made. Then C(n) can be formulated as - C(n) = Two comparisons made for each value of ; Step 5: Let us simplify the sum al Cn) =F 2 a 2 Cy - 2-91 a = 2(-1+1) Ben-ivien] Design and Analysis of Algorithms 2.32 Write an algorithm to find the largest clement in an array of n elements. Find the time complexity. ES LINTU : Part B, Dec.-12, Marks 8] Ans. : Algorithm LargestElement{int al),int n) { int large; large — a0}; for(i=Oilarge) large < ali; } Write(‘The largest element: *+large); a Time complexity = O(n) 15 Probabilistic Analysis Q.33 Write short note on probabilistic analysis. -—SPEINTU : Part B, Marks 5] . Introduc,, ‘* Mutual exclusion : Two events ged B are saig , be mutually exclusive if they do not hay," common sample point Hence Antag example A =(HHTH, HHT, B = (HITT, ye! are mutually exclusive. « Independence : Two events A and B are sag a independent if P(A mB] = PIA]+PIB] + Random variable : The random variable is basi, a function that maps elements of S to set of r, numbers. *For sample point a€S the F (a) denotes 4, mapping. If F denotes a finite set of elements the, it is called discrete. Thus the random variables. be discrete random variables. ‘* For example - If we pick up four balls from an un containing red and white balls then the number 4 red balls that get selected is F (RRRW) = 3 or (RWWW) = 1 and so on. Q.34 Explain the concept of probability, distribution. GR[INTU : Part A, Marks 3) Ans. : Probability distribution : If F is discrete random variable for sample space $ then probailiy distribution can be defined for a range of element (ay/a2/-aq) such that P[F=a,], P[F=a,] Pies) a probability distribution Here 1 means prowence of rect balls and 0 distribution of F is given by popeo) © yg 14 no red ball present pire) “t ie, only one red ball present. pre-e ‘Le. two red balls present from picked up balls p[F+3] = zh be. when 3 red balls present from picked balls, P(R=4] = zk ie. when 4 red balls are present from the picked balls. 35 What Is binomial distribution ? EF “G@P[INTU : Part A, Marks 3] na Introduction | aon dw 0 OB (INTY | Part 6, May-O6, Marks 10) ‘Ans. 1 Assumption 1 Let, f(x) = V4 ~ x2, The Lower bound © 0 and upper bound = 2 ‘Algorithm probabilistic (x), 0, 2) ‘ ‘/fnput : The function f() whowe integration is to be obtained. The lower and /happer bound values //Ourput. ; The result of integration x= 20in0 —— // initialization dx = 200800 /* Pind lower limit value */| /* Computing low limit LOW = 0 from x = 2 sin 6 */ 0 = 255in 6; 0=0; /* Find upper limit value */ Upper = 2 2=2sin 0 sin @ = 1; Om Hf /* putting upper and lower limit values for integral */ 1/2 I= J V4-4sin? 0 2 cosoao ° 2 = 2f Vi-sin2é 2.008640 o n/a = 4 cos?oao // As cos 2t = 2.c08*t-1; ° aoe Sey ex sin20]"? cd Deel and Analy of Algoritims O37 What ts randomizer BP LINTU : Part A, Marks 2] Ams.: A random number generator or randomizer makes use of r, fandomized algorithms. In randomized algorithms the decision is always taken based current output. EE See ee ce randomized m. ESP [INTU : Part A, Marks 2] Ans. : There are two types of randomized algorithms. Q.39 Give the advantages and disadvantages omized algorithm, ES[ONTU : Part A, Marks 3) Ans. : There are two major advantages of randomized algori 1, These algorithms are simple to implement, 2. These algorithms are many times efficient than ‘traditional algorithms. However randomized algorithms may have some drawbacks- of B | Imre, An amortized analysis guarantees the time operation over worst case performance . it is the difference between a, pHs hada analysis? mmNe ca, Ans.: There is a dference between averag, analysis and amortized analysis. In averayy analysis we are averaging over all posse and in amortized analysis we are averaging 5° sequence of operations. An amortized ay assumes worst-case input. O42 Explain about amortized anaes probabilistic analysis. BSP [NTU : Part B,Dec.-12, mariy 0 Ans. : Amortized analysis : Definition : An amortized analysis means average running time per operation over a sequence of operations, There are 3 commonly used amortized analysis - i) Aggregate analysis Potential method Worst ae techniques used ii) Accounting method implementing stack. The tv? ‘perations of the stack are 1. Push and 2. Pop ‘operation, F eration has running time as (1). That mex Ibe ne oPertons the total excsn end Analysi of Algorithms : 1-4 an there is actual cost of each operation. The cost can be more or less than actual amortized cost > actual cost the difference js saved in speeific objects called credits. | for particular operation amortized cost < Mlk ti sexe ced re utilized. | ample - In case of multiple_pop() operation the . cost for each operation is O(n)/n. et De be the initial data structure. Hence for n “operons Dy Di, DaowD will be the data "structure. Let cy, ©, .. ca denotes the actual cost. | | ® is a potential function which is a real ber. (Di) is called potential of D,. | 6j be the amortized cost of i operation } & = 4+ 9(D,)-0(0,.,) change cost. hed (4, +@@,)-4D,_,)) el ee Introduction Here equation (Q.431) is called and equation (Q.43.2) is calle oa od initial condition, The Q44 Explain the method of solving re relation. a Maaco ES'[INTU : Part B, Marks 5] Ans, : There are two types of substitution - * Forward substitution * Backward substitution. Forward substitution method - This method makes use of an initial condition in the initial term and value for the next term is generated. This process is continued until some formula is guessed. Thus in this kind of substitution method, we use recurrence equations to generate the few terms. For example : Consider a recurrence relation Tea) = T(n-1)+n with initial condition T(0) = 0. Let, Tin) = Tn-1)+n -- (Q44.1) Ifn=1 then Td) = T0)+1 =0+1 .. Initial condition a TM =1 (Q.44.2) Ifn=2, then T(2) = Ta) +2 = 142 eres ws (Q44.3) Design and Analysis of Algorithms _ We can also denote T(n) in terms of big oh notation as follows - Tin) = O(n?) But in practice, it is difficult to guess the pattern from forward substitution. Hence this method is not very often used. Backward substitution method - In this method backward values are substituted recursively in order to derive some formula. For example - Consider, a recurrence relation Tw) = Tm-1)+n we (Q445) With initial condition T(0) = 0 Tm-1) = Tim-1-1)+(@-1)_— --- (Q446) Putting equation (Q.44.6) in equation (Q.445) we get Ten) = Tin-2)+(n-1+n — ... (Q447) Let Tin -2) = Tm-2-1)+(n-2) -~ (Q448) ‘Putting equation (Q.44.8) in equation (Q44.7) we get, Tm) = Ta- pela a Hen aa gs Tm-1) = Tn-2*1 Tn) = Ta-Y +1 = (T(n-2)+1) +1 Tn) = Tn-2)+2 ‘Again T(n- 2) = Tn-2-1+1 = Tn-3)+1 Tn) = Tea 2) +2 becomes = (n-3) +) +2 Tn) = Tr -3) +3 Tm) = Tn- kK) +k + (Q451) fk = n then equation (Q.45.1) becomes Ten) = =0+n TO) +n Initial condition T(0) = 0 Ta) =n 4 We can denote T(n) in terms of big oh notation Tin) = OW). Q.48 Solve the recurrence relation aca GP [ONTU : Part B, Marks 5) nt aatysis cf Sigertone_—_— tH) - 2 fae jh n= 45 } een If we assume 2* = = n-T(1)+n-logn + TQ)e1 qin) = n+n log (a) Tin) = 20g 2 ce in terms of big oh notation - Tin) = O (n log n) What is Master's theorem ? EB [NTU : Part B, May-17, Marks 5] +: Consider the following recurrence relation - Tia) = aT (n/b) + Fn) where n 2d and d is some constant. the Master theorem can be stated for efficiency Introduction Another variation of Master theorem is For = Tin) = aT (vb) + fim) ifn >a 1 If f(n) is O (n'°8b"~*), then | Tn) = 0 (n%0*) [2 Iff(n) is © (n'°F0* log*n), then | Tin) = © (n'%80* Jog**t ny 3. If fin) is Q (n°8b***), then | a T(n) is = 9 (f(m)) Example 2: Tin) = 2T(n/2) +n logn Here f(n) = nlogn a=2,b=2 Jog,2 = 1 ‘According to case 2 given in above Master theorem fin) = © (n'°82? log!n) ke1 Then — T(n) = © (n'°8b* Jogk*In) = © (n!%2? Jog?n) = 0 (n! log?n) Tm) = O(n log?n) ie. Q48 Let a, b, c be numbers such that 0 < a, b<1 and c > 0. Let Tin) be defined by T(n) = T(an) + T(bn) + cn. a) Show that if (a + 6) < 1 then T(n) Is bounded by linear function. b) Does there exist a, d such that, for all a, b, above Tin) = O(n’)? ESF [NTU : Part B, Marks 5] ‘Ans, : a) To show that T(n) is bounded by linear function when (a + b) < 1, we will consider a = 3, i. Ga) Ga) ( @-»- ata) Fig. 0.48.1 At third level we will get - (ae -9f ee Tus( 345) pattern works for it* level (i3)" -(B)" for i'® level. Then by summing up total amount of work done by T(n) will be no - Fie) Tin) = Hence Tin) = O(n) This proves that T(n) is bounded by linear function by Ifa =b then, we can waite, in following form ae a(m(s}s)** - ar(5}+2 -et3}} = st(z)* ea Te) = -#a(z}* Ta) = ees Ie we assume 3 =n then Te) - #1(2)+k Tin) = TU) +k = +k Te) = = 28" + log.n «» Assuming T(1) =1 and the @ bound of above recurrence will be Tm) = 20%") |b) Let ne = 51(3)+0 pieseare ty qeades are wes = 97? gen of Master's orem = BE - Te) = @ (0% bog a) Te) = 2a een) Te) = OT @/25)+n™ bog n fa) = a bog n= O(n"F 5°") Te) = © in) Ta) = Ta@-pen | Te-) = Tn-2)+ (@- 18 +08 a Ta) = T@-k)+(@-k-1+n° xsn-lten = Td) +(a—n+1-1+n° lt ie . ear" 8 Introdacton Asa=b, by Master's theorem, Tin) = 6 (a log n) as fa) = @@4)1 By backward substitution, Ta) = Ta-1+e Tan) = Tm-24+2-)4+2 Tin) = Tin-2)+ 2-14 Similarly, Ten) = Tin-3) +c 2+ Ten) = Ta) + Set one | As Introd, the recurrence relation for ns. 7 We will write ‘Ans. : We will Th problem for computing its runnin a) Tin) = st(z}* A Master's theorem Apply Te) - at(B)*F b-2,Fny=n' ad=1 a ie Lie te, > 2 T(n) = e(n'*%b*) Tin) = 0(n'82°) >) Tn) = 2-1) +C where C denotes a constant time. by solving recurrence by backward substitution method, we get Tin) = 2 (2 Tin-2)+C)+C = 2Tm-2)+2C+C = 2 (2T(n-3)+0)+3C = 2Tm-3)+2C+3C =2-1C s» assuming T(0) =i Tm) = 62°) 9 Te) = 97(3) +c? Apply Master's theorem, Te) = a1(5}+ Feo) Here, a=9,b=3,F(n)en? nde a= biz9-32 T(r) = © (x4 log n) fun) Divide and Conquer ‘ES [INTU : Part B, May-09,13, Marks 8, ‘April-t, 12, Marks 7] Consider a 21 and b > 2. Assume n = bk k=1,2, TOD‘) = aT (K/b)+ Fb‘) = aT (bk-}) + fk) a afaT(b¥-2) + s(bk-4)) + Fok) a?T (DK) + af (bk!) + Fok) Now substituting T(b‘-*) by using back substitution, = a? faT DK 9) + FOK-2)] + af (OE) + FO) = aSTQK~S) + a7F(bE-2) + af(bk-}) + FO‘) Continuing in this fashion we get, = FTO + ak 1 £01) sak? £062) +. ¢a° £00k) = FaXT (y+ ak-? fb) ak-2 £02) + ta? fO*)] ) where This can also be written as, k ik k = ATC) +f) + 60%) + tod Taking a* as common factor 2 ‘ = #t[ry «0D. = =| Design and Analysis of Algorithms We will write 7(2") = S(m) S(m) = S(m/2) +1 Here a= 1, b= 2, f(n)=1 Jogya = log, 1= 0 Royer mPa - te, theorem Case 2 of Master's By Master's theorem S(m) = © (log m) . ie Tin) = Q (log log n) e) Let, Tin) = Tin-1)+c" where c>1 By backward substitution, Tn) = Ta@-1) +c" Ti) = Tin-24+7 40 Ti) = Ten-2)+ 142 Similarly, Tir) = Ta-3) 42-2414 1-19 Intro, | write the recurrence relation 4" ‘Ans. : We will problem for computing its running time a Tin) =5T (3 )*" Apply Master's theorem Tn) - orp )+ Fm Here, a=5,b=2, F(n)=n" wdet As nie bl te 6 > 2 Tin) = (n'%b*) Tin) = o(n'82°) b) T(n) = 21(n-1)+C where C denotes a constant time. By solving recurrence by backward substitution method, we ge Tn) = 2(2Tin-2)+O)+C = PT -2)+2C+C = 2 QTm-3)+0+3C = PT -3)+27C+3C 7 2-1C _.. assuming T(0) +1 Ti) = 0@%) ° Ten) = 91(3}+ cat Apply Master's theorem, Ten) = sr(3)+ Fin) esi an Analysis of Algorithms a - - recursive algorithm : recursive and non a ‘wing divide and conquer es [NTU : Part B, May-17, Marks 5] ans: Algorithm (Non-recursive) ‘gugorithm BinSoarch(Al0. .2-1,KEY) Frutti otums the index of an aay clement iit is “jrequal to KEY otherwise it retums -1 (low+high)/2//mid of the array is obtained 4 ‘if (KEY=A[m))then “ . return m i(KEY1 Time required Cworst (A) = Cire ( One comparison to compare left made with sublist or right sublist middle element Also, Cworst (1) = 1 But as we consider the rounded down value when array gets divided the above equations can be written = Cworst (|0/2J) + 1/for n> 1... (Q6.1) | «= (Q62) =1 The above recurrence relation can be solved further. ‘Assume n = 2* the equation (Q.6.1) becomes, Cororst 2*) = Cworst (24/2) + 1 oes 2E) =! Cyroest (252) + 1 w (Q.63) Using backward substitution method, we can substitute Covert (24-1) = Cworst 247) +1 Then equation (Q.6.3) becomes, Corort (2) = [Cyst 2*°7) + T+ 1 = Cyromnt (24°?) +2 $Thhen | Cipgeat (2) = (Crvnst (29) +1 +2 Cort 2°) * Cort 22) #3 Design and Analysis of Algorithms Corornt 2*) = Cyrarse (1) +k But as per equation (Q.6.2), Crrorst (1) = 1 put this value in equation (Q.6.4), Crore 2*) = 1+ * Cwort (A) = 1+ logy! jog: Sat s Comat (nt) = logy x | loggn = k(1) ¥ logy 2 =1 ra k= log n apt The worst case time complexity of binary search is (logan). ‘Average Case # In average case, the search target is equally likely to be located in any array position. ©The average number of probes made by binary search algorithm into an array of n elements is ‘AS we have assumed n=2' : : ponketlaies iat logan = log, 2* ans 5 Divide and Conguy = (n-log(n+1)-n +1og(0+1)-1+1yn | = loga(n +1)-1+ (log (n+1))/n | The average case complexity is (log 20) _ Best case The best case of binary search occurs when the element you are searching for is the middle elemeny of the list/array | Hence ; | The best case complexity is ©(1) | Q.7 What are advantages and disadvantages of binary search ? Ans: ‘Advantage of binary search : * 1. Binary search is an optimal searching algorithm using which we can search the desired element very efficiently. Disadvantage of binary search : 1. This algorithm requires the list to be sorted. Then only this method is applicable. 8 Enlist the applications of binary search method. Ans: 2-5 ec) se Divide and Conquer aie Tin) =n 1(7}Ho8n O(n) Count rng sos oe Tin) = n-T(n) +1og n-O(n) gt>aa) thon Tin) = g(n) When n is small, and a(n) = O@1) oturn d+; + Th) = 1-00) +logn-O(n) an Tin) = nlogn a . = height of the tree ‘nascnan Yeot ii) g(n) = 0) and F(n) = 041) then the recurrence relation is Tin) = ar) +r) =2 (2) +R] Fn) nis small 2) + F(m), otherwise - ar(Z}+2 Fo) 7? 1(3)+2R Thier / when Dae) = 01) and fin) = Of) i) gia) = 01) and f(n) = O(1). SB LNTU : Part B, May-09, Marks 8, May-12, Marks 15] aw Op) HS) 5: Of). If we assumen=2* then k= logn ‘Then the given recurrence relation is - Tia) = z1ini2) + Fin) Te) = 2[21(3) +e] +Re Tn) = nT (1) + logn-F(n) x T(1) is small hence we assume T(1) = g(n) = O(1) Tin) = n-O() +1og n-0@1) Tin) = log n Q.11 Modify binary search of text so that In the case of unsuccessful search it returns the index i such that k(l) < key < k (i +1). T@P[ANTU : Part B, Dec.-09, Marks 8] Tn) = 2a( 5 ]+ ere) = at(Z}+ log n- F(n) Ans. : Algorithm BinSearch(A(0:0-1),KEY) € ‘/fProblem Description : This algorithm retums index _ /Mfor an unsuccessful search using Binary search ‘(input : The array A from which the KEY element Ei ‘to be searched + etums the index on unsuccessful search ysis of Algoritins sqye edemnent io present in the list"): then jisoareh the left ub list Diesige and Anal watt eave (RBS ALD nigheatl owen tt jygoaroh the Fight sub lst syocessful search ¥ ear ronsing 8 UD 2-6 | | Divide and Conguey [2[ss Ts [a [oo lw] lols ala Swap A [i] and A {j) Stop 2: nia|s|u|o 77 | 35 | 66 | 54 | 95 | x ue Swap A [i] and A [j) nla 1 | 33 | 99 77 | 55 | 6 54 | 55 | 32 a : Stop 3: [nla] n| a | T 9 | 77| 55 | 66 54 55 32 ‘As j Ali? = No. ‘Then do not increment i. Now if Alj] 2 pivot, go on decrementing j [TTA DTP TETE] ase eecrementj j pivot i E A pivot i [ETRIADHIPTETE] a> seme i pivot i PLE] AsP>E decrement j. ee Be Acs € 5 6 PER ete) S| A sent t> toe pivot ij ‘Swap Ali] and AQ] Le [alxyM |r [a] iv x Hee Now A 4 We weap POS and Agi, Then x] As AUT pro dara, 01 2 38 4 5 6 j=pivot) do ieHi if(i<=j) then ‘swap(Ali],Alj)//swaps Ali] and Alj) ‘swap(Allow],A[j])//when i crosses j swap Aflow] and Aul return j//rightmost index of the list The Partition function is called to arrange the elements such that all the elements that are less than Pivot are at the left side of pivot and all the elements that are greater than pivot are all at the right of Pivot. In other words pivot is occupying its proper Position and the partitioned list is obtained in an ordered manner. Analysis Best case (split in the middle) If the array is always partitioned at the mid, then it brings the best case efficiency of an algorithm. The recurrence relation for quick sort for obtaining best case time complexity is, ‘Sea: 2-10 Qn) = 2. C(2) +n Here fn) © n! adel Now, a= 2 and b = 2. AS from case 2 we get a = b4 ie. 2 =2!, we get Tia) Le. Cia) = @(n4iogny ‘Cfn) = @(nlogn) Thus, Best case time complexity of quick sort is @(n log , n) ‘Worst case (sorted array) ‘The worst case for quick sort occurs when the pivot is a minimum or maximum of all the elemens . ‘the list. This can be graphically represented as On) = Cia-1)+n or Off) = nF (n—1)+(n-2)4 4201 But as we know Us2sdt en = ROAD 22 _USPLINTU : Part B, Dec.-12, Mat! Design and Analysis of Algorithms Q.16 Assuming that quick sort uses the first item in the list as the pivot item :I) Give a list of n an array of 10 integers) representing the worst-case scenario. Ii) Give a list an array of 10 integers) items (for example, scenario, pril-14, D 11, Marks 8] Ans. : {) The worst case scenario for quick sort is when the pivot always goes to one of the ends of the array. Hence reverse sorted list of elements is usually worst - Consider following 10 elements case, For example that need to be sorted in ascending order. EPEEEEEEEr a a Swap Alj] and A[pivot] PrPrtT Left sublist ‘Now sort left sublist considering 1 as pivot element. eae reilenniiesfies fee] 3] 2 FT i TEE There will not be any swapping ree TTT] =] Right sublist ‘Continuing in this fashion we get finally the sorted list as condition for best case of quick sort is that the ec: Consider following list of elements as EPDEEEP EE Divide and Conquer a}s|str7ie6]o Pivot i —- n Left Sublist Right eublst Finally we get eam | sleet |, 7] 8] Q.17 Is quick sort a stable sorting method ? Justify your answer. GG [NTU : Part B, Dec.-09, April-11, Marks 8] ‘Ans. : No, quick sort is not a stable sorting algorithm because after applying this sorting method the ordering of similar elements may not be preserved (Since swapping is involved in this sorting with pivot element). Q.18 Determine the running time of quick sort for 1) sorted input i) recerse - ordered input iii) random input {3 [JNTU : Part B, May-09, Marks 8) ‘Ans.: Assumption : For finding out the time complexities for given cases we assume that the pivot is somewhere at the middle of the list. Hence running time of quick sort differs based on selection of pivot. a) Sorted input : In sorted input, the left and right pointers of the array simply move without any swapping. Thus the problem is divided into two equal problems. Hence th recurrence relation for this case is - T(n) = 2*T(/2) + 0 (n) Hence time complexity is O(nlogn) b) Reverse - ordered input : This case is similar to previous case. But in this case on each increment of left and right pointer the swapping takes place. This affects O(n) part of the recurrence relation. But every swap is of O(1) time complexity The division of array still remains the same. Hence the time complexity is O(nlogn). ) Random input : This is average case time complexity. The recurrence relation for random input array is - Tin) = T(0)+T(n-1)+n (1) + Tin - 2) +n. ‘ Tin) J zoe, crn ‘An mat fer knoe re) = TH+ Tin-3)+n qn) = Tn -1) +710) +0 : tum out to be Otnlog n) time complexity [a 49 Apply merge sort to sort the list E, X, A, M, > L, E in alphabetical order. S&P [NTU : Part B, Marks 5] 1 | { Algorithm Combine(A(0...n-1) ow, Divide and Conquer ‘id, high) K€ low;// k as index for array temp ic low; 114 a8 index for left sublist of array if(Ali]<=Alj)then 1 smaller element is present in left sublist { 1 copy that smaller element to temp aay temp/k] < Ali] icin kok+H else UZsmaller element is present in right sublist Heopy that smaller element to temp array ‘tempik] + AU] ici kok+H + 2 Hcopy remaining elements of left sublist to temp while(i<=mid)do rompik] < Ali} ieitt kek+l a: ypy remaining elements of right sublist to temp while(j<=high}do: t tempi] — Abi! tem and Analysis of Algorithms Ta) = Ti) + T@2 + a Time taken by ‘Time taken by Time taken for left sublist to right sublist to combining two Bet sorted ——_ get sorted where n>1T (1) =0 sublists Let, the recurrence relation for merge sort is Tin) = T(n/2) + T(n/2) +n ie. Tin) = 2T(n/2)+cn [ter ; ead Te) = aT +H) ‘As per Master theorem Tin) = @(n4 logn) — ifa=b As in equation (1), a=2,b=2and f(n)=cn ie.n¢ with d=1. and a= be ie. 2-2! This case gives us, 2-14 Did ond cg input : This is a no, both the subarray, sorted. Hence bined and then get x conplexity still, OCnlogn). refinements t0 Merge Sort to maj, GaP [INTU ¢ Part B, May-08, Mara - ordered c in whi 2.22 Suggest jin place. ; ‘ams. : The in-place merge sort algorithm is as yi, jresray alt:n| is for storing the unsorted list of jelements declared globally initially low is set at fc, /Mocation of array 4 /faitialy high is set to last location of array a if (n==1) then return; else { if@ow min_new) then Now further division of the list is not possible Hence we start combining the solutions of min and ma, /feormbine solution ) min « min_new Max = 50 Combine (1, ... 3) and (4, 5) Min = - 9, Max = 50 Now we will combine (1, .. 3) and (4, 5) and the min and max values among them are obtained. Hence. ‘min value = -9 max value = 50 ‘Stop 5: 6 7 . i. = ‘Combine (6, 7) and (8, 9) Min = 25, Max = 90 ‘Stop 6:

You might also like