You are on page 1of 26
ria € Preliminaries ‘5 Mathematical Notations and Functions Floor and Ceiling Functions Remainder Function (Modular Arithmetic) © Integer and Absolute Value Functions 2 Summation Symbol ® Factorial Function Permutations 2 Exponents and Logarithms 2 Algorithmic Notations > Control Structures ® Sequeitce 2 Selection @ Iteration 2 Branching 2 Complexity of Algorithms Analysis of Algorithms @ Space Complexity 2 Time Complexity ¥ Measuring an Input's Size Y Units for Measuring Running Time @ Orders of Growth ® Worst Case, Best Case, and Average Case Efficiencies Asymptotic Notations ¥ Big-oh Notation (0) ¥ Omega Notation (Q) “ Theta-Notation (0) ¥ Comparison of 0, 0 and @ notations ¥ Some Useful Properties ¥ Using limits for Comparing Orders of Growth ¥ Basic Efficiency Classes > Review Questions 'e of the commonly used mathematical functions and To analyse an algorithm, we need to know som notations used. Let us see some of the important functions. ‘and Ceiling Functions : rounds downs the nearest floor(x) : Returns the largest integer that is smaller than or equal to x (I int ceil(x) : Returns the smallest integer that is greater than or equal to x (ie : rounds up the nearest integer). }=2 floor(2.9) =2 floor(-7.2)=-8 ceil(2.5)=3- cel(27-3 ceil(-7.2)= Examples A Remainder Function (Modular Arithmetic) Ifk is any integer and M is a positive integer, then: k (mod M) gives the integer ra when kis divided by M. 30(mod 5) = 0 paki Example: 2 )=5 "Integer and Absolute Value Functions + function INT(x) will convert x into integer and the fractional part Ifx is a real number, then intege isTemoved. Example: INT INT (-7.5)=-7 ‘The abscTute function ABS(x) or | x | gives the absolute value of x ie. it gives the positive value of x even if x is negauve— pbalad Ee casita Example: ABS(-99) = 99 or ABS [-99] = 99 ABS(-3.33) = 3.33 or [-3.33] = 3.33 REM Sosection Symbol Here we introduce the summation symbol E (Sigma). Consider a sequence of n-terms a, +a, ...,, then , +a, + .. +, will be denoted as the sum; Ea, Examples are sum of n natural numbers, square of n positive integers and many more. We can write an expression as a sum of series or sequence. We can also call summation of summation. Consider that a function f(n) denotes the summation of n positive integers. Here function f(n) computes the order of magnitude of an algorithm. f{n)= 5 a, =1+2+...+n=n(n+1)/2, and the order of complexity is O(n?) REMI Factoriat Function n! denotes the product of the positive *(n-2)*(n-1)*n *2*3%4=24 S!=5*4!=120 e integers from 1 to n. n! is read as'n factorial’, ie. Permutations tus consider a set of n elements, of the seUin. some order. ne set in some order. . Suppose the set coy coe nev acbybaes Beas cab ok a,b and a ee abe “Dea, cab, cha, TM various permutatio fthere are n elements in the set then there will be nt set has 3 elements then there will be 3! = 1 + 2°3 nl = 6 permutati ents. It means if th Sipunied Gal LR ions of the elements, : ite t means how many ti 1 Exponen : any times a number is multiplied by itself iF grzatata* a (m times) and a*=17 ‘mis. positive integer, then: =1ye fl Example: 25=2*2*2*2*2=32, ‘The concept of logarithms is related to exponents, If bis a positive number, then th pesitge DuMeC x 10 The DASE IIS Written as Tox TE Tepresents te pesos oma fay raised to get x i.e. y = log.x and by = x Presents the exponent to which b should be VEC e castle ta Cait An algorithm can be described in many ways. A natural lan \ 3 iguage such as English analgorithm but generally algorithms are written in English-like pseudo ee evel languages such as'C' an - The following algorithmic notations are used to write an nee Description Every algorithm is given an identifying name. Example: Algorithm LINEARSEARCH Every algorithm can have brief description of the tasks the algorithm performs and any assumptions that have been made. The description gives the name oe and types of the variables used in the algorithm. Comment begins with // and continues until end of line. Comments are included only for clarity. Example: Algorithm: LINEARSEARCH // This algorithm is used to find the element in an array | / [by accessing elements sequentially. Data types such as integer, real, char, boolean and other data structures such as array, pointer, structure are used. Array ith element can be described as Afi] and (i,j) element can be described as Afi. j) oe Example: _ Integer MAX.MINSUM Real SALARY,AVG Integer ARRAY(N] Variable Names The variable names are generally be in capital letters such as MAX, MIN, NAME, a d \d each step has an rithm is made of a sequence of numbered steps and each st Steps sania sequence ofstatements, sich Steps describe a tobe performed The statements in each step are exec Example: Set A:= 1, MAX:=0; MIN:=0 The maaiqamont statement Ix indicated hy 9 ‘equal (#) hotween the varlahte (inv loft hand alide) and expression or value(s) (i Fight hand ade) For example, ‘ulaition of a and bis aunignod in variable a= b ‘Thore ave three exproastons: arithmetic, relational and logical. The arithmetic oil arithmetic oporater: such a /, *, +», Felatlonal expression used » oe, <, = and logical exprension used logical ro are two boolean values, (rue oF fala Axaignment Statement oxprossion rolational operator auch aes, Operator suet ax Hot, ar, and, Kxwmplor if (A=) | Kxprossion Vor Input of data READ statement and for output of dats WHITE statement are uvod. H more than ane fptit or output data then we can wee comma ae separator among the varlahle name Kxampler AWAD) X AWAD XY WAIT: XY Tho goto alatomont cauwor unconditional transfor of control to the roforoncod. Thus statement goto stop N will cause transfer of contol to step Kxampler GOTO Stop 4 ‘Tho ond atatoment In uved to torminate an algorithm, 1s wsually the last step and algorithm name In writtan alter the end, Example: _ {IND LINEARSBARCH ‘A function I used to roturn single value to the calling function, Transfer of ‘control and returning of the value are accomplished by return(value) statement, A function boginn ax follows: Won funetion name (parameters Ht). E # of programming ts the ability to control a program so thatdifferent Gueolteme mportant concep linesaPZode are executed or that some lines of code are executed many Umes, The mechanism that allow us to control the flow of execution are called control structures, There are four main categories Input and Output Goto Statomont p End Statement Functions or Procedures of contro} structures, pence [Statement 2 “Zo one Instruction then the next and the next, It just L Aecutes them in a given soquence or In order listed. Most [ Statement 2 | Jines of code in any program belongs to this category. In the above figure, statement 1 {s executed first, followed by statement 2, thyrfinally statement 3. [[Stotenent 3 | Sequence Control structur fis is where we select or choose between two (or) more flows, ‘The choice ts decided by asking some sort of questions, The questions are nothing but conditions. Tho answer determines the path (oF which lines of code) will be executed, if condition then Statement 1; else Statement 2 ; ere condition is a boolean expression which evaluate to true, then statement 1 will be executed, otherwise single statement or group of statements, toeil "3 sl either true (or) false. Ifcondition evaluates ement 2 will be executed. S, and S, may be 1eje-tlso know as repetition, it allows Zome code (one or more lines) to be executed (or repeated) several times. It executes the statements repeatedly for acertain number of times as long as the while (condition) condition is true. { staterent 1 ‘The condition is basically test expression, statenent 25 which is evaluated to either TRUE or Seas Baly atts toe FALSE. If the value of condition is true, ee then it executes all the statements in the body of loop after which the control is transferred to the while statement for evaluation of condition again. While statenent Thus body of the while loop is executed repeatedly as long as the condition is true. The execution of Joop is terminated, when the condition is evaluated to FALSE. Then the control is transferred to the first statement after while loop. Branching Zontrol structure that allows the flow of execution to jump to a different part of the program. This category is rarely used in modular structured programming. Go To Statement 1o the task of determining the computing time and ‘o known as performance analysis or efficiency of gorithm. The general idea isto take a particular nally we also study whether or not an As mentioned earlier, Algorithm analysis refers t storage space requirement of an algorithm. Itis als an algorithm which enables us to select an efficient al algorithm and to determine its quantitative behaviour, occasio algorithm is optional in some sense. Structure au algorithms available, We would obviously like by analysing the algorithms in proper When we have a problem to solve, there may be many to choose the best. The selection of best algorithm Is possible manner. Given an algorithm to be analyzed, the first task Is to determine which operations are employed and what their relative costs are, These operations may Include additions, subtractions, multiplications, sand division. Other basic operations might include arithmetic on floating point numbers, comparisons, assignments and executing procedure calls. These operations typically take no more than a fixed smount of time and so we say thattheir time is bounded by a constant. This is not true for all operations of a computer. Some may be composed of arbitrarily long sequence of more basic operations. For example, a comparison of two character strings may use a character compare instruction which may in turn use a shift and bit-compare instruction. The total time for the comparison of two strings will depend upon their lengths, while the time for each character compare Is bounded by a constant ‘The second task is to determine a sufficient number of datasets which cause the algorithm to exhibit is one of the important and creative tasks of algorithm analysis, all possible patterns of behaviour. This | What is Analysis of an Algorithm? “Analysis of algorithms or performance analyse is refers to the task of determining how much computing timBandstorage an algon ives. This isa challenging area which sometimes require great mathematical skills, An' Important result of this study is that it allows to make quantitative judgments about the value of one algorithm over another. Another result is that It allows to predict whether the software will meet any efficiency constraints that exist, Analysis of an algorithm Is a process of making evaluative judgement about algorithms, ‘And also Performance of an algorithm means predicting the resources which are required to an algorithm to perform its task. Generally, the performance of an algorithm depends on the following elements Whether that algorithm is providing the exact solution for the problem? Whether it is easy to understand? 2. 3. Whether itis easy to implement? 4. How much space (memory) it requires to solve the problem? 5. How much time it takes to solve the problem? Etc,, When we want to analyse an algorithm, we consider only the space and time required by that particular algorithm and we ignore all remaining elements, in also be defined as follows. Based on this information, performance analysis of an algorithm ca "performance analysis of an algorithm is the process algorithm and time required by that algorithm", Performance analysis of an algorithm is performed by using the following measures. 1. Space required to complete the task ofthat algorithm (SpaceComplexity) It includes program space and data space 2, Time required to complete the task of that algorithy, {Z4nr€ Complexity) 4nalyse an algorithm by two ways. 1. By checking the correctness of an algorithm Lo easuring time and space complexity of an algorithm of calculating space required by that We. To compute the analysis of algorith, (ori Analysis ie, the number of times the state b. The time taken for one execution. ment will be executed, magnitude of an algorithm is important and producing an alg magnitude is a significant achievement. The notation used in the priori analysis are Big-oh (0), Omega (0), Theta (8) and Small-oh(o). Priori analysis of computing time ignores "ll of the facors which are machine or programming language dependent and only concentrates on determining the order of the magnitude of the frequency of execution of the statements, In this we will collect the actual statistics about the algorithm, conjunction of the time and space while executing. Once the algorithm is written it has to be tested. Testing a program consists of two Major phases, . Debugging : It is the process of executing programs on’ Sampte tata Sets that determine whether we get Propet TF Taulty results occurs it has t0 corrected>~ Read data i ime (t b. Profiling : It is the process of executing a correct Time (t,) Process (data) . Time (t,) « Weite (tine = t, = t) Program on actilal data sets and measuring the time and “space-tttakes-to compute the results durthg execution. (he actual time taken by the algorithm to profess the Tata a) | eee Date Sructrat & mn Priori Analysis and Posterior! Analysis Posteriori Analysts he process of executing the correct ‘Analysis is the process of determining 1 fing is the pt Fe ‘computing time and storage an program on data sets and measuring the time algorithm will require. nd space it takes to compute the results, Is independent of machine programming |_—This is dependent on machine. programming Tanguage and won't involve the execution of language and the compiler used. program 3-TTwill give approximate answer. 3-tP Will give exact answer. 4, uses the asymptotic notations to represent pe doesn’t. use asymptotic notations to| a much time the algorithm will take in represent the time complexity of an algorithm. | | order to complete its execution. Performance analysis of an algorithm is performed by using the following measures. 1. Space required to complete the task of that algorithm (Space Complexity). It includes program space and data space 2. Time required to complete the task of that algorithm (Time Complexity) EIEN complexity It indicates the amount of temporary storage required for running the algorithm. i.e, “the amount of memory needed by the algorithm to run to completion’. When we design an algorithm to solve a problem, it needs some computer memory to complete its execution. For any algorithm, memory is required for the following purposes. 1. Memory required to store program instructions 2. Memory required to store constant values 3. Memory required to store variable values 4, And for few other things : Space Complexity fotal amount of computer memory required by an algorithm to complete its execution is called as space complexity of that algorithm. Generally, when a program is under execution it uses the computer memory for three reasons. They are as follows. 1. Instruction Space: It is the amount of memory used to store compiled version of instructions. 2, Environmental Stack: It is the amount of memory used to store information of partially executed functions at the time of function call. Data Space: It is the amount of memory used to store all the variables and constants. We consider only Data Space and ignore Instruction Space and Environmental Stack while calculating space complexity. It means that we calculate only the memory required to store Variables, Constants, Structures, etc. _ ; sate the space complexity, we must know the a | memory required to store ale F di com ‘Mes (according to the compiler). For example, the ¢ Progracy different 4 Iming Language « at type following IE Comper reauipes oe 1, 4bytes to store Integer value, 2. Abytes to store Floating Point value 3. 1byte to store Character value, 4. Sbytes to store Double value | jn most cases, we do not count the storage required for t | complexity: This i so, Because the space efficiency is used to compare diferasn | same problem, in that case the input/output requirements are fixed. Also, we canner | input or ‘output, and we want to count only the Storage that may be served. We also domecoaree | he input / output as part of the space sarage required for the program itself, since it is independent ofthe size of the Input she space needed by an algorithm consists of the following components. | _ & The fixed static part that is independent of the characteristics (eg: number size) ofthe inputs sand outputs, This part typiGally includes the tastruction space (Le. space for code) SparF for simple variables, space for constants and fixed size component variables. Let C, be the space | required for the code segments of a program (ie, static part). ——— b. The varigble dynamic part, that consists of the space needed by component variables whose size is dependent on the particular problem ins ime betng solved, the space by referenced variables, and the recursion stack Space [depends on instance characteristics) Let S, be thé space required for the dynamic part. The overall space requirements for an algorithm is the sum of both the fixed static part storage and variable dynamic part storage. IP be a program, then space required for program PWM be denoted by S(P). scp) : oprsp. In Example 2.1, it requires 4 bytes of memory to store variable ‘a’ and another 4 bytes of memory is used for return value. ‘That means, totally it requires 4 bytes of memory to complete its execution. And this 4 ! bytes of memory. wire(int a) | return ata; 1 space complexity is aires & fixed amount of space for all input values then tha S(P) =8+0 S(P) =4 Hence, space complexity for the above given program is 01), oF constant. woo] aur ne earn EEX =~: (RSET i one Su of Aras Fiemenes | int ADD (int X[], int n) int total = @, i for (i= Oden; iH) total = total + X [i]; return total; In Example 2.2, the space needed by X is the space needed by variables of type array of integer numbers. This is at least containing n elements to be summed. Here the problem instance is characterized by n. The code requires, 'n*2' bytes of memory to store array variable 'X{]' , 4 bytes of memory for integer parameter 'n’, 8 bytes of memory for local integer variables ‘total’ and 'i' (4 bytes each) and 4 bytes of memory for return value. S(P) =C,+S,=4"*44n*2 =16+2n S(P) =2n+16 That means, totally it requires '2n+16" bytes of memory to complete its execution. Here, the amount of memory depends on the input value of 'n’. This space complexity is said to be Linear Space Complexity. if the amount of space required by an algorithm is increased with the increase of input value, then that Space Complexity is said to be Linear Space Complexity. The space complexity for the above code is O(n) or linear. fire 23 Finding the sum of three numbers #include void main() { int x,y,z, 5 printf(“Enter the three numbers”); | scanf(" Xd Xd Xd”, 8x, 8y,8z); | sum = x+y +z; print#(“The sum = Xd", sum); Finding the sum of array elements int Fact(int n) it if(n < = 1) return 1; else return(n * Fact(n-1)); In Example 23, there are no instance characteristics and the space needed by x y, z and ‘is independent of instance characteristics. The space for each integer jabfe is 4. We have 4 integer variables and space needed by x, y, z and sum are 4 x 4 = 16 bytes. S(P) =C,+5, S(P) =16+0 S(P) =16 Hence, space complexity for the above given program is O(1), or constant. In Example 24, The program is recursive and stack space includes space for the formal parameter, local variables and the return address. Here the problem instance are characterized by 'n’. The depth of Tecursion Total Space sa ns4n The depth of recursion = [The depth of recursion The space Complexity fo dey . | —= O(n) or tinea: SM ato cote : 5 2 : B & 1) =¢, ADD(n) + t, SUB(n) +t, MUL(M) + -- - Where n indicates the ins ance characteristics and t, t,t, --- denote the time needed foran addition, subtraction, multiplication, and so on, ADD, SUB, MUL - - - represent the functions and they are Performed when the code for the program is used on an in stance characteristic 'n’ uch an exact formula is itself an impossible task, since the time needed for an addition, ‘wbtraction, multiplication and so on, often depends on the numbers being added, subtracted, multiplied and so on, Obtaining si ¢ Value of t,(n) for any given ‘n' can be obtained only experimentally. Definition: Ti, ‘*xity of an algorithm is the total amount of time required by an algorithm to complete its Measuring an Input's Size - inPUt size is denoted by 'n’ and we Bs pacites the number of array elem: N indicates the degree a Tn, é use 'n' in most of the algorithms. In searching and sorting ents, in matrix manipulation n indicates the matrix order, a ind in travelling sales man problem n indicates the number of re the input size n is very much important in analyzing the algorithm. snanon ofan algorithm will be focussed on input size'n', Some Soraeae ee 5 for aye nica the size oftheir inputs. For example, the number of vertices Finally, ye ean tS ON graphs represented by adjacency linked lists Y tion of some Patametes can say that it is logical to investigate an algorithm's efficiency as a funct re a "nindicating the algorithm's input size. “lynomials ‘ities. Therefo, “analysis rameter t, "ee mmm 1sea en the ses ape ve xa ™ \\ cu x er FREER 011s for Measuring Running Time We cannot measure the running time by seconds, milliseconds, and so on because sucha measurement depends on the type of computer, compiler and the program. We would like to have a metric that does not depend on these extraneous factors. Followin, some of the metods of computing the time pffietency of algorithms. 4Dperation Counts 4 Step Counts ‘mptotic notations (mathematical analysis) Consider an algorithm ‘A’ with 'n’ size of the input data. The time and space are the two main measures for the efficiency of the algorithm. In operation counts, the time is measured by counting the number of basic operations or key operations. The basic operations are defined that the time for the other operations is much less than or almost proportional to the time for the basic operations. Ea ae a | Aza*b; This code takes 1 unit of time | for (i = @; i < ns i ++) ‘This code takes 'n' units of time because, | asa+t it executes for n times. for (j i icon; i ++) This code takes 'n® units of time | | printf (" Hello"); | The operation count method concentrates on certain important basic operations like multiplications, for loop or while loop where it takes considerably more time than any other operations in an algorithm. Bubble Sort | Bubble sort is most simplest method of sorting a set of elements. This method is suitable only for small arrays. Here adjacent elements are compared and swapped for (n-1) number of times. With each iteration, a value | moves like a bubble to the top of the array. | void bubble_sort (int a [], int n) { for (pass Pass< n-1 ; pass++) for (1 = 0; i <= n-pass-1; i++) { int pass, i; | if (a{4) > afi+1]) swap (a[i), a[i+i]; Here the basic operations is comparisons. At the end of each pass, the largest element will be bubbled and Occupy the last position. In first pass, (n-1) comparisons will be made, in second pass, (n-2) comparisons will be made and so on. Therefore the total number of comparisons made is n(n-1)_né—=n 2 T(n)= (n-1) + (n-2)+--+3+2+1= =n? [Since n? > n and neglecting constant 2} Evaluation of Polynomial ‘p” represented as : 7 tonne |? tc mn eel nomial " 10) © yaluate the polynomial ofthe above type forthe given "yane - tet ond. the following program is considered ek 7 polyéval (int c{], int n, int x) Pe me : Therefore two multiplicatn, pen one additions that ate mee we toys vue for ‘n’ iterations Therefore, te ree time efficiency of above code is 24 ert can multiplications and n additions } return value; aa Instep unts method, we attempt to find the time spent in all parts ofthe program. A step isa computational unit that is Tim Tistice ny i xeaeb fi. for (i = 15 4 < = nj itt) xeatb iii i itt) 4; 5 <= nj J++) avd Finding the Sum of all array elements ie sum(int af], int n) | int 4, sum = 0; sum_count++; | hae (1 = 0; 4 function f(n) ‘not consume more id i {(=) = O(g{n)) suck that there exists two positive constants ‘cand ‘n,’ with the constraint that if(n)(FcTet)| vaca, tes ofc ef wo think of a8 some gwen function and {2s the function to analyze. ‘The iasportant point is that {6 bounded above by some constant multiple of g(n) forall large aves #7 us assumed EW a real fi The notation Ogfa)] “big ob of g of a”. tn this notation f{n) grows no more than g(°} ad I f(n) = O(n’) cP sete | vnen, jsneziselnl vnen, «should find the value of c and n0 such that the above inequality can be satisfied were gn 2187181 va2t pe sove neat CAR be satisfied by setting ¢ = 7 and n, = 1. (always find the minimum possible value of ad fn), — that g (n) = O(n) ere g(n) = n and big ~ oh ~ notation constraint is ifn) isclg()! Vnzn, [3n+3|scin] vn2n, 3n+2s4n vn22 or 3n+2<5n The above inequality can be satisfied by setting c= f(n) = 0 (n) Prevonzie | Given f(n) = 10n? + 4n + 2 Prove that F(n) = 0 (n') 1 f(a) |< | g(a) | vnzn, [10n?7+4n+2|sc[n’| vn2n, [10n?+4n+2)<11|n?] ynes 10n?+4n+2 Lin? ynes . The ‘hove inequality can be satisfied by setting n, = S and c= 11. Given | £(n) = 10° n? Prove that f(n) = 0 (n*) Choose Onl sein vnzn, £*10'andn, = 1, we get 110* 2 Sa mls 10¢ni vnel (0) = O(n?) is proved. On? ~ 5 Prove that f(n) = O(n") Iselin] | 20n'-Ssen'¥n2n, | choose c= 20 andn, = 1 20n'-5s20n? f{n) = 0(n°) is proved. Given f(n) = 100n +5 Prove that f(n)= O(n") == vnen, | | 100n +5} sc. |n'} 100n +5 s 105.n? ‘The above inequality can be satisfied by setting c = 105 and n, £(n) = O(n’) is proved. BBE mes Notation ca) This notation is used to find the lower bound behaviour of f(n). The lower bound implies that below this time the algorithm cannot perform better, ig. the algorithm wil ta . (this indicates tre Tower bound} 11s Tépresented mathematically by notation 2 For a given function 8(n), we denote By S7EUY (pronounced as omega ~ of gof n). The function a(n) is only alower bound on f{n). For the statement f(n) = Q(g (n) to be informative, g(n) should be as large a function of nas Possible for which the statement f(n) = 0 (g(n)) is true. vne. Definition: Omega Notation (Q) An) = 2(e(n)) 'fand only if there exists two positive constants ¢ and nO with the constraint Mf) 12 ¢]g@ | Vnen, constant factor c) on the value off —__. Here cis some positive constant. Thus g isa lower bound (except for all suitably fargen- a a = Given, f(n) = Sn +2 Prove that f(n) = (n) Here g(n) isn and omega notation constraint is OTEEO nen, [5n+2]>cIn} vn2n, Since Sn + 2 Is always greater than Sn we can choose 1 Sn+225n vnei The above inequality can be satisfied ‘according to omega-notation by setting = £(n) = 2(n) is proved, _ Prove that f(n) = 0 (25) goyis2eandtwe have the constraint ag ore e1R(") | Vnen, a. gpentze? Vn2n, gzent2 1 vnea ove inequality can be satsfled by setting ¢= tan y . bed f(n) = 2(2") Is proved, en fit)= 10n! + 4n + 3 Prove that f(n) = am) m 10n?+4n+32c.n Yo2a, choasec = 1 and n, = 1, we get 10n?+ 4n+321.n eA Theabove inequality can be satisfied by setting c= 1 and n, = f(n) = Q(n) is proved, PEE theta Notation (0) Thetheta notation can be used when the fui ction f(n) can be bounded both from above and below ie same Tunction g(n). For some functions, the lower bound apt upper bound may be same. In st ons having the same time complexity for lower bound and 4 upper bound and this notation is called the theta - notation and which is denoted by 8. Definition: Theta Notation (0) i a | f(n) = 0(g(n)) ‘and otf There exists three Positive constants ¢,,c, and n, with the constraint that | : ee vane, 7 1 oo that 2™ ~3n=0(n*) To | Prove the above Statement, we must determine positive constants ¢,,¢, and n, such that 1 Oi Son? ~3n sen? vn2n, to 1 ihe inequality 4-2 b f(n) = @(g(n)) =a=b Using limits for Comparing Orders of Growth The asymptotic notations like 0, © and 0 are rarely used for comparing orders of growth of two ‘Peclc functions, A much more convenient method for doing so is based on computing the limit of ‘ratio oftwo functions. There exists three cases for doing so. (n) [implies that ((n) has smaller order of growth than a(n) am) © implies that f(n) has the same order of growth g(n) % — implies that f(n) has larger order of growth than g(n) : oan Q) and only second case is for thay" 8888 are for Big-oh(0), the last two cases are for omega(®) (DEEN 200i = sia3 Y= lim 5+3y=5 tim £0) «i200 =3 im | eee : | Since 20 isa constant, ~. f{n) = O(n’) is proved. | (DRIES Given that f(a) = 2n, then prove that f(n) = O(n) LS £0) sn? tea) ee Weare concerned here about the classification of algorithms according to their asymptotic efficiencies Some times the time efficiencies of a large number of algorithms fall into only a few classes. These classes are listed in the following table in increasing order of their orders of growth alongwith ther names and comments. Class | __Name Comments a constant _ | Short of best-case efficiencies, very few reasonable examples can be given since 20| algorithm's running time typically goes to infinity when its input size grows infinitely large. logn | logarithmic | Typically, a result of cutting a problem's size by a constant factor on each iteration of the algorithm. Note that a logarithmic algorithm cannot take into account als input(or even a fixed fraction of it) : any algorithm that does so will have at ast linear running time. n linear __| Algorithms that scan a list of size n(e.g., sequential search) belong to this lass. nlogn | linearithmic | Many divide-and-conquer algorithms including mergesort and quicksort in average case, fall into this category. m | quadratic | Typically, characterizes efficiency of algorithms with two embedded HP ‘i a Elementary sorting algorithms and certain operations on n-by-n matrices standard examples. a eee Fra | Teal characteris cen stagpmimsae gee - —/—T abi : inva _ WF nontrivial algorithms from linear agebrafallintothievigg ed oops Sewera ‘Typical for algorithms that generate all subsets of an mel “exponential” is used in a broader sense to include this and faster nano eM faster orders of growth Typical for algorithms that generate all permutations ofan element set zs el 1 ‘exponential =| factorial 1 gxplin different mathematical functions and notations. 2 Bxplin Algorithmic Notations with an example. 3, Explain different control structures with an example. 4, Explain time complexity of an algorithm, 5, Explain space complexity of an algorithm. 6 Write a note on operation count and step count. 7. List out various asymptotic notations and their significance. 8, Arrange the following complexities in ascending sequence. Give reasons. O(nlogn) O(n3) O(n) O(n2) O(1) Derive space complexity for finding sum of array elements. 10. Explain the active areas of study of algorithms. 11 Briefly explain best case, average case and worst case time complexity of an algorithm. 12. What do you mean by orders of growth? 13. What are the units for measuring running time? 44. Demonstrate that the computing time to multiply the square matrices is of O(n3). 'S. Given (n) = 120n+20, Prove that f(n) = 0(n2). Given f(n) = 20n2+10n¢5, Prove that f(n) = Q(n). Given that f(n) = 30n3-2, Prove that f(n) = O(n3) using limits of the ratio of two functions. Letf(n) = 100n+5. Express f(n) using Big - Omega and Big - Oh. Prove that 3n3+4n2 = 0(n3). a au : 100n+5. Express f(n) using Big - Theta. ‘me and space complexity with an example. 2 Bing " Sutthe space required for the following algorithms. . Matrix Multiplication b. Factorial of anumber 23. py * Find out the ¢ tthe time required to run the matrix multiplication using ao Peration counts b. Step counts + Explain ‘Worst-case, best-case, and arrange-cas¢ efficiencies? Explain with an example. & KEK ore 25. Explain whatare the basic steps that are to be followed to analyze recursive and Non-recursive algorithms. Explain with an example. 26. Explain asymptotic notations and give an example for each. 27. Design an algorithm to obtain maximum of N elements and obtain the time complexity, 28. Calculate the time complexity for the following recursive algorithms. a. Factorial b. GCD c. Fibonacci series 29. Write an algorithm to add two matrices and explain how its time complexity is obtained by introducing count statements. 30. Write an algorithm for bubble sort and prove that T(n) = n?. 31. Write an algorithm for sequential search and explain best-case, worst-case and average-case efficiencies. Q000

You might also like