1 / 23

CSCI 171

CSCI 171. Presentation 7 Recursion. What is recursion?. “Recursion ... is a technique for defining a problem in terms of one or more smaller versions of the same problem. The solution to the problem is built on the result(s) from the smaller version(s).” . What is recursion?.

chapa
Download Presentation

CSCI 171

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 171 Presentation 7 Recursion

  2. What is recursion? • “Recursion ... is a technique for defining a problem in terms of one or more smaller versions of the same problem. The solution to the problem is built on the result(s) from the smaller version(s).”

  3. What is recursion? • A recursive function is a function that calls itself • Characteristics of a recursive function: • “The problem can be redefined in terms of one or more sub problems, identical in nature to the original problem, but in some sense smaller in size.” • “One or more base cases of the problem have a direct or known solution.” • prevents “Recursion infinity”

  4. 4 Questions • What instance(s) of the problem can serve as the base case(s)? • How can you define the problem in terms of one or more smaller problems of the same type? • As the problem size diminishes will you reach this base case(s)? • How is the solution(s) from the smaller problem(s) used to build a correct solution to the current larger problem?

  5. Fibonacci Sequence • The Fibonacci sequence is defined as follows: • For n = 1 or 2: • Fib(1) = 1 • Fib(2) = 1 • For n > 2: • Fib(n) = Fib(n-1) + Fib(n-2) • The sequence looks like: • 1, 1, 2, 3, 5, 8, 13, 21…

  6. 4 Questions for theFibonacci Sequence • Question 1 • What instance(s) serve as the base case? • 1 and 2 • Fib(1) = 1 • Fib(2) = 1

  7. 4 Questions for theFibonacci Sequence • Question 2 • How can you define the problem in terms of one or more smaller problems of the same type? • Fib(n) = Fib(n-1) + Fib(n-2)

  8. 4 Questions for theFibonacci Sequence • Question 3 • As the problem size diminishes will you reach this base case(s)? • Yes, each time you are decrementing, so you will eventually reach the base case of 1 or 2

  9. 4 Questions for theFibonacci Sequence • Question 4 • How is the solution(s) from the smaller problem(s) used to build a correct solution to the current larger problem? • They are all added together

  10. Solution for Fibonaccimain() function • #include <stdio.h> • #include <math.h> • double Fibonacci(int); • int main( void ) { • intidx = 1; • for (; idx < 40; idx++) { • printf("\nFibonacci(%d) = %.0lf", idx, Fibonacci(idx)); • (idx < 36) ? printf("\t\t") : printf("\t"); • } • return 0; • }

  11. Non-recursive Solution Fibonacci() function double Fibonacci (int n) { double sr5 = 0.0; sr5 = sqrt(5); return (1 / sr5 * pow(((1+sr5)/2), n)) - (1 / sr5 * pow(((1-sr5)/2), n)); }

  12. Recursive SolutionFibonacci() function double Fibonacci(int z) { if ((z == 1) || (z == 2)) return 1; else return (Fibonacci(z-1) + Fibonacci(z-2)); }

  13. Advantages / Uses of Recursion • Sometimes easier to write • Critical for complex computer science algorithms • Used in Artificial Intelligence (AI) • Lisp, Prologue • Binary searches (arrays, trees, etc.)

  14. Disadvantages of Recursion • Sometimes harder to write • Code is generally inefficient • Some languages do not support recursion • COBOL, FORTRAN

  15. Sample Program 9.1.1 • #include <stdio.h> • #include <math.h> • double Fibonacci(int); • int counter = 0; • int main( void ) { • intidx = 1; • for (; idx < 40; idx++) { • printf("\nFibonacci(%d) = %.0lf", idx, Fibonacci(idx)); • (idx < 36) ? printf("\t\t") : printf("\t"); • printf("The function was called %d time(s).", counter); • counter = 0; • } • return 0; • }

  16. Sample Program 9.1.2 double Fibonacci(int z) { counter ++; if ((z == 1) || (z == 2)) return 1; else return (Fibonacci(z-1) + Fibonacci(z-2)); }

  17. Sample Program 9.1.3 double Fibonacci (int n) { double sr5 = 0.0; counter++; sr5 = sqrt(5); return (1 / sr5 * pow(((1+sr5)/2), n)) - (1 / sr5 * pow(((1-sr5)/2), n)); }

  18. Solving recurrence relations • Recurrence relation – recursively defined sequence for which we want to find an equivalent explicitly defined sequence • Techniques • Many techniques • We will discuss linear homogeneity

  19. Solving recurrence relations • Linear Homogeneity • A recurrence relation is linearly homogenous of degree k if: • an = r1an-1 + r2an-2 + … + rkan-k • ri is constant i • A recurrence relation that is linearly homogenous of degree k has the following characteristic equation: • xk = r1xk-1 + r2xk-2 + … + rk • The characteristic equation plays a role in determining the explicit formula

  20. Solving recurrence relations • Theorem to find an explicit formula which generates the same sequence as a recursive formula which is linearly homogenous of degree 2 • If the characteristic equation x2 – r1x – r2 = 0 of the recurrence relation an = r1an-1 + r2an-2 has 2 distinct roots s1 and s2, then an = us1n + vs2n (where u and v depend on initial conditions) is the explicit formula for the sequence. • If the characteristic equation x2 – r1x – r2 = 0 of the recurrence relation an = r1an-1 + r2an-2 has a single root s, then an = usn + vnsn (where u and v depend on initial conditions) is the explicit formula for the sequence.

  21. Solving recurrence relations • Example Solve the following recurrence relation

  22. Sample Program 9.2 • Run sample program 9.2 to determine the first 10 elements in the sequence it is generating • Use the solution to the recurrence relation to rewrite the code using an explicit function • Run the rewritten code to determine the first 10 elements (they should be the same as the first time the code was run)

  23. Recursion • “Typically, solutions to problems use either iteration (looping), or recursion. In general a recursive solution is less efficient than an iterative one in terms of computer time. However in many instances, recursion provides a very natural, simple solution to a problem of great complexity. “

More Related