Recursion
in g a nd Plac ement VNIT
o f Tr ain
D e s hm u k h D epartment
By Yogita Nagpur
1
Recursion
void recurse()
{
1. A function that ... .. ...
calls itself is recurse();
known as a ... .. ...
recursive }
function.
And, this int main()
technique is {
known as ... .. ...
recursion. recurse();
... .. ... 2
By Yogita Deshmukh Department
of Training and}Placement VNIT
Functions
.
By Yogita Deshmukh Department 3
of Training and Placement VNIT
.
By Yogita Deshmukh Department 4
of Training and Placement VNIT
By Yogita Deshmukh Department of Training and
Placement VNIT Nagpur
5
Function Aspects
Function declaration A function must be declared globally in
a c program to tell the compiler about the function name,
function parameters, and return type.
Function call Function can be called from anywhere in the
program. The parameter list must not differ in function calling
and function declaration. We must pass the same number of
functions as it is declared in the function declaration.
Function definition It contains the actual statements which
are to be executed. It is the most important aspect to which
the control comes when the function is called. Here, we must
By Yogita Deshmukh Department of Training and
Placement VNIT Nagpur
notice that only one value can be returned from the function.
6
C function aspects Syntax
1
Function declaration return_type function_name
(argument list);
2
Function call function_name (argument_list)
3
Function definition return_type function_name
(argument list)
{
function
By Yogita Deshmukh Department of Training and body;
Placement VNIT Nagpur
}
7
By Yogita Deshmukh Department of Training and
Placement VNIT Nagpur
8
How are function calls
implemented?
■ The following applies in general, with minor variations
that are implementation dependent
◻ The system maintains a stack in memory
■ Stack is a last-in first-out structure
■ Two operations on stack, push and pop
◻ Whenever there is a function call, the activation
record gets pushed into the stack
■ Activation record consists of the return address
in the calling program, the return value from the
function, and the local variables inside the
function
By Yogita Deshmukh Department 9
of Training and Placement VNIT
void main()
{ int gcd (int x, int y)
…….. {
x = gcd (a, b); ……..
…….. ……..
} return (result);
}
Local
Activation Variables
record Return Value
STACK
Return Addr
Before call After call After return
By Yogita Deshmukh Department 10
of Training and Placement VNIT
Recursion
■ A process by which a function calls itself
repeatedly
◻ Either directly.
■ X calls X
◻ Or cyclically in a chain.
■ X calls Y, and Y calls X
■ Used for repetitive computations in which each
action is stated in terms of a previous result
fact(n) = n * fact (n-1)
By Yogita Deshmukh Department 11
of Training and Placement VNIT
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
n! = n * (n-1)!
ComputeFactorial 12
By Yogita Deshmukh Department
of Training and Placement VNIT
Program for factorial of a
number
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
By Yogita Deshmukh Department 13
printf("Factorial of %d is: %d",number,fact);
of Training and Placement VNIT
Contd.
■ For a problem to be written in recursive form,
two conditions are to be satisfied:
◻ It should be possible to express the problem
in recursive form
■ Solution of the problem in terms of solution of the
same problem on smaller sized data
◻ The problem statement must include a
stopping condition
fact(n) = 1, if n =
= n * fact(n-1), 0 if Stopping condition
Recursive definition
n>
0 Department
By Yogita Deshmukh 14
of Training and Placement VNIT
■ Examples:
◻ Factorial:
fact(0) = 1
fact(n) = n * fact(n-1), if n > 0
◻ Fibonacci series
(1,1,2,3,5,8,13,21,….) fib (0) = 1
fib (1) = 1
fib (n) = fib (n-1) + fib (n-2), if n > 1
By Yogita Deshmukh Department 15
of Training and Placement VNIT
Factorial
long intfact (int n)
{
if (n ==
1) return
(1);
else
return (n *
fact(n-1));
}
By Yogita Deshmukh Department 16
of Training and Placement VNIT
Factorial Execution
long int fact (int n)
{
if (n = = 1) return
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 17
of Training and Placement VNIT
Factorial Execution
fact(
4)
long int fact (int n)
{
if (n = = 1) return
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 18
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
long int fact (int n)
{
if (n = = 1) return
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 19
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3
* fact(2));
long int fact (int n)
{
if (n = = 1) return
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 20
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3
* fact(2));
long int fact (int n)
{ if (2 = = 1) return
if (n = = 1) return (1); else return (2
* fact(1));
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 21
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3
* fact(2));
long int fact (int n)
{ if (2 = = 1)if return
(1 = = 1)
if (n = = 1) return (1); else return
return (2(1);
* fact(1));
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 22
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return
if (3
(2 = = 1) return
* fact(2)); (1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 23
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3 2
* fact(2));
if (2 = = 1) return
(1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 24
of Training and Placement VNIT
Factorial Execution
fact(4)
if (4 = = 1) return
(1); else return (4
* fact(3));
if (3 = = 1) return
(1); else return (3 2
* fact(2));
if (2 = = 1) return
(1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 25
of Training and Placement VNIT
Factorial Execution
fact(
4)
if (4 = = 1) return
(1); else return (4 6
* fact(3));
if (3 = = 1) return
(1); else return (3 2
* fact(2));
if (2 = = 1) return
(1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 26
of Training and Placement VNIT
Factorial Execution
fact( 24
4)
if (4 = = 1) return
(1); else return (4 6
* fact(3));
if (3 = = 1) return
(1); else return (3 2
* fact(2));
if (2 = = 1) return
(1); else return (2 1
long int fact (int n) * fact(1));
{ if (1 = = 1)
if (n = = 1) return return (1);
(1); else return (n *
fact(n-1));
} By Yogita Deshmukh Department 27
of Training and Placement VNIT
Look at the variable addresses (a slightly
different program) !
void main()
{ Output
int x,y;
4
scanf("%d",&x); F: data = 4, &data = 3221224528
y = fact(x);
&val = 3221224516
printf ("M: x= %d, y = %d\n", x,y);
F: data = 3, &data = 3221224480
}
&val = 3221224468
int fact(int data) F: data = 2, &data = 3221224432
{ int val = 1; &val = 3221224420
printf("F: data = %d, &data = %u \n F: data = 1, &data = 3221224384
&val = %u\n", data, &data, &val);
&val = 3221224372
if (data>1) val = data*fact(data-1);
return val; M: x= 4, y = 24
} By Yogita Deshmukh Department 28
of Training and Placement VNIT
How are function calls
implemented?
■ The following applies in general, with minor variations
that are implementation dependent
◻ The system maintains a stack in memory
■ Stack is a last-in first-out structure
■ Two operations on stack, push and pop
◻ Whenever there is a function call, the activation
record gets pushed into the stack
■ Activation record consists of the return address
in the calling program, the return value from the
function, and the local variables inside the
function
By Yogita Deshmukh Department 29
of Training and Placement VNIT
void main()
{ int gcd (int x, int y)
…….. {
x = gcd (a, b); ……..
…….. ……..
} return (result);
}
Local
Activation Variables
record Return Value
STACK
Return Addr
Before call After call After return
By Yogita Deshmukh Department 30
of Training and Placement VNIT
void main()
{
…….. int ncr (int n, int r)
x = ncr (a, b); {
…….. return (fact(n)/ int fact (int n)
} fact(r)/fact(n-r)); 3 times {
} ………
return (result);
}
3 times
LV2, RV2, RA2
LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1
Before call Call ncr Call fact fact returns ncr returns
By Yogita Deshmukh Department 31
of Training and Placement VNIT
What happens for recursive
calls?
■ What we have seen ….
◻ Activation record gets pushed into the stack
when a function call is made
◻ Activation record is popped off the stack
when the function returns
■ In recursion, a function calls itself
◻ Several function calls going on, with none of
the function calls returning back
■ Activation records are pushed onto the stack
By Yogita Deshmukh Department 32
47
continuouslyof Training and Placement VNIT
◻ Activation records keep popping off, when the
termination condition of recursion is reached
■ We shall illustrate the process by an example of
computing factorial
◻ Activation record looks like:
Local
Variables
Return Value
Return Addr
By Yogita Deshmukh Department 33
of Training and Placement VNIT
Example:: main() calls fact(3)
void main()
{ int fact
int n;
(n) int n;
n = 3;
{
printf (“%d \n”, fact(n) ); if (n = =
}
0) return
(1);
else
return (n * fact(n-1));
By Yogita Deshmukh Department 34
}
of Training and Placement VNIT
TRACE OF THE STACK DURING EXECUTION
n=0
1
RA .. fact fact
n=1 n=1 n=1 returns
main - - 1*1 = 1
calls to main
RA .. fact RA .. fact RA .. fact
fact n=2 n=2 n=2 n=2 n=2
- - - - 2*1 = 2
RA .. fact RA .. fact RA .. fact RA .. fact RA .. fact
n=3 n=3 n=3 n=3 n=3 n=3 n=3
- - - - - - 3*2 = 6
RA .. main RA .. main RA .. RA .. main RA .. main RA .. main RA .. main
main
By Yogita Deshmukh Department 35
of Training and Placement VNIT
Do Yourself
■ Trace the activation records for the following
version of Fibonacci sequence
int f (int n)
{ int a, b; Local
if (n return (n); Variables
< 2) else (n, a, b)
X { Return Value
a = f(n-1);
Return Addr
Y return (a+b);
b = f(n-2); (either main,
} or X, or Y)
}
void main() {
printf(“Fib(4) is: %d \n”, f(4));
main }
By Yogita Deshmukh Department 36
of Training and Placement VNIT
Fibonacci Numbers
Fibonacci recurrence:
fib(n) = 1 if n = 0 or 1;
= fib(n – 2) + fib(n – 1)
otherwise;
int fib (int n){
if (n == 0 or n == 1)
return 1; [BASE]
return fib(n-2) + fib(n-1) ;
[Recursive]
}
By Yogita Deshmukh Department 37
of Training and Placement VNIT
int fib (int n) {
if (n == 0 || n == 1) Fibonacci recurrence:
return 1; fib(n) = 1 if n = 0 or 1;
return fib(n-2) + fib(n-1) ; = fib(n – 2) + fib(n – 1)
}
otherwise;
fib (5)
fib (3) fib (4)
fib (1) fib (2) fib (2) fib (3)
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
fib (0) fib (1)
By Yogita Deshmukh Department 38
of Training and Placement VNIT
while (t != 0)
{
r = r * 10;
r = r + t%10;
t = t/10;
}
5225
By Yogita Deshmukh Department 39
of Training and Placement VNIT
int fib (int n) {
if (n == 0 || n == 1) Fibonacci recurrence:
return 1; fib(n) = 1 if n = 0 or 1;
return fib(n-2) + fib(n-1) ; = fib(n – 2) + fib(n – 1)
}
otherwise;
fib (5)
fib (3) fib (4)
fib (1) fib (2) fib (2) fib (3)
1
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
1 1 1 1 1
fib (0) fib (1)
1 1
By Yogita Deshmukh Department fib.c 40
20
of Training and Placement VNIT
int fib (int n) {
if (n==0 || n==1) Fibonacci recurrence:
return 1; fib(n) = 1 if n = 0 or 1;
return fib(n-2) + fib(n-1) ; = fib(n – 2) + fib(n – 1)
}
otherwise;
8
fib (5)
3 5
fib (3) fib (4)
1 2 2 3
fib (1) fib (2) fib (2) fib (3)
1 2
1 1 1 1 1
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
1 1 1 1 1 1
1
fib (0) fib (1)
1 1
By Yogita Deshmukh Department 41
of Training and Placement VNIT
Sum of Squares
int sumSquares (int m, int n)
{
int middle ;
if (m == n) return m*m;
else
{
middle = (m+n)/2;
return sumSquares(m,middle)
+ sumSquares(middle+1,n);
}
}
By Yogita Deshmukh Department 42
of Training and Placement VNIT
Annotated Call
Tree 355
sumSquares(5,10)
110 245
sumSquares(5,7) sumSquares(8,10)
61 49 145 100
sumSquares(5,6) sumSquares(7,7) sumSquares(8,9) sumSquares(10,10)
25 36 64 81
sumSquares(5,5) sumSquares(6,6) sumSquares(8,8) sumSquares(9,9)
25 36 49 64 Department 81 100 23
43
By Yogita Deshmukh
of Training and Placement VNIT
Towers of Hanoi Problem
1
2
3
4
5
LEFT CENTER RIGHT
By Yogita Deshmukh Department 44
of Training and Placement VNIT
■ Initially all the disks are stacked on the
LEFT pole
■ Required to transfer all the disks to the
RIGHT pole
◻ Only one disk can be moved at a time.
◻ A larger disk cannot be placed on a smaller
disk
■ CENTER pole is used for temporary
storage of disks
By Yogita Deshmukh Department 45
of Training and Placement VNIT
■ Recursive statement of the general problem of n
disks
◻ Step 1:
■ Move the top (n-1) disks from LEFT to CENTER
◻ Step 2:
■ Move the largest disk from LEFT to RIGHT
◻ Step 3:
■ Move the (n-1) disks from CENTER to RIGHT
By Yogita Deshmukh Department 46
of Training and Placement VNIT
Relook at recursive Fibonacci:
Not efficient !! Same sub-problem solved many times.
int fib (int n) {
if (n==0 || n==1)
fib (5) return 1;
return fib(n-2) + fib(n-1) ;}
fib (3) fib (4)
fib (1) fib (2) fib (2) fib (3)
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
How many calls? fib (0) fib (1)
How many additions?
By Yogita Deshmukh Department 47
of Training and Placement VNIT
■ Every recursive program can also be written
without recursion
■ Recursion is used for programming
convenience, not for performance
enhancement
■ Sometimes, if the function being computed has
a nice recurrence form, then a recursive code
may be more readable
By Yogita Deshmukh Department 48
of Training and Placement VNIT