You are on page 1of 5

Recursion is a programming technique that divides a big computation problem

into smaller subproblems, and as explained in the video above, the same function
is called again and again till it reaches the terminating condition. The recursion
technique will be used frequently in the upcoming modules

Complexity Analysis of
Algorithm 1(Fibbonacci)
Explain, in your own words, how the equation T(n) = T(n - 1) + T(n - 2) + 1 is formed.

T(n): No. of additions to calculate F(n); when calculating F(n), the function recursively calls F(n
- 1) and F(n - 2), which will take T(n -1 ) and T(n - 2) respectivelyT(n - 1): No. of additions to
calculate F(n - 1)T(n - 2): No. of additions to calculate F(n - 2)Also, when calculating F(n), you
need to add the previous two numbers F(n - 1) and F(n - 2). The result is counted as one
more addition besides T(n - 1) and T(n - 2). So, you get the following equation:T(n) = T(n - 1)
+ T(n - 2) + 1

Assume that you are generating only three Fibonacci numbers using the function
fibonacci(3), and this function is called from the main function. When a method is
executed currently, it is saved in the memory so that all the information of the
variables and operations is also saved.
Right now, the main function is being executed, excluding the main function let us
see how fibonacci function is executed in the memory.
 
Information related to the Image below:
Circle indicates that the function is in the memory stack
Square indicates function completed its execution and popped out of the memory
stack.
Nothing indicates function did not start its execution.
Example
When the main function calls fibonacci(3), this function is executed in the
memory, and main() is paused.
Below are the steps which indicate the steps in the above image:
1. fibonacci(3) starts executing and calls fibonacci(2)

2. fibonacci(2) now calls for fibonacci(1)

3. In fibonacci(1) since function variable is 1, the base case of the fibonacci function is

executed, and it terminates the recursion.

4. The fibonacci(1) gets popped from the memory stack, then fibonacci(2) calls

fibonacci(0)

5. In fibonacci(0) since function variable is 0, the base case of the fibonacci function is

executed, and it terminates the recursion.

6. The fibonacci(0) gets popped from the memory stack

7. Since the fibonacci(2) completes its execution, it gets popped out of the memory

stack, then fibonacci(3) calls fibonacci(1)

8. In fibonacci(1) since function variable is 1, the base case of the fibonacci function is

executed, and it terminates the recursion.

9. The fibonacci(1) gets popped from the memory stack


10. Since the fibonacci(3) completes its execution it gets popped out of the memory stack.
Finally, the main function resumes with all the called functions popped out of the
memory.
The same process of memory allocation to generate three numbers can be extended
to n numbers of the Fibonacci series. In this recursion algorithm, the maximum
memory used is proportional to the number generated in the function:
F(0) = 0
F(1) = 1
F(n) = [F(n - 1) + F(n - 2)]%10, n > 1

Therefore, the space complexity is O(n)-linear function, with respect to the input
size.

Program to find the factorial of a number using recursion.


import java.util.Scanner;

public class Demo {


public static int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive no: ");
int n = sc.nextInt();
System.out.println("Factorial of " + n + " is : " + factorial(n));
}
}

Let us assume that total time taken by the above factorial function to calculate the
factorial ofn  is  T(n).
If we analyse the factorial function above:
In “if (n==0)”  comparison takes constant time ‘c’
In “return n∗ factorial(n−1);” multiplication takes constant execution time ‘ c’
and subtraction in (n−1) takes constant execution time ‘c'
Since the time taken by the above factorial function to calculate the factorial of n
is T(n), the time taken by the above factorial function to calculate the factorial
of n−1 is T(n−1).
 
When n=0 in the above factorial function, T(n)=c, because, only comparison is
done in “if (n==0)”. This implies T(0)=c.
 
So total time complexity of the above factorial function is
T(n)=c+c+c+T(n−1)
=3∗c+T(n−1)       ----------------- 1st step
Substitute n with n−1 in the above equation we get
T(n−1)=T(n−2)+3∗c
Now substitute this T(n−1) in the first equation, then we get
T(n)=6c+T(n−2)      ----------------- 2nd step
        
Similarly
T(n)=T(n−k)+3∗k∗c      ----------------- kth step
 
Let us assume the kth step as the final step.
The recursion will terminate only when n=0, so T(n−k) should be equal to T(0)
⟹n−k=0
⟹n=k
By substituting n=k in kth step
We get T(n)=T(0)+3∗n∗c
T(n)=c+3∗n∗c (since T(0)=c) 
Let that constant time c=1 then 
T(n)=1+3n
By excluding the constant terms we get the total time complexity as
O(n)
You can use recursion to search for a file in its directory.First, pass the file that you
are looking for and the file directory path (folder path) that you want to start the
search, to the recursive function.Then, follow these steps inside the recursive
function:1. List all the contents of the file directory passed to the function.2. Loop
through the content inside the file directory.a. If any part of the content is another
file directory(folder), recursively call the function and pass the file directory(folder)
and its path through.b. If the content is a file, check and see if it matches the file you
are searching for. If it matches, then return “file is found” with the file path.

So, when you search for a specific file in your file explorer, the content in the
folder (directory) being searched for is listed. In the listed content, if it is a file,
then look for the correct match with the specific file. If it is a directory, call the
function findFile() recursively again in order to look for a match for the files in that
folder. In this way, file searching recursively finds the correct match for the
specific file and displays the search results.

Algorithm 2
In the first algorithm, the function follows a recursive procedure to generate the Fibonacci
number, but it does not store any of the values. We have recalculated the same values at
different instances, which increases the number of operations while executing this algorithm.
However, in the second algorithm, declaring an extra array variable to store all the Fibonacci
numbers reduces the redundant operations while executing the algorithm.

Algorithm 2 uses an array variable f[], stores the calculated values for each
iteration of the for loop (i), and finally, prints the nth number of the function. The
time complexity of algorithm 2 is a linear function which is relatively more
efficient than the exponential function of algorithm 1. This is why algorithm 2 is
able to process the output for n = 100 in an instant.

You might also like