![]() |
VOOZH | about |
What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
Types of Recursions:
Recursion are mainly of two types depending on whether a function calls itself from within itself or more than one function call one another mutually. The first one is called direct recursion and another one is called indirect recursion. Thus, the two types of recursion are:
1. Direct Recursion: These can be further categorized into four types:
3 2 1
Let's understand the example by tracing tree of recursive function. That is how the calls are made and how the outputs are produced.
Time Complexity For Tail Recursion : O(n)
Space Complexity For Tail Recursion : O(n)
Note: Time & Space Complexity is given for this specific example. It may vary for another example.
Let's now converting Tail Recursion into Loop and compare each other in terms of Time & Space Complexity and decide which is more efficient.
3 2 1
Time Complexity: O(n)
Space Complexity: O(1)
Note: Time & Space Complexity is given for this specific example. It may vary for another example.
So it was seen that in case of loop the Space Complexity is O(1) so it was better to write code in loop instead of tail recursion in terms of Space Complexity which is more efficient than tail recursion.
Why space complexity is less in case of loop ?
Before explaining this I am assuming that you are familiar with the knowledge that's how the data stored in main memory during execution of a program. In brief,when the program executes,the main memory divided into three parts. One part for code section, the second one is heap memory and another one is stack memory. Remember that the program can directly access only the stack memory, it can't directly access the heap memory so we need the help of pointer to access the heap memory.
Let's now understand why space complexity is less in case of loop ?
In case of loop when function "(void fun(int y))" executes there only one activation record created in stack memory(activation record created for only 'y' variable) so it takes only 'one' unit of memory inside stack so it's space complexity is O(1) but in case of recursive function every time it calls itself for each call a separate activation record created in stack.So if there's 'n' no of call then it takes 'n' unit of memory inside stack so it's space complexity is O(n).
1 2 3
Let's understand the example by tracing tree of recursive function. That is how the calls are made and how the outputs are produced.
Time Complexity For Head Recursion: O(n)
Space Complexity For Head Recursion: O(n)
Note: Time & Space Complexity is given for this specific example. It may vary for another example.
Note: Head recursion can't easily convert into loop as Tail Recursion but it can. Let's convert the above code into the loop.
1 2 3
fun(n)
{
// some code
if(n>0)
{
fun(n-1); // Calling itself only once
}
// some code
}
Program for tree recursion
3 2 1 1 2 1 1
Let's understand the example by tracing tree of recursive function. That is how the calls are made and how the outputs are produced.
Time Complexity For Tree Recursion: O(2^n)
Space Complexity For Tree Recursion: O(n)
Note: Time & Space Complexity is given for this specific example. It may vary for another example.
91
Let's understand the example by tracing tree of recursive function. That is how the calls are made and how the outputs are produced.
2. Indirect Recursion: In this recursion, there may be more than one functions and they are calling one another in a circular manner.
From the above diagram fun(A) is calling for fun(B), fun(B) is calling for fun(C) and fun(C) is calling for fun(A) and thus it makes a cycle.
Example:
20 19 9 8 4 3 1
Let's understand the example by tracing tree of recursive function. That is how the calls are made and how the outputs are produced.