AfterAcademy Tech
•
12 Dec 2019

What is Recursion??
Recursion is a way of solving problems via the smaller versions of the same problem. We solve the problem via the smaller sub-problems till we reach the trivial version of the problem i.e. base case.
“In order to understand recursion, one must first understand recursion.”
In other words, a recursive function is a function that calls itself until a “base condition” is true, and execution stops. The recursive function has two parts:
Any recursive function will look like
function(arg1,arg2,....)
{
if( Base Case Condition )
{
// Base Case
}
// Recursive Structure
}
Base Case: The smallest version of the problem for which we already know the solution or a terminating condition where a function can return the result immediately.
Recursive Structure: Finding the solution to a problem via the solution of its smaller sub-problems. Here function must call itself to break the current problem down to a simpler level.
Let us take an example of Countdown Problem to understand the base case and recursive structure.
You need to print numbers from N to 1 in decreasing order.
Recursive Structure
In a recursive structure, we break the solution of the problem in terms of the solution of smaller versions of the same problem. So, here we can print N at the first step and call the same function to print the remaining N -1 numbers.
Printing numbers from N to 1 = print (N) + Printing numbers from N-1 to 1
Original Problem: Printing numbers from N to 1
Smaller subproblem: Printing numbers from N-1 to 1
=> countdown(N) = print(N) + countdown(N-1)
Now, The chain of the function call needs to stop somewhere. Just Think, what can be the base case for the above solution?
Base Case
The countdown needs to stop after printing 1. So we need to write a base condition to stop the program execution.
void countdown(int n)
{
if( n < 1 ) // Base Case
{
return // stops the execution with return statement.
}
}
Now, we just need to combine the recursive structure and base case to write the full recursive implementation of the above problem
Pseudo Code
void countdown(int n)
{
if(n < 1 ) // Base Case
{
return
}
print(n)
countdown(n-1) // Recursive
}
Flow Chart of Program

Critical Ideas to Think!
We need to find the nth factorial. The factorial of a number is the product of numbers from 1 to n (Inclusive). For Example, Factorial of 4 is 1*2*3*4 = 24
Recursive Structure
According to the definition of the factorial, we can describe a solution to a problem via the solution of its smaller sub-problem.
Finding nth factorial = n * finding (n-1)th factorial
Original Problem: finding nth factorial
Smaller sub-problem: finding (n-1)th factorial
fact(n) = n * fact(n-1)
Base Case
Now think about the base case, where the function will directly give you results without breaking it again into sub-problem.
fact(5)
= 5 * fact(4)
= 5 * 4 * fact(3)
= 5 * 4 * 3 * fact(2)
= 5 * 4 * 3 * 2 * fact(1)
= 5 * 4 * 3 * 2 * 1 * fact(0)
The factorial of a negative number is not defined, so fact(0) is the smallest version of the factorial problem where our solution will terminate.
Here n = 0 is the base case which will return 1. Since 0! = 1
Flow of Program

Pseudo Code
int fact(int n)
{
if(n == 0) // Base Case
{
return 1
}
return n*fact(n-1) // Recursive Structure
}
Critical Ideas to Think
Take a problem “Reverse the String” and think about its recursive solution with the help of the above thought process.
Finding nth fibonacci
Recursive Structure
fibonacci(n)= fibonacci(n-1) + fibonacii(n-2)
Base Case
if (n <= 1) return n.
Here we have 2 base cases: fib(0) = 0 and fib(1) = 1
Finding GCD of two numbers
Recursive Structure
gcd(a, b) = gcd(b, a mod b)
Here we are assuming that a > b
Base Case
gcd(a, 0) = a
Reverse an array
Recursive Structure
reverseArray(A, l, r)
- swap(A[l], A[r])
- reverseArray(A, l+1, r-1)
Base Case
If (l >= r) then return (Think!)
Binary Search
Recursive Structure
BinarySearch(A[], l, r, k)
- mid = l + (r-l)/2
- if (A[mid] > k), then call BinarySearch(A[], l, mid-1, k)
- if (A[mid] < k), then call BinarySearch(A[], mid+1, r, k)
Base Case
If (l > r) then return -1. This is the case of unsuccessful search (Think!)
Merge Sort
Recursive Structure
mergeSort(A[], l, r)
- mid = l+(r-l)/2
- Recursively sort first half : mergeSort(A, l, mid)
- Recursively sort second half: mergeSort(A, mid+1, r)
- Merge the two sorted halves: merge(A, l, mid, r)
Base Case
If (l == r) then return. This is the case of single element (Think!)
Quick Sort
Recursive Structure
quickSort(A[], l, r)
- pivot = partition(A, l, r)
- quickSort(A, l, pivot - 1)
- quickSort(A, pivot + 1, r)
Base Case
if (l >= r) then return.
Here we have two base cases : (l > r) and (l == r)
In-Order Traversal of binary tree
Recursive Structure
Inorder(root)
- Traverse the left subtree: Inorder(root->left)
- Visit the root
- Traverse the right subtree: Inorder(root->right)
Base Case
if(root == NULL) then return
Print all permutation of a given string
Recursive Structure
permute(string A, l, r)
for(i = l to r)
- swap(A[l], A[i])
- permute(A, l+1, r)
- swap(A[l], A[i])
Base Case
if(l == r) then print(A)(Think!)
Longest Common Subsequence
Recursive Structure
lcs(X, Y, m, n)
- if(X[m-1] == Y[n-1]), return 1 + lcs(X, Y, m-1, n-1)
- else, return max (lcs(X, Y, m, n-1), lcs(X, Y, m-1, n))
Base Case
if (m == 0 || n == 0) then return 0
If we observe the above flow of execution of recursion, one pattern is visible: fact(0) get called last but returning the value first. Similarly, we are calling fact(n) first but returning the value last. (Think!)
Order of execution of function call
fact(n)-> fact(n-1)...-> fact(i)->...-> fact(1)-> fact(0)
Order in which functions are returning values
fact(0)-> fact(1)...-> fact(i)->...-> fact(n-1)-> fact(n)
The above idea looks similar to the Last In First Out(LIFO) order. Here are some key insights related to the execution of recursion:
If you are not familiar with stack data structure, assume a stack of books. In this, you can add one book at a time. Then when have to take one out of it, you can take only that is on the top of the stack.
Let's see the call stack for factorial example

To solve any problem recursively, just try to follow these thought process, it will make it easier to decide the recursive structure and base cases :
Here are some benefits of using recursion:
Happy coding! Enjoy Algorithms.
AfterAcademy Tech
In this blog, we will analyze the recursive algorithm using Recurrence Tree Method, Master theorem. We will also discuss the advantages and disadvantages of recursion.

AfterAcademy Tech
Write a program for the recursive implementation of Insertion Sort. Insertion Sort is used to sort a given array. This problem will sharpen your recursion skills.

AfterAcademy Tech
Iteration and Recursion are programming methodologies with similar objective and thus one might seem trivial compared to another, but that's not the case. We will be discussing the important differences between iteration and recursion and how both are useful.

AfterAcademy Tech
Given a stack of integers st, write a program to reverse the stack using recursion. This problem will clear the concept of recursion.
