Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Recursion

What is recursion?
• Sometimes, the best way to solve a
problem is by solving a smaller version of
the exact same problem first

• Recursion is a technique that solves a


problem by solving a smaller problem of
the same type

2
When you turn this into a program, you end up
with functions that call themselves (recursive
functions)
int f(int x)
{
int y;
if(x==0)
return 1;
else
{
y = 2 * f(x-1);
return y+1;
}
}
3
Content of a Recursive Method
• Base case(s).
– Values of the input variables for which we perform
no recursive calls are called base cases (there
should be at least one base case).
– Every possible chain of recursive calls must
eventually reach a base case.
• Recursive calls.
– Calls to the current method.
– Each recursive call should be defined so that it
makes progress towards a base case.

4
Problems defined recursively
• There are many problems whose
solution can be defined recursively
Example: n factorial

1 if n = 0
n!= (recursive solution)
(n-1)!*n if n > 0

1 if n = 0
n!= (closed form solution)
1*2*3*…*(n-1)*n if n > 0

5
Coding the factorial function
• Iterative implementation

int Factorial(int n)
{
int fact = 1;

for(int count = 2; count <= n; count++)


fact = fact * count;

return fact;
}

6
Coding the factorial function
• Recursive implementation

int Factorial(int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1);
}

7
8
Recursion vs. iteration
• Iteration can be used in place of recursion
– An iterative algorithm uses a looping construct
– A recursive algorithm uses a branching structure
• Recursive solutions are often less efficient, in
terms of both time and space, than iterative
solutions
• Recursion can simplify the solution of a
problem, often resulting in shorter, more
easily understood source code

9
How do I write a
recursive function?
• Determine the size factor
• Determine the base case(s)
(the one for which you know the answer)
• Determine the general case(s)
(the one where the problem is expressed as a
smaller version of itself)
• Verify the algorithm
(use the "Three-Question-Method")

10
How is recursion implemented?
• What happens when a function gets called?
int a(int w)
{
return w+w;
}

int b(int x)
{
int z,y;
……………… // other statements
z = a(x) + y;
return z;
}

11
What happens when a function is
called? (cont.)
• An activation record is stored into a stack
(run-time stack)
1) The computer has to stop executing function b
and starts executing function a
2) Since it needs to come back to function b later, it
needs to store everything about function b that is
going to need (x, y, z, and the place to start
executing upon return)
3) Then, x from a is bounded to w from b
4) Control is transferred to function a

12
What happens when a function is
called?
• After function a is executed, the
activation record is popped out of the
run-time stack
• All the old values of the parameters and
variables in function b are restored and
the return value of function a replaces
a(x) in the assignment statement

13
What happens when a recursive
function is called?
• Except the fact that the calling and called functions have
the same name, there is really no difference between
recursive and nonrecursive calls
int f(int x)
{
int y;
if(x==0)
return 1;
else
{
y = 2 * f(x-1);
return y+1;
}
}

14
2*f(2)

2*f(1)

2*f(1)

=f(0)

=f(1)

=f(2)

=f(3)

15
Deciding whether to use a recursive
solution
• The recursive function takes more
memory
• The recursive version does about the
same amount of work as the non-
recursive version
• The recursive version is shorter and
simpler than the non-recursive solution

16
Types of Recursion
• Direct
When a function call itself directly, means it’s a
direct recursive function

• Indirect
When a function calls itself indirectly, it means
it’s calling the function with the help of another
function defined is called indirect recursion

17
Direct Recursion Example
void recursive_function()
{
recursive_function();
}

18
Indirect Recursion Example
void function ()
{
recursive_function () ;
}
void recursive_function ()
{
function () ;
}

19
Indirect Recursion Example

20
Direct Recursion Types
1. Tail recursion
•If a recursive function calling itself and
that recursive call is the last statement in
the function
•Recursive function performs nothing
•Function has to process or perform any
operation at the time of calling and it does
nothing at returning time.
21
Tail Recursion Example
// Recursion function
void fun(int n)
{
if (n > 0) {
cout << n << " ";
fun(n - 1); // Last statement in the function
}
}
int main() {
int x = 3;
fun(x);
} 22
Tail Recursion Example

23
Direct Recursion Types
2. Head Recursion:
– If a recursive function calling itself and that
recursive call is the first statement in the
function
– There’s no statement, no operation before
the call.
– The function doesn’t have to process or
perform any operation at the time of calling
and all operations are done at returning time.

24
Head Recursion Example
// Recursive function
void fun(int n)
{
if (n > 0) {
fun(n - 1); // First statement in the function
cout << " "<< n;
}
}
int main()
{
int x = 3;
fun(x);
}
25
Head Recursion Example

26
Direct Recursion Types
3. Tree Recursion:
– If a recursive function calling itself for one
time then it’s known as Linear Recursion.
Otherwise if a recursive function calling itself
for more than one time then it’s known
as Tree Recursion.

27
Tree Recursion Example
void fun(int n)
{
if (n > 0)
{
cout << " " << n;
fun(n - 1); // Calling once
fun(n - 1); // Calling twice
}
}
int main()
{
fun(3);
}

28
Tree Recursion Example

29
Direct Recursion Types
4. Nested Recursion:
– a recursive function will pass the parameter
as a recursive call. That means “recursion
inside recursion”

30
Nested Recursion Example
int fun(int n)
{
if (n > 100)
return n - 10;
// A recursive function passing parameter
// as a recursive call or recursion inside
// the recursion
return fun(fun(n + 11));
}
int main() {
int r;
r = fun(95);
cout << " " << r;
}
31
Nested Recursion Example

32

You might also like