Adobe Scan 08 Nov 2023
Adobe Scan 08 Nov 2023
• 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.
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
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.
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.
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:
A(0, A(1,0))
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.
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
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
X X X
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