Recursion: - Recursion Is A Fundamental Programming
Recursion: - Recursion Is A Fundamental Programming
[email protected] 1
Recursive Thinking
• A recursive definition is one which uses the word
or concept being defined in the definition itself
[email protected] 2
Recursive Definitions
• Consider the following list of numbers:
A LIST is a: number
or a: number comma LIST
[email protected] 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
[email protected] 4
Infinite Recursion
• All recursive definitions have to have a non-
recursive part
[email protected] 5
Recursive Definitions
• N!, for any positive integer N, is defined to be the
product of all integers between 1 and N inclusive
[email protected] 6
Recursive Definitions
5!
120
5 * 4!
24
4 * 3!
6
3 * 2!
2
2 * 1!
1
[email protected] 7
Recursive Programming
• A Function can invoke itself; if set up that way, it
is called a recursive function
[email protected] 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
[email protected] 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;
}
[email protected] 10
Recursive Programming
result = 6
main
sum(3)
result = 3
sum
sum(2)
result = 1
sum
sum(1)
sum
[email protected] 11
Recursive Programming
• Note that just because we can use recursion to
solve a problem, doesn't mean we should
[email protected] 12
Indirect Recursion
• A function invoking itself is considered to be
direct recursion
[email protected] 13
Indirect Recursion
m1 m2 m3
m1 m2 m3
m1 m2 m3
[email protected] 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
[email protected] 15
Towers of Hanoi
Move 2 Move 3
[email protected] 16
Towers of Hanoi
Move 4 Move 5
[email protected] 17
Towers of Hanoi
• An iterative solution to the Towers of Hanoi is
quite complex
[email protected] 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;
}
[email protected] 19
Drawbacks of Recursion
Regardless of the algorithm used, recursion has two
important drawbacks:
Function-Call Overhead
Memory-Management Issues
[email protected] 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.
[email protected] 21
Eliminating Recursion — Tail
Recursion
For a void function, tail recursion looks like this:
[email protected] 22
A tail-recursive Factorial Function
We will use an auxiliary function to rewrite factorial as tail-
recursive: