recursion
DESCRIPTION
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!. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/1.jpg)
Recursion
![Page 2: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/2.jpg)
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
![Page 3: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/3.jpg)
Factorial problem can be broken down into smaller problems.
Note: 0! = 1
10!
10 9!
8 7!
9 8!
3 2!
5 4!
4 3!
7 6!
2 1!
1 0!
*
*
*
*
*
*
*
*
*
![Page 4: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/4.jpg)
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
![Page 5: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/5.jpg)
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! = 6fac(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
![Page 6: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/6.jpg)
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
![Page 7: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/7.jpg)
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;}
Basecase
RecursiveCase
![Page 8: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/8.jpg)
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)
![Page 9: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/9.jpg)
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]='*'; } }}
Basecase
Recursivecase
![Page 10: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/10.jpg)
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
![Page 11: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/11.jpg)
// 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));}
Basecase
Recursivecase
![Page 12: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/12.jpg)
Recursion not always most efficientRecursive functions can always be converted toiterative 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(2)fib(1)
1 fib(0) fib(1)
0 1
fib(0) fib(1)
0 1
fib(2)fib(1)
1 fib(0) fib(1)
0 1
fib(4)
fib(3)fib(2)
fib(2)fib(1)
1 fib(0) fib(1)
0 1
fib(0) fib(1)
0 1
![Page 13: Recursion](https://reader035.vdocuments.net/reader035/viewer/2022072013/56812a93550346895d8e4560/html5/thumbnails/13.jpg)
// 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);}