Download presentation
Presentation is loading. Please wait.
Published byLewis Sullivan Modified over 9 years ago
1
Recursion
2
Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 n! = n(n-1)(n-2)…2 * 1
3
Factorial problem can be broken down into smaller problems. Note: 0! = 1 10! 109! 87!98!32!54!43!76!21!10! * * * * * * * * *
4
Recursive Algorithm An algorithm that calls itself with smaller and smaller subsets of the original input, doing a little bit or work each time, until it reaches the base case. factorial(n) = n * factorial(n-1) where factorial(0)=1
5
Sample output for recursive factorial algorithm fac(1) = 1 * fac(0) = 1 * 1 1! = 1 fac(2) = 2 * fac(1) = 2 * (1 * fac(0)) = 2 * (1 * 1) 2! = 2 fac(3) = 3 * fac(2) = 3 * (2 * fac(1)) = 3 * (2 * (1 * fac(0))) = 3 * (2 * (1 * 1)) 3! = 6 fac(n) = n * fac(n-1) fac(0) = 1 fac(4) = 4 * fac(3) = 4 * (3 * fac(2)) = 4 * (3 * (2 * fac(1))) = 4 * (3 * (2 * (1 * fac(0)))) = 4 * (3 * (2 * (1 * 1))) 4! = 24
6
Writing recursive functions Recursive function keeps calling itself until it reaches the base case. Base case - the part which makes recursion actually work by forcing it to stop at the necessary point. Leaving out the base case will almost certain result in a function which never terminates. Usually a conditional statement Recursive case - the part in which the function actually calls itself with diminished input. The function must not call itself with the exact same input again, otherwise it will continue doing so forever! Recursive algorithm
7
Base Case and Recursive Case Base case: fac(0) = 1 Recursive Case: fac(n) = n * fac(n-1) #include using namespace std; int fac(int n) { if (n == 0) return 1; else return n * fac(n-1); } int main() { int num; cout "; cin >> num; cout << "fac(" << num << ")=" << fac(num) << endl; } Base case Recursive Case
8
Maze Example Neat example that uses recursion to traverse through a maze. Base case: Leave if row or column not in maze Leave if current spot is not the path Announce success if you reached the finish point Recursive case: mazeTraverse(north) mazeTraverse(west) mazeTraverse(south) mazeTraverse(east)
9
void mazeTraverse(char maze[12][12],int start_row,int start_col) { if(start_row>=0&&start_row =0&&start_col<12) { if(start_row==4&&start_col==11) cout<<"success"<<endl; if(maze[start_row][start_col]=='.') { maze[start_row][start_col]='x'; print_array(maze,12,12); mazeTraverse(maze,start_row-1,start_col); mazeTraverse(maze,start_row,start_col-1); mazeTraverse(maze,start_row+1,start_col); mazeTraverse(maze,start_row,start_col+1); maze[start_row][start_col]='*'; } Base case Recursive case
10
Fibonacci numbers Fibonacci numbers for n=0,1, 2, 3,... 0, 1, 1, 2, 3, 5, 8, 13, 21,... fib(n) = fib(n-2) + fib(n-1) Note: fib(0) = 0 fib(1) = 1
11
// File: fibRecursive.cpp #include using namespace std; long fib(int n); int main() { int num; cout "; cin >> num; cout << "FIB(" << num << ")=" << fib(num) << endl; } long fib(int n) { if ((n==0) || (n==1)) return n; else return (fib(n-2) + fib(n-1)); } Base case Recursive case
12
Recursion not always most efficient Recursive functions can always be converted to iterative solutions. Recursive solutions are usually more elegant but may have a higher cost because every function call pushes another instance of the function on the program stack. Calling a function is more expensive than iterating a loop fib(6) fib(5) fib(3) fib(4) fib(3) fib(2) fib(1) 1 fib(0)fib(1) 01 fib(0)fib(1) 01 fib(2)fib(1) 1 fib(0)fib(1) 01 fib(4) fib(3) fib(2) fib(1) 1 fib(0)fib(1) 01 fib(0)fib(1) 01
13
// File: fibIterative.cpp #include using namespace std; long fib(int n); int main() { int num; cout "; cin >> num; cout << "FIB(" << num << ")=" << fib(num) << endl; } long fib(int n) { if (n<0) return(-1); if (n<=1) return(n); int now=1,last=0,before; for (int i=2;i<=n;i++) { before=last; last=now; now=before+last; } return(now); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.