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

Adobe Scan 08 Nov 2023

The document provides a comprehensive overview of recursion, detailing its definition, structure, advantages, disadvantages, types, and applications. It explains the basic structure of recursive functions, compares recursion with iteration, and illustrates various types of recursion such as direct, indirect, tail, non-tail, and nested recursion. Additionally, it includes examples and applications of recursion, including the K Queens problem and the Towers of Hanoi.

Uploaded by

jenniekim11198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Adobe Scan 08 Nov 2023

The document provides a comprehensive overview of recursion, detailing its definition, structure, advantages, disadvantages, types, and applications. It explains the basic structure of recursive functions, compares recursion with iteration, and illustrates various types of recursion such as direct, indirect, tail, non-tail, and nested recursion. Additionally, it includes examples and applications of recursion, including the K Queens problem and the Towers of Hanoi.

Uploaded by

jenniekim11198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Contents Cover

• Recursion Function
• Basic Structure
• Recursion Vs Iteration
• Advantages
• Disadvantages
• Recursion Algorithm and Example
• Types of Recursion
• Applications of Recursion
RECURSIVE FUNCTIONS
• A recursive function is defined as a function that calls itself to solve
a smaller version of its task until a final call is made which does not
require a call to itself.
• Every recursive solution has two major cases which are given as
follows:
• Base case, in which the problem is simple enough to be solved
directly without making any further calls to the same function.
(USED TO STOP THE RECURSION)
• Recursive case, in which first the problem at hand is divided into
simpler sub parts.
• Second, the function calls itself but with sub parts of the problem
obtained in the first step.
• Third, the result is obtained by combining the solutions of simpler
sub-parts.
RECURSIVE FUNCTIONS- BASIC
STRUCTURE
In if block, we mention the
base case which is the
stopping point to stop the
recursive process.

In else block, we mention


the recursive procedure in
which the problem is
divided into smaller
problems.
RECURSIVE FUNCTIONS- BASIC
STRUCTURE
For example, in order to calculate factorial(4), the problem could be
solved by dividing it into smaller problems.
Here, we can start from the easiest one i.e. factorial(1)
1. Fact(1) = 1 Started from smaller problem
2. Fact(2) = 2 * 1 = 2 * Fact(1)
3. Fact(3) = 3 * 2 * 1 = 3 * Fact(2)
4. Fact(4) = 4 * 3 * 2 * 1 = 4 * Fact(3) Reached to solution

IDEA BEHIND RECURSION- To solve a problem, solve a sub-


problem that is a smaller instance of the same problem, and then use
the solution to that smaller instance to solve the original problem
RECURSIVE FUNCTIONS- BASIC
STRUCTURE
For example, in order to calculate factorial(4), the problem could be
solved by dividing it into smaller problems.
Here, we can start from the easiest one i.e. factorial(1)
1. Fact(1) = 1 Started from smaller problem
2. Fact(2) = 2 * 1 = 2 * Fact(1)
3. Fact(3) = 3 * 2 * 1 = 3 * Fact(2)
4. Fact(4) = 4 * 3 * 2 * 1 = 4 * Fact(3) Reached to solution

What will be the base condition


In this problem?
RECURSIVE FUNCTIONS- BASIC
STRUCTURE
For example, in order to calculate factorial(4), the problem could be
solved by dividing it into smaller problems.
Here, we can start from the easiest one i.e. factorial(1)
1. Fact(1) = 1 Started from smaller problem
2. Fact(2) = 2 * 1 = 2 * Fact(1)
3. Fact(3) = 3 * 2 * 1 = 3 * Fact(2)
4. Fact(4) = 4 * 3 * 2 * 1 = 4 * Fact(3) Reached to solution

What will be the base condition


In this problem?
Fact(1) as the base condition is the one which doesn’t require
the same function to be called again and helps to stop recursion
RECURSIVE FUNCTIONS- BASIC
STRUCTURE
For example, in order to calculate factorial(4), the problem could be
solved by dividing it into smaller problems.
Here, we can start from the easiest one i.e. factorial(1)
1. Fact(1) = 1 Started from smaller problem
2. Fact(2) = 2 * 1 = 2 * Fact(1)
3. Fact(3) = 3 * 2 * 1 = 3 * Fact(2)
4. Fact(4) = 4 * 3 * 2 * 1 = 4 * Fact(3) Reached to solution

Could you write the basic structure


for this problem?
RECURSIVE FUNCTIONS- BASIC
STRUCTURE
For example, in order to calculate
factorial(4), the problem could be
solved by dividing it into smaller
problems.
Here, we can start from the easiest
one i.e. factorial(1)
1. Fact(1) = 1
2. Fact(2) = 2 * 1 = 2 * Fact(1)
3. Fact(3) = 3 * 2 * 1 = 3 * Fact(2)
4. Fact(4) = 4 * 3 * 2 * 1 = 4 *
Fact(3)
Program to calculate factorial using recursion
ITERATION V/S RECURSION

A recursive implementation and an iterative implementation do


the same exact job, but the way they do the job is different.
Recursion involves a function that calls itself. Iteration uses a
counter to track through a structure or complete an operation n
times.
ADVANTAGES OF RECURSION

Which path should I follow?


Iteration or recursion? For this, let’s look at the advantages of
recursive function:
1. Every recursive program can be modelled into an iterative
program but recursive programs are more elegant and
required less lines of code.
Iterative function Recursive function
ADVANTAGES OF RECURSION

Which path should I follow?


Iteration or recursion? For this, let’s look at the advantages of
recursive function:
1. Every recursive program can be modelled into an iterative
program but recursive programs are more elegant and
required less lines of code.
2. Recursive solutions often tend to be shorter and simpler than
non-recursive ones.
3. Code is clearer and easier to use.
4. Recursion represents like the original formula to solve a
problem.
5. Follows a divide and conquer technique to solve problems.
6. In some (limited) instances, recursion may be more efficient.
DISADVANTAGES OF RECURSION
• Recursion is implemented using system stack. If the stack space
on the system is limited, recursion to a deeper level will be
difficult to implement.
• Recursion utilize more function calls.
• Using a recursive function takes more memory and time to
execute as compared to its non-recursive counter part.
• It is difficult to find bugs, particularly, when using global
variables
TYPES OF RECURSION
• Direct and Indirect Recursion
• Tail and Non-Tail Recursion
• Nested Recursion
TYPES OF RECURSION- DIRECT

DIRECT RECURSION
• A function is called direct recursive if it calls the same function
again.
• Structure of direct recursion
TYPES OF RECURSION- INDIRECT

INDIRECT RECURSION
• A function (let’s say func) is called indirect recursive if it calls
the another function (let’s say func2) and then that another
function (func2) calls the first function (func).
• Structure of indirect recursion
TYPES OF RECURSION- INDIRECT

INDIRECT RECURSION
• A function (let’s say func) is called indirect recursive if it calls
the another function (let’s say func2) and then that another
function (func2) calls the first function (func).
• Structure of indirect recursion
TYPES OF RECURSION- INDIRECT

INDIRECT RECURSION
• A function (let’s say func) is called indirect recursive if it calls
the another function (let’s say func2) and then that another
function (func2) calls the first function (func).
• Structure of indirect recursion
TYPES OF RECURSION- INDIRECT

WAP to print numbers from 1 to 10 such that when number is


odd: add 1, and when number is even: subtract 1.
Output: 2 1 4 3 6 5 8 7 10 9
TYPES OF RECURSION- INDIRECT

WAP to print numbers from 1 to 10 such that when number is


odd: add 1, and when number is even: subtract 1.
Output: 2 1 4 3 6 5 8 7 10 9
TYPES OF RECURSION- TAIL

TAIL RECURSION
• A function is said to be tail recursive if the recursive call is the
last thing done by the function. There is no need to keep record
of previous state.

Last thing done by the function is recursion


TYPES OF RECURSION- TAIL

TAIL RECURSION
• A function is said to be tail recursive if the recursive call is the
last thing done by the function. There is no need to keep record
of previous state.
TYPES OF RECURSION- TAIL

TAIL RECURSION
• A function is said to be tail recursive if the recursive call is the
last thing done by the function. There is no need to keep record
of previous state.
TYPES OF RECURSION- TAIL

TAIL RECURSION
• A function is said to be tail recursive if the recursive call is the
last thing done by the function. There is no need to keep record
of previous state.
TYPES OF RECURSION- TAIL

TAIL RECURSION
• A function is said to be tail recursive if the recursive call is the
last thing done by the function. There is no need to keep record
of previous state.
TYPES OF RECURSION- TAIL

TAIL RECURSION
• A function is said to be tail recursive if the recursive call is the
last thing done by the function. There is no need to keep record
of previous state.

Returning
Back to
previous
Function
and
nothing to
evaluate

F1 will get popped out of stack first, then f2, then f3, and then main
TYPES OF RECURSION- NON-TAIL

NON-TAIL RECURSION
• A function is said to be tail recursive if the recursive call is not
the last thing done by the function. There is need to keep record
of previous state.

Recursion is not the last thing done by the


function but it has something to evaluate
TYPES OF RECURSION- NON-TAIL

NON-TAIL RECURSION
• A function is said to be tail recursive if the recursive call is not
the last thing done by the function. There is need to keep record
of previous state.

ANOTHER EXAMPLE:

Recursion is not the last thing done by the


function but it has something to evaluate
TYPES OF RECURSION- NESTED
NESTED RECURSION
• In this recursion, a recursive function will pass the parameter as
a recursive call. That means “recursion inside recursion”
• Example: Ackermann Function
TYPES OF RECURSION- NESTED
m+1, if n=0
A(n, m)= A(n-1,1) if n>0, m = 0
A(n-1, A(n, m-1) if n, m>0

• Let’s say we have n=1, m=2, So


A(1,2) = A(0, A(1,1) )

A(0, A(1,0))

A(0,1)= 1+1=2 Now solving formula A(n.m) where


A(0,2) =3 n=0, value =m+1

A(0,3) = 3+ 1 = 4
Application 1-K Queens Problem
Given a KxK chessboard, find a way to place K queens on the board so that no queen
can attack another queen.
A queen can move an arbitrary number of squares vertically, horizontally or diagonally.

It's immediately clear that there must be


one queen in every row and one queen
in every column.
Here is one solution:

There are over 4 billion different ways


to
drop 8 queens onto an 8x8 board.

It's known that there are exactly


92 distinct solutions to the
problem.
4 Queens Problem
Let's consider a variant on a 4x4 board… how to start?
Let's flag squares that are under attack with Xs, since
we cannot put a queen there.
Let's process the board row by row, from the top down.
X X X
Let's start by putting a queen in the first square in row 1:
X X

X X

X X

Now for row 2… we have two choices, let's try the first one: X X X
X X X
Oops… now all the squares in row 3 are under attack, so this X X X X
cannot lead to a solution…
X X X
4 Queens Problem
What to try next?
X X X
Let's backtrack… take back the last move and try
a different one: X X X

X X X

X X X

OK, now we have possibilities… let's fill the free


square in row 3: X X X
X X X
Rats! Now there are no free squares left in row 4. X X X

X X X X
We can backtrack again, but that means we must now
remove the 2nd and 3rd queens, since we've already
tried all the possibilities for the 2nd one, and then we
must consider a different spot for the 1st one…
4 Queens Problem
And, that leaves just one place for a queen in row 3: X X X
X X X

X X X

X X X

And, that leaves just one place for a queen in row 4: X X X


X X X
X X X

X X X

And, we have a solution… now can we deduce an algorithm?


2-Towers of Hanoi
Move one disk at a time
No disk can sit on a smaller disk 1 2 3
Get all disks from pole 1 to pole 3

Algorithm idea:
Move top n-1 disks to pole 2
Move bottom disk to pole 3
Move disks from pole 2 to pole 3
HOMEWORK PROBLEM
1. Write a program to check whether a string is palindrome or not
using
a) Recursion
b) Iteration
2. Write a program to find sum of digits of a numbers using
recursion and iteration (in a single for loop).
References:
• https://www.geeksforgeeks.org/recursion/
• https://
www.tutorialspoint.com/data_structures_algorithms/re
cursion_basics.htm
• https://
www.cseworldonline.com/data-structure/recursion-in-d
ata-structures.php
• https://www.youtube.com/watch?v=xp6yWryIQTg

You might also like