Upload
neron
View
38
Download
0
Embed Size (px)
DESCRIPTION
Functions : Recursion. Lecture 12 4.2.2002. Recursion. a + (a+1) + (a+2) + ... + b = a + ((a+1) + (a+2) + ... + b) sum(a,b) = a + sum(a+1,b) Base case ? factorial (n) = n*factorial(n-1) Base case ?. Recursion. int factorial (int n) { int temp; if (n
Citation preview
31.1.2001 Sudeshna Sarkar, IIT Kharagpur1
Functions : Recursion
Lecture 124.2.2002
31.1.2001 Sudeshna Sarkar, IIT Kharagpur2
Recursion
a + (a+1) + (a+2) + ... + b
= a + ((a+1) + (a+2) + ... + b)
sum(a,b) = a + sum(a+1,b)
Base case ?
factorial (n) = n*factorial(n-1)
Base case ?
31.1.2001 Sudeshna Sarkar, IIT Kharagpur3
Recursion
int sum (int a, int b)
{
int temp;
if (a>b) return 0;
temp=sum(a+1,b);
return a+temp;
}
int factorial (int n)
{
int temp;
if (n<=1) return 1;
temp=factorial(n-1);
return n*temp;
}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur4
Recursion
main () { . . .
x = sum(1,4); . . .}int sum (int a, int b){
int temp;if (a>b) return 0;temp=sum(a+1,b);return a+temp;
}
1 4sum(a,b) { temp=sum(2,4); return 1+temp;}
sum(2,4){ temp=sum(3,4); return 2+temp;}
sum(3,4){ temp=sum(4,4); return 3+temp;}
sum(4,4){ temp=sum(5,4); return 4+temp;}
sum(5,4){ return 0;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur5
Recursion
main () { . . .
x = sum(1,5); . . .}int sum (int a, int b){
int temp;if (a>b) return 0;temp=sum(a+1,b);return a+temp;
}
sum(1,4){ temp=sum(2,4); return 1+temp;}
sum(2,4){ temp=sum(3,4); return 2+temp;}
sum(3,4){ temp=sum(4,4); return 3+temp;}
sum(4,4){ temp=0; return 4;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur6
Recursion
main () { . . .
x = sum(1,5); . . .}int sum (int a, int b){
int temp;if (a>b) return 0;temp=sum(a+1,b);return a+temp;
}
sum(1,4){ temp=sum(2,4); return 1+temp;}
sum(2,4){ temp=sum(3,4); return 2+temp;}
sum(3,4){ temp=4; return 3+4;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur7
Recursion
main () { . . .
x = sum(1,5); . . .}int sum (int a, int b){
int temp;if (a<=b) return 0;temp=sum(a+1,b);return a+temp;
}
sum(1,4){ temp=sum(2,4); return 1+temp;}
sum(2,4){ temp=7; return 2+7;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur8
Recursion
main () { . . .
x = sum(1,5); . . .}int sum (int a, int b){
int temp;if (a<=b) return 0;temp=sum(a+1,b);return a+temp;
}
sum(1,4){ temp=9; return 1+9;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur9
void foo ()
{
char ch;
scanf (“%c”, &ch);
if (ch==‘\n’) return;
foo ();
printf (“%c”, &ch);
}
What will the programoutput on the followinginput ?PDS \n
31.1.2001 Sudeshna Sarkar, IIT Kharagpur10
Example
int sumSquares (int m, int n) {
if (m<n)
/* Recursion */
return sumSquares(m, n-1) + n*n;
else
/* Base Case */
return n*n ;
}
int sumSquares (int m, int n) {if (m<n)
/* Recursion */return m*m +
sumSquares(m+1, n);
else /* Base Case */return m*m ;
}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur11
Example
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) ;
}
5 . . . 1087 . . .
mmidmid+1
n
31.1.2001 Sudeshna Sarkar, IIT Kharagpur12
Call Tree
sumSquares(5,10)sumSquares(5,10)
sumSquares(5,7) sumSquares(5,10)sumSquares(8,10)
sumSquares(5,6) sumSquares(7,7) sumSquares(8,9) sumSquares(10,10)
sumSquares(5,5) sumSquares(6,6) sumSquares(8,8) sumSquares(9,9)
31.1.2001 Sudeshna Sarkar, IIT Kharagpur13
Annotated Call Tree
sumSquares(5,10)sumSquares(5,10)
sumSquares(5,7) sumSquares(5,10)sumSquares(8,10)
sumSquares(5,6) sumSquares(7,7) sumSquares(8,9) sumSquares(10,10)
sumSquares(5,5) sumSquares(6,6) sumSquares(8,8) sumSquares(9,9)
355
110
61 49
245
145 100
25 36 64 81
25 36 49 64 81 100
31.1.2001 Sudeshna Sarkar, IIT Kharagpur14
Trace
sumSq(5,10) = (sumSq(5,7) + sumSq(8,10))= (sumSq(5,6) + (sumSq(7,7)) + (sumSq(8,9) + sumSq(10,10))= ((sumSq(5,5) + sumSq(6,6)) + sumSq(7,7)) + ((sumSq(8,8) + sumSq(9,9)) + sumSq(10,10))= ((25 + 36) + 49) + ((64 + 81) + 100)= (61 + 49) + (145 + 100)= (110 + 245)= 355
31.1.2001 Sudeshna Sarkar, IIT Kharagpur15
Recursion : The general idea
Recursive programs are programs that call themselves to compute the solution to a subproblem having these
properties : 1. the subproblem is smaller than the overall problem or,
simpler in the sense that it is closer to the final solution
2. the subproblem can be solved directly (as a base case) or recursively by making a recursive call.
3. the subproblem’s solution can be combined with solutions to other subproblems to obtain the solution to the overall problem.
31.1.2001 Sudeshna Sarkar, IIT Kharagpur16
Think recursively
Break a big problem into smaller subproblems of the same kind, that can be combined back into the overall solution :
Divide and Conquer
31.1.2001 Sudeshna Sarkar, IIT Kharagpur17
Exercise
1. Write a recursive function that computes xn , called power (x, n), where x is a floating point number and n is a non-negative integer.
2. Write an improved recursive version of power(x,n) that works by breaking n down into halves, squaring power(x, n/2), and multiplying by x again if n is odd.
3. To calculate the square root of x (a +ve real) by Newton’s method, we start with an initial approximation a=x/2. If |x-aa| epsilon, we stop with the result a. Otherwise a is replaced with the next approximation, (a+x/a)/2. Write a recursive method, sqrt(x) to compute square root of x by Newton’s method.
31.1.2001 Sudeshna Sarkar, IIT Kharagpur18
Common Pitfalls
Infinite Regress : a base case is never encountered a base case that never
gets called : fact (0) running out of resources :
each time a function is called, some space is allocated to store the activation record. With too many nested calls, there may be a problem of space.
int fact (int n) { if (n==1) return 1; return n*fact(n-1);}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur19
Tower of Hanoi
A B C
31.1.2001 Sudeshna Sarkar, IIT Kharagpur20
Tower of Hanoi
A B C
31.1.2001 Sudeshna Sarkar, IIT Kharagpur21
Tower of Hanoi
A B C
31.1.2001 Sudeshna Sarkar, IIT Kharagpur22
Tower of Hanoi
A B C
31.1.2001 Sudeshna Sarkar, IIT Kharagpur23
void towers (int n, char from, char to, char aux) { if (n==1) {
printf (“Disk 1 : %c -> &c \n”, from, to) ; return ;
} towers (n-1, from, aux, to) ; printf (“Disk %d : %c %c\n”, n, from, to) ; towers (n-1, aux, to, from) ;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur24
Disk 1 : A -> CDisk 2 : A -> BDisk 1 : C -> BDisk 3 : A -> CDisk 1 : B -> ADisk 2 : B -> CDisk 1 : A -> CDisk 4 : A -> BDisk 1 : C -> BDisk 2 : C -> ADisk 1 : B -> ADisk 3 : C -> BDisk 1 : A -> CDisk 2 : A -> BDisk 1 : C -> B
towers (4, ‘A’, ‘B’, ‘C’) ;
31.1.2001 Sudeshna Sarkar, IIT Kharagpur25
Recursion may be expensive !
Fibonacci Sequence: fib (n) = n if n is 0 or 1 fib (n) = fib (n-2) + fib(n-1) if n>= 2.
int fib (int n) { if (n==0 or n==1)
return 1; return fib(n-2) + fib(n-1) ;}
31.1.2001 Sudeshna Sarkar, IIT Kharagpur26
Call Tree
fib (5)
fib (3) fib (4)
fib (1)
fib (2)fib (1) fib (2)
fib (0)
fib (3)
fib (1)
fib (1) fib (2)
fib (0)
fib (0) fib (1)
0 1 0 1 1
1
10
1 1 1
111010
2
2
3
5
0 1
31.1.2001 Sudeshna Sarkar, IIT Kharagpur27
Iterative fibonacci computation
int fib (int n) { if (n <= 1)
return n; lofib = 0 ; hifib = 1 ; for (i=2; i<=n; i++) {
x = lofib ;lofib = hifib ;hifib = x + lofib;
} return hifib ;}
i = 2 3 4 5 6 7x = 0 1 1 2 3 5lofib= 0 1 1 2 3 5 8hifib= 1 1 2 3 5 8 13
31.1.2001 Sudeshna Sarkar, IIT Kharagpur28
Merge Sort
To sort an array of N elements, 1. Divide the array into two halves. Sort each
half. 2. Combine the two sorted subarrays into a
single sorted array Base Case ?
31.1.2001 Sudeshna Sarkar, IIT Kharagpur29
Binary Search revisited
To search if key occurs in an array A (size N) sorted in ascending order:
mid = N/2; if (key == N/2) item has been found if (key < N/2) search for the key in A[0..mid-1] if (key > N/2) search for key in A[mid+1..N]
Base Case ?