0% found this document useful (0 votes)
12 views

Recursion

DSA Notes

Uploaded by

headofthetable06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Recursion

DSA Notes

Uploaded by

headofthetable06
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 6 : Recursion

1
Introduction

 Recursion is a method to define something in terms of itself.

 A function that calls itself is a recursive function.

 Examples : Factorial, Fibonacci series, Tower of Hanoi etc.

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.

Recursion involves two key components:

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.

2. Recursive Step: It defines how the problem is divided into smaller


instances and how the recursive function is called to solve them. The
recursive step typically involves calling the same function with a smaller
input or a modified version of the original problem.

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

Here's the Fibonacci series up to the 10th term:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Each number in the series (starting from the third term) is


obtained by adding the previous two numbers.

4
Tower of Hanoi

The Tower of Hanoi is a classic mathematical puzzle or problem


that involves three pegs and a set of disks of different sizes.

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.

The rules for the Tower of Hanoi problem are as follows:

• Only one disk can be moved at a time.


• Each move consists of taking the top disk from one of the pegs and
placing it on top of another peg.
• A larger disk cannot be placed on top of a smaller disk.

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:

int fact(int n) { int fact(int n)


int i, result=1; {
if(n==0) if(n==0)
return 1; return 1;
else else
for(i=1;i<=n;i++) return n*fact(n-1);
result=result*i; }
return result;
}

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

You might also like