Recursion v1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 48

Functions-Recall

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);
}

void interchange (int x, int y)


{ int temp;
printf (“F1: x = %d, y = %d\n”, x, y);
temp= x; x = y; y = temp;
printf (“F2: x = %d, y = %d\n”, x, y);
}
2
Parameter passing & return
void main()
Output
{
int x=10, y=5; M1: x = 10, y = 5
printf (“M1: x = %d, y = %d\n”, x, y);
F1: x = 10, y = 5
interchange (x, y);
printf (“M2: x = %d, y = %d\n”, x, y); F2: x = 5, y = 10
}
M2: x = 10, y = 5
void interchange (int x, int y)
{ int temp;
printf (“F1: x = %d, y = %d\n”, x, y);
temp= x; x = y; y = temp;
printf (“F2: x = %d, y = %d\n”, x, y);
}
3
Parameter passing & return
int x=11,y=6;
void interchange (int a, int b); Homework
int main()
{
{ int x=6,y=11;
interchange (x,y);
}
printf("x=%d y=%d",x,y);
}
void interchange (int x, int y)
{ int temp;
temp=x;
x=y;
y=temp;
}
4
How are function calls
implemented?
 The following applies in general, the implementation
details of function call
 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

5
 Pop activation record, whenever the function
returns

 Activation record looks like:

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

Before call After call After return


7
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

LV2, RV2, RA2

LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1

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

LV2, RV2, RA2

LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1

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

LV2, RV2, RA2

LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1

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

 Used for repetitive computations in which each


action is stated in terms of a previous result
fact(n) = n * fact (n-1)

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

 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

21
Factorial
long int fact (int n)
{
if (n == 1)
return (1);
else
return (n * fact(n-1));
}

22
Factorial Execution

long int fact (int n)


{
if (n = = 1) return (1);
else return (n * fact(n-1));
}

23
Factorial Execution
fact(4)

long int fact (int n)


{
if (n = = 1) return (1);
else return (n * fact(n-1));
}

24
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));
}

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));

long int fact (int n)


{
if (n = = 1) return (1);
else return (n * fact(n-1));
}

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

 We shall illustrate the process by an example of


computing factorial
 Activation record looks like:

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

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);
M: x= 4, y = 24
return val;
38
}
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]
}
39
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)

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 (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
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

You might also like