# Recursion Algorithm – Introduction to Algorithm – Analysis of Algorithm

Hello friends today we will study something which is very interesting known as Recursion what do you mean by the term Recursion how and why do we implement Recursion in any algorithm or any programming technique and what is the output of it what are the advantages of it so to start with what is a Recursion Recursion is more than just a programming technique that you will observe as will proceed further it is a way of describing defining and specifying things now here the things are nothing but our functions are modules or our various programming terminologies and techniques as a way of designing solutions to problem this is also one of a very simplest definition of recursion that it divides and conquered a problem that is any problem which is given to you whether from the mathematical world or from the real world I can divide or break down that problem and then find a solution then it is satisfying the need of recursion recursion is also defined as a function calling itself that is when a function calls itself again and again it is known as recursion how recursion works that is one main and one big thing that we need to study to truly understand how recursion works we first need to explore how a function call works now to see how a function call works we have to first see that when a program calls a subroutine or a function the current function must suspend its obsessing now what do we mean by this step that is if I am at a particular location or if I am on a particular call let’s take a very simple example if a person is talking on the phone and he gets another call then we suspend the present call for some time or put it on hold and take another call we go to that we speak to that particular person and then we come back to our original call but what happens in this process we don’t lose the previous phone we have a link which is provided so that is what is a main terminology or main process that our recursion follows the called function then takes over the control of the program that is what we just explained that the current call which comes into picture takes charge of your program now let’s take a very simple example to add the two numbers so what we do first we write a simple because it is a C programming sample we write the head of five simple main function we declare the variables integer n 1 n 2 and some end of the two numbers using the printf statement we say scanf end of the values that is ampersand n1 and ampersand n2 let’s see that we entered 10 and 20 so 10 or 20 are the two values that we have inputted the third step or the next step that we write we say sum is equal to n1 plus n2 we are adding the two values and the sum is we printed out to be 30 so this is a small program without using functions now what happens when this program is converted using a function so what do we do same thing the header file but here is a change what we do is we have declared a function which is known as a function prototype now any function that I need to call I have to give it a name here I have given it a name add num always make sure the names that you give to a function have some significance related to your program I can give it any name but just to make it significant and to use it by the programmer or any other person it should have a meaning related to it so we’ll give a function prototype here we are adding two numbers so we’ve given it as int add nom and the two variables are int a and int B the next step is void main now I start it’s a normal declaration integer N 1 n 2 and some I enter the two values that is n1 gets a value of 10 and n2 gets a value of 20 now this is a regular program now comes the next step here I call my function if you see we have written sum is equal to add num n1 n2 this particular statement signifies a function call what happens when the statement is written you will see that it was to a location where that particular function definition is there so int add Nam int a int B now here whatever variable I declare inside the brackets have to be same whatever I have declared in the function prototype I start with this program or I start with this function code I declare another variable known as answer I say answer is equal to a plus B we see that n1 value will be transferred to a + 2 value will be transferred to B and now when I say answer is equal to a plus B answer is equal to 10 plus 20 is equal to 30 so answer gets a value of 30 I say returned and some now what does this return answer specifies return also means whatever I have calculated in this function from this function it will be returned to the calling point from where this particular function was called that is at a location where my function call was written that statement was sum is equal to ad Nam n 1 comma n 2 so this answer will go and get reflected in the variable known as sum so when I print printf sum is equal to percentage these some my output will be 30 when I print the output will be 30 so this is how I have broken down my simple add two numbers program using functions now we will see how recursion works now to see how recursion works when a function is called the very important feature to remember is when a function is called the information at that point is need to be saved in order to return the calling module back to its original state that is from the state the function call was given so we need to store that particular address location of the statement and we need to save the information such as the local variables and the spot of code to return when we have finished with the function execution so this complete scenario explains how my function works how my function call works and how my recursion will save the calling point and the return point value now when we are implementing recursion the few data structures which are used now what are data structures these are programming strategies which are defined to design and analyze your algorithms now recursion works the best when the algorithms and data structures support recursion there are two major data structures which support recursion those are stacks and trees and one such algorithm which is used and implemented using recursion is a binary social world now this minor ësocial gore-ism will be implementing in the next few sessions when we study the depth algorithms and implementation of binary search algorithm now what happens here is will be seeing simple examples of how recursion is better then the iterative methods now what is an iterative definition this is an iterative definition for a simple example of a factorial of a number now what happens is in a nitration the definition and the algorithm only contains parameters and it has only flow there is no implementation strategy which goes into picture and it is very easier to implement then using recursion but recursion becomes important when I have larger problems and I can divide and implement and make it simpler now in a night rative algorithm for a factorial of a number let us consider your evidence one function to find a factorial of a number it has given a name factorial n I is equal to 1 factors equal to 1 while I is less than n now n as any number whose factorial I need to find let’s take an example if I take the value of n as 5 is 1 and I is less than equal to 5 fact is equal to factor into I now the value of fact we have initialized to 1 you will observe over here that I cannot initialize the value of fact to 0 because if I initialize the value of fact to 0 then anything multiplied by 0 will result me in a 0 answer so you remember that in the factorial program the value of fact has to be initialized to 1 i say fact is equal to factor into I the value of factors 1 the value of I is also 1 so 1 into 1 resultant is again the result is again 1 which is stored back in the variable factor the next statement I say I is equal to I plus 1 the value of I is now incremented to to the while loop gets closed it goes back again and checks the value of I is now 2 but 2 is still less than equal to 5 so it goes back into the loop and fact is equal to what value of fact is 1 value is to so 1 into 2 kills me too that was told back into factorial this loop executes till my eye becomes equal to 5 in the next step when I becomes greater than 5 it terminates the loop and returns the value of fact to a place where the function factorial was odd now the iterative solution is simple while loop executes through all the integers between 1 to n and multiply them together now what happens here is the value of n we have 5 so this loop will execute till my value of I becomes greater than 5 and it will give me the value of 5 factorial which is 120 now what is a feature break down in this this solution is known or is called as the base case every recursion algorithm must have a base case to simplify to that is I can break it down to a simpler solution which will be the minimum and the best solution otherwise the algorithm will run for ever that is it will never come out of the loop and it will become an infinite loop so there has to be a base case at which my algorithm will stop and terminate now what is a basic recursion now base cases always have at least one case that is solved without recursion that is when I reach to that stage my program should come out at a later stage when I reduce it make process state any recursive call must make process towards the base case that is the first instance is the checking condition and later on when I divide and conquer a particular program then it should always approach to my base case now we will see that when we have a base case and a making progress case how do we implement a factorial of a number using it what happens in a factorial of a number the logic behind the factorial is n factorial is equal to n into n minus 1 factorial what happens in this case is if n is 5 so 5 factorial is equal to 5 into 5 minus 1 factorial which will be 5 into 4 into 3 into 2 into 1 which will be equivalent to 20 so that is a simple example of a factorial of a number now when you have the simple program return in a recursive manner now what happens to it I have a function named factorial I have a variable int n which gets the value from the function call as this is my function definition now the first base case in this example is if my n is less than or equal to 1 I return the value 1 else I go to another part that is if my value of n is negative there will not be any factorial but if my value is 0 or 1 my factorial is 1 so if it is more than that then I come to the else part of it and my else part will return and into factorial n minus 1 what happens over here is my value of n is 5 so the first time it takes the value 5 it says toriel n -1 here is a function calling itself so function factorial calls itself so first instance of 5 factorial n minus 1 so factorial 5 minus 1 is 4 it goes back to the calling function and comes downs and checks if 5 is less than equal to 1 no it is not comes down to the else part and this recursive factor continues till the value of n becomes 1 the factorial of a number is given by factorial n in the bracket it can be defined as 1 if n is equal to 0 and n into factorial n minus 1 if n is greater than 0 so this is how my I creative process of recursion will continue now n factorial end into fact n minus 1 example 5 into 5 minus 1 factorial for factorial as 5 minus 1 is 4 the function calls itself so 4 factorial is equal to 5 into 4 into 4 minus 1 which is 5 into 4 into 3 factorial as function calls itself so 3 factorial is equal to 5 into 4 into 3 into 3 minus 1 factorial which is 2 factorial so 2 factorial is equal to 5 into 4 into 3 into 2 into 2 minus 1 is 1 factorial so 5 into 4 into 3 into 2 into 1 1 minus 1 which is 0 factorial so when it becomes less than that it comes out of the loop now the recursive algorithm for factorial n if n is equal to 0 it returns 1 else return n into factorial n minus 1 and it spins so recursion eliminates the loop this is what we’ve seen and rewritten implemented the algorithm using a simple if loop so here we observe that when a simple factorial program is written using recursion then my while loop is eliminated the time taken for me to execute a while loop is totally removed that program is converted into simple two line if-else loop structure and it returns a correct measure so recursion always provides a correct measure if we are able to break that particular program into base and of make progress step recursion is always used if we do a divide and conquer technique so that my large programs can be broken down into small small steps and they reduce from a larger bit or a larger problem and shrink towards the base case when I get a finite solution and when I reach that base rate base case when I reach that base case then we terminate the recursive function now we will see how recursion and hydration are differentiated or compared we see that recursion eliminates the looping atmosphere of hydration in hydration we have different looping structures which come into picture and recursion uses a simple in structure and eliminates all the loops so how recursion a nitration can be compared recursion is based upon calling the same function again again and again that is a function calling itself that is Nona and simple definition recursion hydration is a looping structure where the execution jumps back to the beginning of the loop and there are a set of statements that get executed in between this looping jump structure where it goes from an initial condition to its final condition a function call is often more expensive than a jump so we find out over here that there are fewer things which are good and recursion and there are few things which are disadvantages or give us a little backstage to use a recursion as a looping structure always has a less execution time but the complexity is more and function call is often more expensive in terms of space as I have to always keep storing the location from where it will be moved or from where the function call is given so what happens we have to now see what are the benefits of using or a question invariably recursive functions are more clearer simpler shorter and are easier to understand then then non recursive counterparts now when I say a non recursive counterparts that is the looping strategy or the iterative strategy the program directly reflects the algorithm which is termed as an abstract solution strategy that is algorithm is clearly defined when we are using a recursion the only thing that we do is in nitration we write a complete code in recursion using functions we divide the same function we divide the same program into functions which give us more efficient outputs and when a function calls by itself that is I break my program into more smaller bits then it becomes more easy and simplified and gives me a perfect output but with this advantages or benefits recursion also has few limitations what are these limitations recursion is powerful problem-solving technique that produces a clean solution to the most complex problem but it also describes an iterative solution the only limitation it has is sometimes it becomes difficult for the programmer or a developer to understand or how to break a particular problem into smaller bits when you cannot decide to break a problem into a smaller bits then it is always advisable not to use recursion so we can use our simple iterative methods recursive algorithm usually runs slower than their hydrated counterparts and every time we make of call to a function we must use some memory resources to make the use of the stack frame now what do we need a stack frame over here we need a stack frame because we have to store the address location and the calling point of that particular function if we do not store the calling point of the function then we lose the track of where we to return after we have jumped to that particular location now what is the purpose of recursion as we’ve seen the need the advantages the disadvantages of recursion recursion is truly valuable when a problem has no simple iterative solution or the problem becomes too large with the nitration x’ then what we do is we define a base class for the function we define a base class for the recursion and then we try to find a solution that a recursion can be more advantageous to use than our iteration as we reduce the number of loops and recursion and implemented with a simple if structure there is no reason to use recursion when it used does not gain anything now gain in terms of space or time if it takes more time to solve a simple problem using recursion then it is always advisable to use iterations rather than recursion but if I tration becomes more complicated than it is advisable in the case to solve using recursion we have seen the example of factorial it can be solved using iteration but it became more simpler when we solved it using recursion so recursion while implementing has to be chosen very carefully so that it does not make a problem statement difficult but it should be used to simplify the programming structure now what is the importance of recursion a recursive solution solves a problem by solving a smaller instance at same time that is I have smaller bits which needs to be solved it solves this new problem by solving even the smaller instance of the same problem that is when I divide this bit as we have seen the five factorial problem as I was reducing as we were reducing the bits that is from 5 factorial 4 factorial 3 factorial 2 factorial 1 factorial at every smaller instance the recursion was working perfectly and it was reducing and every instance was getting smaller eventually the new problem that was small and solution was becoming very obvious as were we were reducing itself towards the base case so recursion becomes very important factor when we are very clear about the base case this solution will lead to a solution of original problem that is when I started with 5 factorial and reduced down to 1 factorial by that time we reached 1 minus 1 function calling itself we had already approached to the problem solutions so recursion was easy to implement for the factorial then the iteration thank you

Good Class

Hello Friends,

Watch Complete Video Series of Subject Analysis of Algorithm only on Ekeeda Application.

Use Coupon Code "NEWUSER" and access any one Subject free for 3 days!

Download Ekeeda Application and take advantage of this offer.

Android:- https://play.google.com/store/apps/details?id=student.ekeeda.com.ekeeda_student&hl=en

iOS:- https://itunes.apple.com/tt/app/ekeeda/id1442131224