Upcoming SlideShare
×

# Introductionto analysis of algorithms

7,848 views

Published on

Success key book for:

* Campus Preparation

* Degree/Masters Course Preparation

* Instructor’s

* GATE Preparation

* Big job hunters: Microsoft, Google, Amazon, Yahoo, Flip Kart, Adobe, IBM Labs, Citrix, Mentor Graphics, NetApp, Oracle, Webaroo, De-Shaw, Success Factors, Face book, McAfee and many more

* Reference Manual for working people

Published in: Education, Technology
31 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No
Views
Total views
7,848
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
2
3
Likes
31
Embeds 0
No embeds

No notes for slide

### Introductionto analysis of algorithms

1. 1. ©www.CareerMonk.comData Structures and Algorithms Made Easy Chapter 2 ANALYSIS OF ALGORITHMSIntroductionThis chapter will create a base for all upcoming chapters. The objective of this chapter is toexplain the importance of analysis of algorithms, their notations, relationships and solving asmany problems as possible. We first concentrate on understanding the importance of analysisand then slowly move towards analyzing the algorithms with different notations and finally,the problems. After completion of this chapter you will get the confidence in finding thecomplexities of any given algorithm (especially recursive functions).What is an Algorithm?Just to understand better, let us consider the problem of preparing an omelet. For preparingomelet, general steps which we follow are: 1) Get the frying pan. 2) Get the oil. a. Do we have oil? i. If yes, put it in the pan. ii. If no, do we want to buy oil? 1. If yes, then go out and buy. 2. If no, we can terminate. 3) Turn on the stove. 4) Etc...The above steps says that, for a given problem (in this case, preparing an omelet), giving stepby step procedure for solving it. So, the definition of algorithm can be given as: step-by- An algorithm is the step-by-step instructions to a given problem.One important thing to remember while writing the algorithms is, we do not have to proveeach step. Also, all algorithms involve the basic elements which we have discussed in chapter. 66 Analysis of Algorithms | CareerMonk Publications ™
2. 2. ©www.CareerMonk.comData Structures and Algorithms Made Easy lgorithms?Why Analysis of Algorithms?If we want to go from city “ ” to city “ ”. There can be many ways of doing this: by flight,by bus, by train and also by cycle. Depending on the availability and convenience we choosethe one which suits us. Similarly, in computer science there can be multiple algorithms existfor solving the same problem (for example, sorting problem has lot of algorithms likeinsertion sort, selection sort, quick sort and many more). Algorithm analysis helps usdetermine which of them is efficient in terms of time and space consumed.Goal of Analysis of Algorithms?The goal of is to compare algorithms (or solutions) mainly in termsof running time but also in terms of other factors (e.g., memory, developers effort etc.)What is Running Time Analysis? nalysis?It is the process of determining how processing time increases as the size of the problem(input size) increases. Input size is number of elements in the input and depending on theproblem type the input may be of different types. In general, we encounter the followingtypes of inputs. • Size of an array • Polynomial degree • Number of elements in a matrix • Number of bits in the binary representation of the input • Vertices and edges in a graphHow to Compare Algorithms?To compare algorithms, let us define some .Execution times? as execution times are specific to a particular computer.Number of statements executed? since the number of statements varies with the programming languageas well as the style of the individual programmer. Solution?Ideal Solution 67 Analysis of Algorithms | CareerMonk Publications ™
3. 3. ©www.CareerMonk.comData Structures and Algorithms Made EasyLet us assume that we expressed running time of given algorithm as a function of the inputsize (i.e., ). We can compare these different functions corresponding to running timesand this kind of comparison is independent of machine time, programming style, etc..What is Rate of Growth?The rate at which the running time increases as a function of input is called rate of growth. . This is because costLet us assume that you went to a shop for buying a car and a cycle. If your friend sees youthere and asks what you are buying then in general we sayof car is too big compared to cost of cycle ( approximating the cost of cycle to cost of car). Total Cost = cost_of_car + cost_of_cycle Total Cost cost_of_car (approximation)For the above example, we can represent the cost of car and cost of cycle in terms of functionlarge value of input size, ). As an example in the below case, , 2 , 100 and 500 are theand for a given function we ignore the low order terms that are relatively insignificant (forindividual costs of some function and we approximate it to . Since, is the highest rate of 2 100 500growth.Commonly used Rate of GrowthsBelow is the list of rate of growths which come across. complexityTime complexity Name Example 1 Constant Adding an element to the front of a linked list Logarithmic Finding an element in a sorted array Linear Finding an element in an unsorted array Linear Logarithmic Sorting n items by ‘divide-and-conquer’-Mergesort Quadratic Shortest path between two nodes in a graph 2 Cubic Matrix Multiplication Exponential The Towers of Hanoi problemBelow diagram shows the relationship between different rates of growth. 68 Analysis of Algorithms | CareerMonk Publications ™
4. 4. ©www.CareerMonk.comData Structures and Algorithms Made Easy 2 ! D e c 4 r e a 2 s i n g log R log ! a t e s O 2 f G r o w t h log log 1Types of AnalysisAssume that we have an algorithm for a problem and want to know on what inputs thealgorithm is taking less time (performing well) and on what inputs the algorithm is takinghuge time.We have already seen that an algorithm can be represented in the form of an expression.That means we represent the algorithm with multiple expressions: one for case where it istaking the less time and other for case where it is taking the more time. In general the first 69 Analysis of Algorithms | CareerMonk Publications ™
5. 5. ©www.CareerMonk.comData Structures and Algorithms Made Easycase is called the best case for the algorithm and second case is called the worst case for thealgorithm.To analyze an algorithm we need some kind of syntax and that forms the base for asymptoticanalysis/notation. There are three types of analysis:• Worst case o Defines the input for which the algorithm takes huge time. o Input is the one for which the algorithm runs the slower.• Best case o Defines the input for which the algorithm takes lowest time. o Input is the one for which the algorithm runs the fastest.• Average case o Provides a prediction about the running time of the algorithm o Assumes that the input is random Lower Bound Average Time Upper BoundFor a given algorithm, we can represent best case, worst case, average case analysis in theform of expressions. As an example, let be the function which represents the givenalgorithm. Then, the following expression for best case and worst case. 500, for worst case 100 500 , for best caseSimilarly, for average case too. The expression defines the inputs with which the algorithmtakes the average running time (or memory).Asymptotic notation?Having the expressions for best case, average case and worst case, to find the upper boundsand lower bounds for each of them: for all the three cases we need to identify the upperbound, lower bounds. In order to represent these upper bound and lower bounds we needsome syntax and that creates the base for following discussion.For the below discussions, let us assume that the given algorithm is represented in the formof function .Big-Big-O notation 70 Analysis of Algorithms | CareerMonk Publications ™
6. 6. ©www.CareerMonk.comData Structures and Algorithms Made EasyThis notation gives the upper bound of the given function. Generally we represent itas . That means, at larger values of , the upper bound of is .For example, if 100 10 50 is the given algorithm, then is .That means gives the maximum rate of growth for at larger values of . : there exist positive constants and n such that 0Now, let us see the notation with little more detail. notation defined as for all . is an asymptotic tight upper bound for . Our objective is to give some rate ofgrowth which is greater than given algorithms rate of growth .In general, we do not consider lower values of . That means the rate of growth at lowervalues of is not important. In the below figure, is the point from which we considerthe rate of growths for a given algorithm. Below the rate of growths may be different. Rate of Growth Input Size,Big-Big-O Visualization 1 100,1000, 3 100, 200,1,20, . 100 , 2 1, 3, . 5 ,3 100, ,5 10, 100, 2 1, 100, 100 , 2 1, 5, . . . For example, includes 1 , , is the set of functions with smaller or same order of growth as etc.. 71 Analysis of Algorithms | CareerMonk Publications ™
7. 7. ©www.CareerMonk.comData Structures and Algorithms Made EasyNote: Analyze the algorithms at larger values of only. What this means is, below we donot care for rates of growth.Big-Big-O examplesExample-Example-1 Find upper bound for 3 8Solution: 3 8 4 , for all 1 ∴3 8= with c = 4 and n 8Example-Example-2 Find upper bound for 1 1 2 , for all 1 ∴ 1= with c = 2 and n 1Solution:Example-Example-3 Find upper bound for 100 50 100 50 2 , for all 1 ∴ 100 50 = with c = 2 and n 100Solution:Example-Example-4 Find upper bound for 2 2Solution: 2 2 2 , for all 1 ∴2 2 = 2 with c = 2 and 1Example-Example-5 Find upper bound for , for all 1 ∴ = 1Solution: with c = 1 andExample-Example-6 Find upper bound for 410Solution: 410 410, for all 1 ∴ 100 1 with c = 1 and n 1No uniqueness?consider, 100 5 . For this function there are multipleThere is no unique set of values for and in proving the asymptotic bounds. Let us and . 72 Analysis of Algorithms | CareerMonk Publications ™
8. 8. ©www.CareerMonk.comData Structures and Algorithms Made EasySolution1: 100 5 100 101 101 5, 5 and101 is a solution. for allSolution2: 100 5 100 5 105 105 1, 1 and 105 is also a solution. for allOmega-Omega- notationSimilar to above discussion, this notation gives the tighter lower bound of the given .algorithm and we represent it as Ω . That means, at larger values of , thetighter lower bound of isFor example, if 100 10 50, is Ω .The Ω notation as be defined as Ω : there exist positive constants andn such that 0 0 . . Ω for all is an asymptotic lower bound for is the set of functions with smaller or same order of growth as . Rate of Growth Input Size, ExamplesExample-Example-1 Find lower bound for 5Solution: ∃ , n Such that: 0 ≤ ≤ 5 ⇒ ≤5 ⇒ 1 and n = 1 ∴5 Ω with 1 and =1Example-Example-2 Prove 100 5 ΩSolution: ∃ c, n Such that: 0 ≤ ≤ 100 5 100 5 ≤ 100 5 ∀ ≥1 105 73 Analysis of Algorithms | CareerMonk Publications ™
9. 9. ©www.CareerMonk.comData Structures and Algorithms Made Easy ≤ 105 ⇒ – 105 ≤ 0 Since is positive ⇒ – 105 ≤ 0 ⇒ ≤ 105/ ⇒ Contradiction: cannot be smaller than a constantExample-Example-3 Ω 2 , Ω , ΩTheta-Theta-θ notationThis notation decides whether the upper and lower bounds of a given function are same ornot. The average running time of algorithm is always between lower bound and upper 10bound. If the upper bound ( ) and lower bound (Ω) gives the same result then notationwill also have the same rate of growth. As an example, let us assume that isthe expression. Then, its tight upper bound is . The rate of growth in best caseis . In this case, rate of growths in best case and worst are same. As a result, theaverage case will also be same.Note: For a given function (algorithm), if the rate of growths (bounds) for and Ω are notsame then the rate of growth case may not be same. c Rate of Growth c Input Size,Now consider the definition of notation. It is defined as : there existpositive constants , and n such that 0 0 . . for all . is an asymptotic tight bound for is the set of functions with the sameorder of growth asθ ExamplesExample-Example-1 Find θ bound for 74 Analysis of Algorithms | CareerMonk Publications ™
10. 10. ©www.CareerMonk.comData Structures and Algorithms Made EasySolution: , for all, n≥ 1 ∴ θ with 1/5, 1 and n = 1Example-Example-2 ProveSolution: ⇒ only holds for: 1/ ∴ ΘExample-3 Prove 6Example- ≠ /6 ∴6Solution: n2 ≤ 6 ≤ ⇒ only holds for: ΘExample-Example-4 ProveSolution: log ⇒ ,∀ n0 – ImpossibleImportant NotesFor analysis (best case, worst case and average) we try to give upper bound ( ) and lowerbound (Ω) and average running time . From the above examples, it should also be clearthat, for a given function (algorithm) getting upper bound ( ) and lower bound (Ω) andaverage running time ( ) may not be possible always.For example, if we are discussing the best case of an algorithm, then we try to give upperbound ( ) and lower bound (Ω) and average running time ( ).In the remaining chapters we generally concentrate on upper bound ( ) because knowinglower bound (Ω) of an algorithm is of no practical importance and we use θ notation if upperbound ( ) and lower bound (Ω) are same. calledWhy is it called Asymptotic Analysis?From the above discussion (for all the three notations: worst case, best case and average case),we can easily understand that, in every case for a given function we are trying to findother function which approximates at higher values of . That means, is alsoa curve which approximates at higher values of . In mathematics we call such curve as 75 Analysis of Algorithms | CareerMonk Publications ™
11. 11. ©www.CareerMonk.comData Structures and Algorithms Made Easy . In other terms, is the asymptotic curve for . For this reason, wecall our analysis as .Guidelines for asymptotic analysis?There are some general rules to help us in determining the running time of an algorithm.Below are few of them.1) Loops The running time of a loop is, at most, the running time of the statements inside Loops: the loop (including tests) multiplied by the number of iterations. // executes times for (i=1; i<=n; i++) { m = m + 2; // constant time, c } Total time = a constant .2) Nested loops: Analyze from inside out. Total running time is the product of the sizes of loops: all the loops. //outer loop executed n times for (i=1; i<=n; i++) { // inner loop executed n times for (j=1; j<=n; j++) { k = k+1; //constant time } } Total time .3) Consecutive statements: Add the time complexities of each statement. statements: x = x +1; //constant time // executed n times for (i=1; i<=n; i++) 76 Analysis of Algorithms | CareerMonk Publications ™
12. 12. ©www.CareerMonk.comData Structures and Algorithms Made Easy { m = m + 2; //constant time } //outer loop executed n times for (i=1; i<=n; i++) { //inner loop executed n times for (j=1; j<=n; j++) { k = k+1; //constant time } } Total time .4) If-then-else statements: Worst-case running time: the test, plus either the then part or If-then- statements: the else part (whichever is the larger). //test: constant if (length ( ) != otherStack. length ( ) ) { return false; //then part: constant } else { // else part: (constant + constant) * n for (int n = 0; n < length( ); n++) { // another if : constant + constant (no else part) if (!list[n].equals(otherStack.list[n])) //constant return false; } } Total time . problem size by a fraction (usually by ½).5) Logarithmic complexity: An algorithm is complexity: if it takes a constant time to cut the 77 Analysis of Algorithms | CareerMonk Publications ™
13. 13. ©www.CareerMonk.comData Structures and Algorithms Made Easy As an example let us consider the following program: for (i=1; i<=n;) { i = i*2; } 1, 2, and in subsequent steps 4, 8 and so on. If we observe carefully, the value of i is doubling every time. That means, initially in next step Let us say the loop is executing some times. That means at step 2 n and we come out of loop. So, if we take logarithm on both sides, 2 2 //if we assume base-2 So the total time .Note: Similarly, for the below case also the worst case rate of growth is . That means,the same discussion holds good for decreasing sequence also. for (i=n; i<=1;) { i = i/2; }Another example algorithm (binary search): finding a word in a dictionary of pages • Look at the centre point in the dictionary • Is word towards left or right of centre? • Repeat process with left or right part of dictionary until the word is foundProperties of notations • Transitivity: Θ and Θ ⇒ Θ . Valid for O . Valid for O and Ω also. and Ω also. . • Reflexivity: Θ • Symmetry: Θ if and only if Θ 78 Analysis of Algorithms | CareerMonk Publications ™
14. 14. ©www.CareerMonk.comData Structures and Algorithms Made Easy • Transpose symmetry: O if and only if Ω .Commonly used logarithms and summationsLogarithms –Arithmetic series 1 1 2 2Geometric series x x … x n 2 xn 1 1 1 x x 1 x 1 k 1 1 1 1HarmonicHarmonic series 1 … log 2Other important formulae log 1 1 2 1Master Theorem for Divide and ConquerIn all efficient divide and conquer algorithms we will divide the problem into subproblems,each of which is some part of the original problem, and then perform some additional workto compute the final answer. As an example, if we consider merge sort [for details, refer chapter], it operates on two problems, each of which is half the size of the original,and then uses additional work for merging. This gives the running time equation: 79 Analysis of Algorithms | CareerMonk Publications ™
15. 15. ©www.CareerMonk.comData Structures and Algorithms Made Easy 2 2The following theorem can be used to determine the running time of divide and conqueralgorithms. For a given program or algorithm, first we try to find the recurrence relation forthe problem. If the recurrence is of below form then we directly give the answer withoutfully solving it.If the reccurrence is of the form , where 1, 1, 0and is a real number, then we can directly give the answer as: If a b , then n 2) If a b 1) 1, then n 1, then n loglogn a. If 1, then n s b. If If a b c. If 0, then n 3) 0, then n a. If b. IfProblems on Master Theorem for Divide and ConquerFor each of the following recurrences, give an expression for the runtime if therecurrence can be solved with the Master Theorem. Otherwise, indicate that the MasterTheorem does not apply.Problem-Problem-1 3 /2Solution:Solution: 3 /2 (Master Theorem Case 3.a)Problem-Problem-2 4 /2Solution:Solution: 4 /2 (Master Theorem Case 2.a)Problem-Problem-3 /2 2Solution:Solution: /2 2 2 (Master Theorem Case 3.a)Problem-4Problem- 2 /2Solution:Solution: 2 /2 => Does not apply ( is not constant) 80 Analysis of Algorithms | CareerMonk Publications ™
16. 16. ©www.CareerMonk.comData Structures and Algorithms Made EasyProblem-Problem-5 16 /4Solution:Solution: 16 /4 (Master Theorem Case 1)Problem-Problem-6 2 /2Solution:Solution: 2 /2 (Master Theorem Case 2.a)Problem-Problem-7 2 /2 /Solution:Solution: 2 /2 / loglogn (Master Theorem Case2.b)Problem-Problem-8 2 /4 .Solution:Solution: 2 /4 . . (Master Theorem Case 3.b)Problem-Problem-9 0.5 /2 1/Solution:Solution: 0.5 /2 1/ Does not apply ( 1)Problem-Problem-10 6 /3Solution:Solution: 6 /3 (Master Theorem Case 3.a)Problem-Problem-11 64 /8Solution:Solution: 64 /8 Does not apply (function is not positive)Problem-Problem-12 7 /3SolutionSolution: 7 /3 (Master Theorem Case 3.as)Problem-Problem-13 4 /2Solution:Solution: 4 /2 (Master Theorem Case 1)Problem-Problem-14 /2 2 /2 2 Does not apply. We are in Case 3, but theregularity condition is violated. (Consider = 2Πk, where is odd and arbitrarily large. ForSolution:Solution:any such choice of , you can show that 3/2, thereby violating the regularitycondition.)Problem-Problem-15 16 /4 ! 81 Analysis of Algorithms | CareerMonk Publications ™
17. 17. ©www.CareerMonk.comData Structures and Algorithms Made EasySolution:Solution: 16 /4 ! ! (Master Theorem Case 3.a)Problem-Problem-16 √2 /2Solution:Solution: √2 /2 √ (Master Theorem Case 1)Problem-Problem-17 3 /2Solution:Solution: 3 /2 (Master Theorem Case 1)Problem-Problem-18 3 /3 √Solution:Solution: 3 /3 √ (Master Theorem Case 1)Problem-Problem-19 4 /2Solution:Solution: 4 /2 (Master Theorem Case 1)Problem-Problem-20 3 /4Solution:Solution: 3 /4 (Master Theorem Case 3.a)Problem-Problem-21 3 /3 /2Solution: olution: 3 /3 /2 (Master Theorem Case 2.a)Master Theorem for Subtract and Conquer RecurrencesLet be a function defined on positive , and having the property c, if 1 aT n b f n , if 1for some constants , 0, 0, 0, and function . If is in , then O n , if a 1 O n , if a 1 O n a , if a 1Variant of subtraction and conquer master theorem theorem 1 , where 0 1 and 0 are constants, is .The solution to the equation 82 Analysis of Algorithms | CareerMonk Publications ™
18. 18. ©www.CareerMonk.comData Structures and Algorithms Made Easy AlgorithmsProblems on Algorithms Analysis , ,Note: From the following problems, try to understand in what cases we get differentcomplexities ( etc..).Problem-Problem-22 Find the complexity of the below recurrence: 3 1 , 0, 1,Solution: Let us try to solve this function with substitution. 3 1 3 3 2 3 2 3 3 3 . . 3 3 0 3This clearly shows that the complexity of this function is 3 .Note: We can directly use the master theorem for this problem.Problem-Problem-23 Find the complexity of the below recurrence: 2 1 1, 0, 1,Solution:Solution: Let us try to solve this function with substitution. 2 1 1 2 2 2 1 1 2 2 2 1 2 2 3 2 1 1 2 4 2 2 2 2 2 2 2 ….2 2 2 2 2 2 2 ….2 2 2 2 2 1 :2 2 2 2 1So the complexity is 1 . Note that while the recurrence relation looks exponential thesolution to the recurrence relation here gives a different result. 83 Analysis of Algorithms | CareerMonk Publications ™
19. 19. ©www.CareerMonk.comData Structures and Algorithms Made EasyProblem-Problem-24 What is the running time of the following function (specified as a function of the input value n) void Function(int n) { int i=1 ; int s=1 ; while( s ≤ n) { i++ ; s= s+i ; print(*"); } }Solution:Solution: Consider the comments in below function: void Function (int n) { int i=1 ; int s=1 ; // s is increasing not at rate 1 but i while( s ≤ n) { i++ ; s= s+i ; print(“*"); } }We can define the terms ‘ ’ according to the relation i. The value of ‘ ’ increasesby one for each iteration. So the value contained in ‘ ’ at thefirst ‘ ’ positive integers. If is the total number of iterations taken by the program, then the iteration is the sum of thewhile loop terminates once. 1 1 2 ... √ . 2Problem-Problem-25 Find the complexity of the function given below. void Function(int n) { 84 Analysis of Algorithms | CareerMonk Publications ™
20. 20. ©www.CareerMonk.comData Structures and Algorithms Made Easy int i, count =0;; for(i=1; i*i<=n; i++) count++; }Solution: Consider the comments in below function:void Function(int n){ int i, count =0;; for(i=1; i*i<=n; i++) count++;} √as that of Problem-24.In the above function the loop will end, if . The reasoning is sameProblem-Problem-26 What is the complexity of the below program: void function(int n) { int i, j, k , count =0; for(i=n/2; i<=n; i++) for(j=1; j + n/2<=n; j= j++) for(k=1; k<=n; k= k * 2) count++; }Solution:Solution: Let us consider the comments in the following function. void function(int n) { int i, j, k , count =0; //outer loop execute n/2 times for(i=n/2; i<=n; i++) //Middle loop executes n/2 times for(j=1; j + n/2<=n; j= j++) //outer loop execute logn times for(k=1; k<=n; k= k * 2) count++; . }The complexity of the above function is 85 Analysis of Algorithms | CareerMonk Publications ™
21. 21. ©www.CareerMonk.comData Structures and Algorithms Made EasyProblem-Problem-27 What is the complexity of the below program: void function(int n) { int i, j, k , count =0; for(i=n/2; i<=n; i++) for(j=1; j<=n; j= 2 * j) for(k=1; k<=n; k= k * 2) count++; }Solution:Solution: Let us consider the comments in the following function. void function(int n) { int i, j, k , count =0; //outer loop execute n/2 times for(i=n/2; i<=n; i++) //Middle loop executes logn times for(j=1; j<=n; j= 2 * j) //outer loop execute logn times for(k=1; k<=n; k= k*2) count++; . }The complexity of the above function isProblem-Problem-28 Find the complexity of the below program. function( int n ) { if ( n == 1 ) return ; for( i = 1 ; i <= n ; i + + ) { for( j= 1 ; j <= n ; j + + ) { print(“*" ) ; break; } } }Solution: Let us consider the comments in the following function.function( int n ) 86 Analysis of Algorithms | CareerMonk Publications ™
22. 22. ©www.CareerMonk.comData Structures and Algorithms Made Easy{ //constant time if ( n == 1 ) return ; //outer loop execute n times for( i = 1 ; i <= n ; i + + ) { // inner loop executes only time due to break statement. for( j= 1 ; j <= n ; j + + ) { print(“*" ) ; break; } }}The complexity of the above function is . Even though the inner loop is bounded by n,but due to the break statement it is executing only once. .Problem-Problem-29 Write a recursive function for the running time of the function function, whose code is below. Prove using the iterative method that function( int n ) { if ( n == 1 ) return ; for( i = 1 ; i <= n ; i + + ) for( j = 1 ; j <= n ; j + + ) print(“*" ) ; function( n-3 ); }Solution:Solution: Consider the comments in below function: function (int n) { //constant time if ( n == 1 ) return ; //outer loop execute n times for( i = 1 ; i <= n ; i + + ) //inner loop executes n times for( j = 1 ; j <= n ; j + + ) 87 Analysis of Algorithms | CareerMonk Publications ™
23. 23. ©www.CareerMonk.comData Structures and Algorithms Made Easy //constant time print(“*" ) ; function( n-3 ); }The recurrence for this code is clearly T 3 for some constant 0since each call prints out asterisks and calls itself recursively on n - 3. Using the iterativemethod we get: 3Using the master theorem, we get .Problem-Problem-30 Determine bounds for the following recurrence relation: 2 2Solution:Solution: Using Divide and Conquer master theorem, we get .Problem-Problem-31 Determine bounds for the following recurrence /2 /4 /8Solution:Solution: Substituting in the recurrence equation, we get: 1 2 3 2 4 8 , where k is a constant.Problem-Problem-32 Determine Θ bounds for the following recurrence relation:  /2 7Solution: Using Master Theorem we getSolution: .Problem-33 Prove that the running time of the code below is ΩProblem- . Read(int n); { int k = 1 ; while( k < n ) k = 3k; } 88 Analysis of Algorithms | CareerMonk Publications ™
24. 24. ©www.CareerMonk.comData Structures and Algorithms Made EasySolution: The while loop will terminate once the value of ‘ ’ is greater than or equal to thevalue of ‘ ’. Since each loop the value of ‘ ’ is being multiplied by 3, if i is the number ofSolution:iterations, then ‘ ’ has the value of 3i after i iterations. That is the loop is terminated uponreaching i iterations when 3 ≥ n ↔ i ≥ log , which shows that Ω .Problem-Problem-34 Solve the following recurrence. 1, 1 1 1 , 2Solution: By iteration: 2 1 2 1 … 1 1 1 1 2 1 1 1 6 2 ѲNote: We can directly use the master theorem for this problem.Problem-Problem-35 Consider the following program: Fib[n] if (n==0) then return 0 else if (n==1) then return 1 else return Fib[n-1]+Fib[n-2]Solution: The recurrence relation for running time of this program is 1 2 .program for only reduced by 1 and 2, so the depth of the recurrence tree is . TheNotice has two recurrence calls indicating a binary tree. Each step recursively calls thenumber of leaves at depth is 2 since this is a full binary tree, and each leaf takes at least 1 computation for the constant factor. So, the running time is clearly exponential in . 89 Analysis of Algorithms | CareerMonk Publications ™
25. 25. ©www.CareerMonk.comData Structures and Algorithms Made EasyProblem-Problem-36 Running time of following program? function(n) { for( i = 1 ; i ≤ n ; i + + ) for( j = 1 ; j ≤ n ; j+ = i ) print( “*” ) ; }Solution:Solution: Consider the comments in below function: function (n) { //this loop executes n times for( i = 1 ; i ≤ n ; i + + ) //this loop executes j times with j increase by the rate of i for( j = 1 ; j ≤ n ; j+ = i ) print( “*” ) ; } √24].Its running time is since the inner loop is same as that ofProblem-37 What is the complexity of ∑Problem- ?Solution: Using the logarithmic property that,Solution: . We can see that thisproblem is equivalent to 1 2 1 2 … !This shows that that the time complexity .Problem-Problem-38 What is the running time of the following recursive function (specified as a function of the input value )? First write the recurrence formula and then find its complexity. function(int n) { if (n ≤ 1) 90 Analysis of Algorithms | CareerMonk Publications ™
26. 26. ©www.CareerMonk.comData Structures and Algorithms Made Easy return ; int i; for (i=1 ; i ≤ 3; i++ ) f( ); }Solution: Consider the comments in below function: olution: function (int n) { //constant time if (n ≤ 1) return ; int i; //this loop executes with recursive loop of value for (i=1 ; i ≤ 3; i++ ) f( ); }We can assume that for asymptotical analysis k k for every integer 1. The 3 1 . .recurrence for this code isUsing master theorem, we getProblem-Problem-39 What is the running time of the following recursive function (specified as a function of the input value ? First write a recurrence formula, and show its solution using induction. function(int n) { if (n ≤ 1) return; for (i=1 ; i ≤ 3 ; i++ ) function (n − 1). }Solution:Solution: Consider the comments in below function: function (int n) { //constant time if (n ≤ 1) 91 Analysis of Algorithms | CareerMonk Publications ™
27. 27. ©www.CareerMonk.comData Structures and Algorithms Made Easy return; //this loop executes 3 times with recursive call of n-1 value for (i=1 ; i ≤ 3 ; i++ ) function (n − 1). }The requires constant time (call it 1 ). With the , we neglect theloop overhead and only count the three times that the function is called recursively. Thisimplies a time complexity recurrence , 1; 3 1 , 1.Now we use repeated substitution to guess at the solution when we substitute k times: 3 1Using the master theorem, we get 3 .Problem-Problem-40 Write a recursion formula for the running time of the function f, whose code is below. What is the running time of , as a function of ? function (int n) { if (n ≤ 1) return; int i = 1 ; for(i = 1; i < n; i + +) print(“*”); function ( 0.8n ) ; }Solution:Solution: Consider the comments in below function:function (int n){ //constant time if (n ≤ 1) return; //constant time int i = 1 ; // this loop executes times with constant time loop 92 Analysis of Algorithms | CareerMonk Publications ™
28. 28. ©www.CareerMonk.comData Structures and Algorithms Made Easy for(i = 1; i < n; i + +) print(“*”); //recursive call with 0.8n function ( 0.8n ) ;} .8 4The recurrence for this piece of code is 5 4 5Applying master theorem, we get .Problem-Problem-41 Find the complexity of the following recurrence. 2 √ 2Solution: The given recurrence is not in the master theorem form. So we try to convert thismaster theorem format. For that let use assume thatIf we apply logarithm on both side, we get, 2Now, the given function becomes, 2 2 √2 2 2 .To make it simple we assume 2 2 2Applying the master theorem would resultIf we substitute back, .Problem-Problem-42 Find the complexity of the following recurrence. √ 1Solution: We apply the same logic as that of Problem-41 and we get 1 2 93 Analysis of Algorithms | CareerMonk Publications ™
29. 29. ©www.CareerMonk.comData Structures and Algorithms Made EasyApplying the master theorem would result .Substituting , gives .Problem-Problem-43 Find the complexity of the following recurrence. 2 √ 1Solution: Applying the logic of Problem-41, gives 2 1 2Using the master theorem results .Substituting gives .Problem-Problem-44 Find the complexity of the below function. int Function (int n) { if (n <= 2) return 1; else return (Function (floor(sqrt(n))) + 1); }Solution: Consider the comments in below function:int Function (int n){ //constant time if (n <= 2) return 1; // executes √ + 1 times else return (Function (floor(sqrt(n))) + 1);} √ 1. And,this is same as that of Problem-42.For the above function, the recurrence function can be given as: 94 Analysis of Algorithms | CareerMonk Publications ™
30. 30. ©www.CareerMonk.comData Structures and Algorithms Made EasyProblem-Problem-45 Analyze the running time of the following recursive procedure as a function of n. You may assume that each assignment or division takes unit time. void function(int n) { if ( n < 2 ) return; else counter = 0; for i = 1 to 8 do function ( ); for I =1 to do counter = counter + 1; }Solution: Consider the comments in below function and let us refer to the running time offunction (n) as T(n). void function(int n) { //constant time if ( n < 2 ) return; else counter = 0; // this loop executes 8 times with n value half in every call for i = 1 to 8 do function ( ); // this loop executes times with constant time loop for I =1 to do counter = counter + 1; } can be defined as follows: 1 2, 8 1 . 2Using the master theorem gives, log Ѳ log n . 95 Analysis of Algorithms | CareerMonk Publications ™
31. 31. ©www.CareerMonk.comData Structures and Algorithms Made EasyProblem-Problem-46 Find the complexity of the below function. temp = 1 repeat for i = 1 to n n = ; temp = temp + 1; until n ≤ 1Solution: Consider the comments in below function: //const time temp = 1 repeat // this loops executes n times for i = 1 to n //recursive call with value temp = temp + 1; n = ; until n ≤ 1The recurrence for this function isUsing master theorem, we get, .Problem-Problem-47 Running time of following program? function(int n) { for( i = 1 ; i ≤ n ; i + + ) for( j = 1 ; j ≤ n ; j * = 2 ) print( “*” ) ; }Solution: Consider the comments in below function:function(int n){ // this loops executes n times for( i = 1 ; i ≤ n ; i + + ) // this loops executes logn times from our logarithms //guideline 96 Analysis of Algorithms | CareerMonk Publications ™
32. 32. ©www.CareerMonk.comData Structures and Algorithms Made Easy for( j = 1 ; j ≤ n ; j * = 2 ) print( “*” ) ;}Complexity of above program is : .Problem-Problem-48 Running time of following program? function(int n) { for( i = 1 ; i ≤ n/3 ; i + + ) for( j = 1 ; j ≤ n ; j += 4 ) print( “*” ) ; }Solution: Consider the comments in below function:function(int n){ // this loops executes n/3 times for( i = 1 ; i ≤ n/3 ; i + + ) // this loops executes n/4 times for( j = 1 ; j ≤ n ; j += 4) print( “*” ) ;}The time complexity of this program is : .Problem-Problem-49 Find the complexity of the below function. void function(int n) { if(n ≤ 1) return; if (n > 1) { print ("*"); function( ); function( ); } } 97 Analysis of Algorithms | CareerMonk Publications ™
33. 33. ©www.CareerMonk.comData Structures and Algorithms Made EasySolution: Consider the comments in below function:void function(int n){ //constant time if(n ≤ 1) return; if (n > 1) { //constant time print ("*"); //recursion with n/2 value function( n/2 ); //recursion with n/2 value function( n/2 ); }}The recurrence for this function is: 2 1Using master theorem, we get . , ,Note:Note: With these examples, I hope you got an understanding of analysis of algorithms. In this , , 2 etc… so that it will be clear when we refer them in remaining chapters.chapter, try to understand when do we get different complexities like: 98 Analysis of Algorithms | CareerMonk Publications ™