Recursive Algorithm
Like Recurrence Relations, recursive algorithms, are algorithms that have a cycle in function calls
1 2 3 4 5 6 |
|
A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two properties that a recursive function must have:
- Base criteria: Or Initial Condition, when this condition is met the function stops calling itself recursively.
- Progressive approach: The recursive calls should progress in such a way that each time a recursive call is made it comes closer to the base criteria.
Concepts
Call Stack
Many programming languages implement recursion by means of stacks, The caller calls the callee in form of transfering execution control to the callee with passing Input Data, these datas will save in a new Stack Frame (Activation Record) then the callee will access them
- Activation record of each call contains:
- Input params
- Output space
- Return address
- etc
Call Tree
Recursion functions will call each other, we can draw a tree of function calls, by traversing that tree we can find the order of function calls
- Example: Write the correct sequence of numbers which will be printed by calling fn(4):
1 2 3 4 5 6 7 8 |
|
Analysis
In most cases the Space Complexity and Time Complexity of an Iterative Algorithm is better than it's Recursive Version
For analysing recursive algorithms, we must convert them into Iterative Algorithm version, then analyse it and find the Time Complexity and Space Complexity of them
There are two main types of analysis for Recursive Algorithms:
- Call Analysis: Finding recurrence relation for number of recursion calls
- Value Analysis: Finding recurrence relation for value of recursion
- Time Analysis: Finding recurrence relation for number of operations to run
- The Time required by Recursive Algorithm is more bigger than Iterative Algorithm because of changing Branch in each call
- The Space required by Recursive Algorithm is more bigger than Iterative Algorithm because of saving Activation Record in each call
Call Analysis
Finding a Recurrence Relation for number of recursion calls in recursive function
- For finding the call recurrence relation we will sum:
- Recursive Calls
- One(Self callee called by caller)
- Initial Conditions are One, because they only will called by caller
1 2 3 4 5 6 7 |
|
Now we can solve this Recurrence Relation to find the Number of Recursion Calls of fn(n)
Value Analysis
Finding a Recurrence Relation for value of recursive function
- For finding the value recurrence relation we will write the direct function in recurrence format
- Initial Conditions have the exact value in recursive function
1 2 3 4 5 6 7 |
|
Now we can solve this Recurrence Relation to find the Direct Relation of Value of fn(n)
- Tip: Using Bottom to Top Method is better for finding the exact value of function per an input:
1 2 3 4 5 6 7 |
|
Time Analysis
Finding a Recurrence Relation for number of operations to run of recursive function
- For finding the time recurrence relation we will sum:
- Recursive Calls
- Number of Operations(if, loop, add, mul, etc)
- Initial Conditions are Number of Operations with their input
1 2 3 4 5 6 7 |
|
Now we can solve this Recurrence Relation to find the Time Complexity of fn(n)
- Tip: We must focus on exact number of function calls:
- f has One recursive function call
- g has Two recursive function call
1 2 3 4 5 6 7 |
|
Examples
- Example 1: Find the Number of calls, Value, Number of multiplies, Time needed for function below per fn(4):
1 2 3 4 5 6 7 |
|
Now we can find the Time Complexity of this function using Time Recurrence Relation:
- Example 2: Find the Printed Stars for fn(4):
1 2 3 4 5 6 7 8 9 10 11 12 |
|