Recursion - Recursion In C/C a function may call itself...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Recursion In C/C++, a function may call itself either directly or indirectly. When a function call itself recursively, each invocation gets a fresh set of all explicit parameters and automatic (local) variables, independent of the previous set. Example: Compute the greatest common divisor (GCD) of two numbers. Let M > N > 0, and M = QN + R such that 0 <= R < N if R = 0, then M = QN, and gcd(M, N) = N if R > 0, then N > R, and gcd(M, N) = gcd(N, R) int gcd(int M, int N) /* given M > 0 and N > 0, compute gcd of M & N */ { int R; R = M % N; if (R == 0) //base case return N; else return gcd(N, R); //recursion } An equivalent non-recursive implementation. int gcd(int M, int N) /* given M > 0 and N > 0, compute gcd of M & N */ { int R; while (R = M % N) //R = M % N and R != 0 { M = N; N = R; } return N; } Usually, looping (iteration) is more efficient than recursion. Recursion is preferred when the problem is recursively defined, or when the data structure that the algorithm operates on is recursively defined .
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Two fundamental rules of recursion: 1. Base cases You must have some base cases, which can be solved without recursion. 2. Making progress (recursion) For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case . Example: Fibonacci number f(0) = 0; f(1) = 1; f(n) = f(n 2) + f(n 1) for n 2; long long fib(long long n) { // precondition: given n >= 0 if (n == 0) // base case #1 return 0; if (n == 1) // base case #2 return 1; return fib(n-2) + fib(n-1); // recursion } // this program works but is unacceptably slow for n > 30 Computing the Fibonacci number using iteration is much more efficient.
Image of page 2
3 Example: Ackermann’s function otherwise 1 1 0 ) 1 , 1 ( 0 1 ) , ( )) , A(M, N A(M N if M A M if N N M A long long Ack(long long m, long long n) // precondition: given m >= 0 and n >= 0 { if (m == 0) return n+1; if (n == 0) return Ack(m-1, 1); return Ack(m-1, Ack(m, n-1)); }
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Example: Recursive binary search
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern