Recursion
Recursion
1
Introduction
Simple example :
int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
2
Principle of recursion
The principle of recursion is a fundamental concept in computer science and mathematics that involves
solving a problem by breaking it down into smaller, similar subproblems.
It is based on the idea of solving a problem by reducing it to one or more simpler instances of the same
problem.
1. Base Case: It is the simplest form or smallest input of the problem that
can be directly solved without further recursion. The base case acts as
the termination condition for the recursive function, preventing it from
infinitely calling itself.
3
Pseudocode
Fibonacci series
fib(n)
{
if (n==0)
The Fibonacci series is a sequence of numbers in which return 0;
each number is the sum of the two preceding numbers, else if(n==1)
starting from 0 and 1. In mathematical terms, it can be return 1;
defined recursively as: else
return (fib(n-1) + fib(n-2))
F(0) = 0
F(1) = 1
}
F(n) = F(n-1) + F(n-2) for n > 1
4
Tower of Hanoi
The objective is to move all the disks from one peg (known as the source)
to another peg (known as the destination), using a third peg
(known as the auxiliary) as temporary storage.
5
How to solve it ?
The problem can be solved using a recursive algorithm. Here is the recursive algorithm
to solve the Tower of Hanoi problem:
1. If the number of disks is 1, move it directly from the source peg to the destination
peg.
2. If the number of disks is greater than 1, recursively move n-1 disks from the source
peg to the auxiliary peg, using the destination peg as the temporary peg.
3. Move the largest disk from the source peg to the destination peg.
4. Recursively move the n-1 disks from the auxiliary peg to the destination peg, using
the source peg as the temporary peg.
6
How it works???
7
Pseudocode
toh(n,s,a,d)
{
if (n==1)
{
print: s->d
return;
}
else
{
toh(n-1,s,d,a)
print: s->d
toh(n-1,a,s,d)
return;
}
}
8
Recursion VS Iteration
Iteration Recursion
It is a process of executing a statement or It is a technique of defining anything in
a set of statements repeteadly, until terms of itself.
some specific condition is meet.
Any recursive problems can be solved Not all problems have recursive solutions.
iteratively.
It is more efficient in terms of memory It is less efficient in terms of memory
utilization and execution speed. utilization and execution speed.
Example: Example:
9
Applications of Recursion
1. Mathematical Problems: Recursion is frequently used to solve mathematical problems with recursive
structures. Examples: Factorial, Fibonacci, TOH etc.
2. Tree and Graph Traversals: Recursive algorithms are commonly employed to traverse and manipulate tree
and graph data structures. Examples : DFS, BFS etc.
3. Backtracking: Backtracking algorithms often involve recursion. Examples: N queen problems, Sudoku etc.
4. Divide and Conquer: Many divide and conquer algorithms use recursion to solve complex problems by
breaking them down into smaller subproblems. Examples : Merge Sort, Quick Sort etc
5. Parsing and Compiler Design: Recursive descent parsing is a technique used in compiler design to analyze
and process complex grammatical structures. Recursive functions are used to represent and parse the
different grammar rules recursively, enabling the recognition and interpretation of programming languages
and other formal languages.
10
Search Tree
Search tree is a tree-like data structure that allows efficient searching, insertion, deletion, and retrieval of
elements.
It is commonly used to organize and store data in a hierarchical manner, enabling fast access and
manipulation operations.
A search tree typically follows certain rules or properties to maintain its structure and facilitate efficient
searching. One common type of search tree is a binary search tree (BST). Figure is given below :
11