Recursion: - Recursion Is A Fundamental Programming
Recursion: - Recursion Is A Fundamental Programming
hmehta.scs@dauniv.ac.in 1
Recursive Thinking
• A recursive definition is one which uses the word
or concept being defined in the definition itself
hmehta.scs@dauniv.ac.in 2
Recursive Definitions
• Consider the following list of numbers:
A LIST is a: number
or a: number comma LIST
hmehta.scs@dauniv.ac.in 3
Recursive Definitions
• The recursive part of the LIST definition is
used several times, terminating with the
non-recursive part:
number comma LIST
24 , 88, 40, 37
number
37
hmehta.scs@dauniv.ac.in 4
Infinite Recursion
• All recursive definitions have to have a non-
recursive part
hmehta.scs@dauniv.ac.in 5
Recursive Definitions
• N!, for any positive integer N, is defined to be the
product of all integers between 1 and N inclusive
hmehta.scs@dauniv.ac.in 6
Recursive Definitions
5!
120
5 * 4!
24
4 * 3!
6
3 * 2!
2
2 * 1!
1
hmehta.scs@dauniv.ac.in 7
Recursive Programming
• A Function can invoke itself; if set up that way, it
is called a recursive function
hmehta.scs@dauniv.ac.in 8
Recursive Programming
• Consider the problem of computing the sum of all
the numbers between 1 and any positive integer N
N N −1 N −2
∑i
i =1
= N + ∑i
i =1
= N + N −1 + ∑i
i =1
N −3
= N + N −1 + N − 2 + ∑i
i =1
o
hmehta.scs@dauniv.ac.in 9
Recursive Programming
// This function returns the sum of 1 to num
int sum (int num)
{
int result;
if (num == 1)
result = 1;
else
result = num + sum (n-1);
return result;
}
hmehta.scs@dauniv.ac.in 10
Recursive Programming
result = 6
main
sum(3)
result = 3
sum
sum(2)
result = 1
sum
sum(1)
sum
hmehta.scs@dauniv.ac.in 11
Recursive Programming
• Note that just because we can use recursion to
solve a problem, doesn't mean we should
hmehta.scs@dauniv.ac.in 12
Indirect Recursion
• A function invoking itself is considered to be
direct recursion
hmehta.scs@dauniv.ac.in 13
Indirect Recursion
m1 m2 m3
m1 m2 m3
m1 m2 m3
hmehta.scs@dauniv.ac.in 14
Towers of Hanoi
• The Towers of Hanoi is a puzzle made up of three
vertical pegs and several disks that slide on the
pegs
hmehta.scs@dauniv.ac.in 15
Towers of Hanoi
Move 2 Move 3
hmehta.scs@dauniv.ac.in 16
Towers of Hanoi
Move 4 Move 5
hmehta.scs@dauniv.ac.in 17
Towers of Hanoi
• An iterative solution to the Towers of Hanoi is
quite complex
hmehta.scs@dauniv.ac.in 18
Towers of Hanoi
#include <stdio.h>
#include <conio.h>
void transfer(int,char,char,char);
int main()
{
int n;
printf("Recursive Solution to Towe of Hanoi Problem\n");
printf("enter the number of Disks");
scanf("%d",&n);
transfer(n,'L','R','C');
getch();
return 0;
}
void transfer(int n,char from,char to,char temp)
{
if (n>0)
{
transfer(n-1,from,temp,to); /* Move n-1 disk from origin to temporary */
printf("Move Disk %d from %c to %c\n",n,from,to);
transfer(n-1,temp,to,from); /* Move n-1 disk from temporary to origin */
}
return;
}
hmehta.scs@dauniv.ac.in 19
Drawbacks of Recursion
Regardless of the algorithm used, recursion has two
important drawbacks:
Function-Call Overhead
Memory-Management Issues
hmehta.scs@dauniv.ac.in 20
Eliminating Recursion — Tail
Recursion
A special kind of recursion is tail recursion.
Tail recursion is when a recursive call is the last thing a
function does.
Tail recursion is important because it makes the
recursion → iteration conversion very easy.
That is, we like tail recursion because it is easy to
eliminate.
In fact, tail recursion is such an obvious thing to optimize
that some compilers automatically convert it to iteration.
hmehta.scs@dauniv.ac.in 21
Eliminating Recursion — Tail
Recursion
For a void function, tail recursion looks like this:
hmehta.scs@dauniv.ac.in 22
A tail-recursive Factorial Function
We will use an auxiliary function to rewrite factorial as tail-
recursive:
hmehta.scs@dauniv.ac.in 23