130 likes | 252 Views
Recursion. 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. Factorial problem can be broken down into smaller problems. Note: 0! = 1. 10!.
E N D
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
Factorial problem can be broken down into smaller problems. Note: 0! = 1 10! 10 9! 1 2 4 5 7 8 3 9 6! 8! 4! 3! 7! 1! 0! 2! * * * * * * * * *
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
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(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 fac(n) = n * fac(n-1) fac(0) = 1
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
Base Case and Recursive Case • Base case: fac(0) = 1 • Recursive Case: fac(n) = n * fac(n-1) #include <iostream> using namespace std; int fac(int n) { if (n == 0) return 1; else return n * fac(n-1); } int main() { int num; cout << "Find factorial of > "; cin >> num; cout << "fac(" << num << ")=" << fac(num) << endl; } Base case Recursive Case
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)
void mazeTraverse(char maze[12][12],int start_row,int start_col) { if(start_row>=0&&start_row<12&&start_col>=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
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
// File: fibRecursive.cpp #include <iostream> using namespace std; long fib(int n); int main() { int num; cout << "Find fibonacci of >"; 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
Recursion not always most efficient • 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) Recursive functions can always be converted to iterative solutions. fib(5) fib(3) fib(1) fib(2) fib(4) fib(0) fib(1) fib(4) 1 fib(2) fib(3) 0 1 fib(2) fib(3) fib(0) fib(1) fib(1) fib(2) fib(0) fib(1) fib(1) fib(2) 0 1 1 fib(0) fib(1) 0 1 1 fib(0) fib(1) 0 1 0 1
// File: fibIterative.cpp #include <iostream> using namespace std; long fib(int n); int main() { int num; cout << "Find fibonacci of >"; 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); }