Professional Documents
Culture Documents
Recursion v1
Recursion v1
Recursion v1
1
Parameter passing & return
void main()
Output
{
int x=10, y=5;
printf (“M1: x = %d, y = %d\n”, x, y);
interchange (x, y);
printf (“M2: x = %d, y = %d\n”, x, y);
}
5
Pop activation record, whenever the function
returns
Local
Variables
Return Value
Return Addr
6
void main()
{ int gcd (int x, int y)
…….. {
x = gcd (a, b); ……..
…….. ……..
} return (result);
}
Local
Activation Variables
record Return Value
STACK
Return Addr
3 times
Before call Call ncr Call fact fact returns ncr returns
8
Push activation record
a, b (Local x
Variables)
Return Value main
Return Addr
9
Push activation record
Parameter n, r, p (Local
passing Variables)
Return Value nCr
Return Addr
a, b (Local x
Variables)
Return Value main
Return Addr
10
void main()
{
…….. int ncr (int n, int r)
x = ncr (a, b); {
…….. x=fact(n); y=fact(r); int fact (int n)
} z=fact(n-r); 3 times {
p=x/(y*z) ………
return (p); return (result);
} }
3 times
Before call Call ncr Call fact fact returns ncr returns
11
Push activation record
n, result (Local
Variables) fact
Return Value result
Return Addr Return
Parameter n, r (Local x
passing Variables)
Return Value nCr
Return Addr
a, b (Local x
Variables)
Return Value main
Return Addr
12
Pop activation record
Local
Variables
Return Value nCr
Return Addr
Local
Variables
Return Value main
Return Addr
13
void main()
{
…….. int ncr (int n, int r)
x = ncr (a, b); {
…….. x=fact(n); y=fact(r); int fact (int n)
} z=fact(n-r); 3 times {
p=x/(y*z) ………
return (p); return (result);
} }
3 times
Before call Call ncr Call fact fact returns ncr returns
14
Push activation record
Local
Variables fact
result
Return Value
Return Addr
Local y
Variables
Return Value nCr
Return Addr
Local
Variables
Return Value main
Return Addr
15
Pop activation record
Local
Variables nCr
Return Value p
Return Addr Return
Local x
Variables
Return Value main
Return Addr
16
Pop activation record
a, b (Local x
Variables)
Return Value main
Return Addr
17
Recursion
18
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
19
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
Stopping condition
fact(n) = 1, if n = 0
= n * fact(n-1), if n > 0
Recursive definition
20
Examples:
Factorial:
fact(0) = 1
fact(n) = n * fact(n-1), if n > 0
21
Factorial
long int fact (int n)
{
if (n == 1)
return (1);
else
return (n * fact(n-1));
}
22
Factorial Execution
23
Factorial Execution
fact(4)
24
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
25
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
if (3 = = 1) return (1);
else return (3 * fact(2));
26
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
if (3 = = 1) return (1);
else return (3 * fact(2));
if (2 = = 1) return (1);
else return (2 * fact(1));
long int fact (int n)
{
if (n = = 1) return (1);
else return (n * fact(n-1));
}
27
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
if (3 = = 1) return (1);
else return (3 * fact(2));
if (2 = = 1) return (1);
else return (2 * fact(1));
long int fact (int n)
{ if (1 = = 1) return (1);
if (n = = 1) return (1);
else return (n * fact(n-1));
}
28
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
if (3 = = 1) return (1);
else return (3 * fact(2));
if (2 = = 1) return (1);
else return (2 * fact(1)); 1
long int fact (int n)
{ if (1 = = 1) return (1);
if (n = = 1) return (1);
else return (n * fact(n-1));
}
29
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
if (3 = = 1) return (1);
else return (3 * fact(2)); 2
if (2 = = 1) return (1);
else return (2 * fact(1)); 1
long int fact (int n)
{ if (1 = = 1) return (1);
if (n = = 1) return (1);
else return (n * fact(n-1));
}
30
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3));
if (3 = = 1) return (1);
else return (3 * fact(2)); 2
if (2 = = 1) return (1);
else return (2 * fact(1)); 1
long int fact (int n)
{ if (1 = = 1) return (1);
if (n = = 1) return (1);
else return (n * fact(n-1));
}
31
Factorial Execution
fact(4)
if (4 = = 1) return (1);
else return (4 * fact(3)); 6
if (3 = = 1) return (1);
else return (3 * fact(2)); 2
if (2 = = 1) return (1);
else return (2 * fact(1)); 1
long int fact (int n)
{ if (1 = = 1) return (1);
if (n = = 1) return (1);
else return (n * fact(n-1));
}
32
Factorial Execution
fact(4) 24
if (4 = = 1) return (1);
else return (4 * fact(3)); 6
if (3 = = 1) return (1);
else return (3 * fact(2)); 2
if (2 = = 1) return (1);
else return (2 * fact(1)); 1
long int fact (int n)
{ if (1 = = 1) return (1);
if (n = = 1) return (1);
else return (n * fact(n-1));
}
33
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 continuously
Large stack space required
34
Activationrecords keep popping off, when the
termination condition of recursion is reached
Local
Variables
Return Value
Return Addr
35
Example:: main() calls fact(3)
void main()
{
int fact (n)
int n;
int n;
n = 3;
{
printf (“%d \n”, fact(n) );
if (n = = 0)
}
return (1);
else
return (n * fact(n-1));
}
36
TRACE OF THE STACK DURING EXECUTION
n=0
1
RA .. fact fact
n=1 n=1 n=1 returns
main - 1*1 = 1
- to main
calls 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 .. main RA .. main RA .. main RA .. main RA .. main
37
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
Fibonacci recurrence:
fib(n) = 1 if n = 0 or 1;
= fib(n – 2) + fib(n – 1)
otherwise;
fib (0) fib (1) fib (0) fib (1) fib (1) fib (2)
40
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 (0) fib (1) fib (0) fib (1) fib (1) fib (2)
1 1 1 1
1
fib (0) fib (1)
1 1
fib.c 41
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)
8 otherwise;
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
42
Mergesort
43
Basic Idea
Divide the array into two halves
Sort the two sub-arrays
Merge the two sorted sub-arrays into a single
sorted array
Step 2 (sorting the sub-arrays) is done
recursively (divide in two, sort, merge) until the
array has a single element (base condition of
recursion)
44
Merging Two Sorted Arrays
Problem: Two sorted arrays A and B are given. We are required to
produce a final sorted array C which contains all elements of A and B.
3 4 7 8 9 3 4 7 8 9
2 5 7 2 5 7
2
3 4 7 8 9 3 4 7 8 9
2 5 7 2 5 7
2 3 2 3 4
45
3 4 7 8 9 3 4 7 8 9
2 5 7 2 5 7
2 3 4 5 2 3 4 5 7
3 4 7 8 9 3 4 7 8 9
2 5 7 2 5 7
2 3 4 5 7 7 2 3 4 5 7 7 8
46
Merge Code
void
merge (int A[], int B[], int C[], int m,int n)
{
3 4 7 8 9 int i=0,j=0,k=0;
while (i<m && j<n)
2 5 7 {
if (A[i] < B[j]) C[k++] = A[i++];
2 3 4 57 7 8 9 else C[k++] = B[j++];
}
while (i<m) C[k++] = A[i++];
while (j<n) C[k++] = B[j++];
}
47
Merge Sort: Sorting an array recursively
void mergesort (int A[], int n)
{
int i, j, B[max];
if (n <= 1) return;
i = n/2;
mergesort(A, i);
mergesort(A+i, n-i);
merge(A, A+i, B, i, n-i);
for (j=0; j<n; j++) A[j] = B[j];
free(B);
}
48