Posts

Time Complexity of Recursive Algorithm

Image
In the previous post, we have seen the complexity analysis of iterative algorithms. Here, we will do the complexity analysis of recursive algorithms. Recursion is a process in which a function call itself directly or indirectly.   A(n)   {   If()   return (A(n/2)+A(n/2))   } The above function has two recursive call in return statement of the program and a one conditional check if statement. Hence, the recursive relation for this algorithm will be. T(n)=C+2T(n/2) Here, C represent the constant time taken to check the if condition, and 2T(n/2) is the two recursive calls. To solve the recursive relation of the given algorithm, we have three methods 1> Back Substitution 2> Tree Method 3> Masters Method. Back Substitution method Example: A() { if(n>1) return (A(n-1)) } Before writing the recursive relation for the following algorithm, we will see the stopping if condition of the algorithm. if (n...

Types of Algorithm and Its Complexity Analysis

Image
In this post, we will learn types of Algorithm and its time complexity analysis with examples  An algorithm is divided into two forms called iterative and recursive, as shown above. Firstly, we analyze the time complexity of the iterative algorithm and then recursive. Example 1:                           A()                           {                            int i;                            for(i=1 to n)   % loop run n times                         ...

Time Complexity Analysis

Image
In this post, we will do some excise to understand the actual significance of asymptotic notation. How, we will calculate the complexity and what will be the notations. The following series are important before we can begin to calculate the complexity. Linear series (arithmetic series) for n>=0   Quadratic series for n>=0 Cubic series for n>=0   Geometric series for x!=1,         For |x|  Linear Geometric series for n>=0, real c!=1 Harmonic series nth harmonic number The unrolling of the looping statement within the computer programs may resemble with the following series, and we can directly use it to compute the complexity of the scheme.  How to define notations  Normally, the time complexity with worst case analysis is important If the complexity of  Worst case (Big-O) == Best cast (Big-Omega), than we can go with Average case complexity i.e (Big-theta)  If the loops i...

Asymptotic Notations

Image
 In computer science, any problem can be solved using the programming language. And a particular problem can have multiple solutions in terms of Algorithm. To find out which one is the best algorithm for a problem, we analyse it in terms of time and space.  To define the time and space complexity of an algorithm, we use the Asymptotic notation. Mostly three types of Asymptotic notation is widely preferred to represent the complexity of the algorithm. Big-O mathematically represented as O The X-axis of the above graph showing the size of the input (n) and Y axis showing the increase in  time (t) . In the graph,   f(n) function showing the increase of time as input   size   n is increasing for a particular algorithm. Beside, the another function called cg(n) showing the worst case of f(n) . Hence, in the above graph cg(n)  is above the f(n). If we bound the time complexity or f(n) of a given algorithm in terms of input n with...