1 / 18

Recursion

This chapter discusses recursive definitions, base and general cases of recursion, recursive algorithms, and the use of recursive functions. Examples of factorial and Fibonacci functions are provided.

jstehle
Download Presentation

Recursion

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. Recursion Chapter 11

  2. Chapter Contents • Recursive Definitions • Base and General Cases of Recursion • What is a Recursive Algorithm • Recursive Functions • Using Recursive Functions

  3. Recursive Definitions • Definition:The process of solving a problem by reducing it to smaller versions of itself is called recursion. • Example:Consider the concept of factorials 0! = 1 1! = 1 2! = 1 * 2 3! = 1 * 2 * 3 . . . n! = 1 * 2 * 3 * 4 * … * (n – 1) * n

  4. Base Case General Case Recursive Algorithm • The factorial of a number could be defined recursively

  5. Base Case General Case Base and General Cases of Recursion • Every recursive definition must have one (or more) base cases. • The general case must eventually reduce to a base case. • The base case stops the recursion

  6. Recursive Functions • This would be the recursive function for the recursive factorial algorithm int fact (int num) { if (num == 0) return 1; else return num * fact (num – 1);}

  7. Recursive Functions • Execution ofa call to therecursive factorial functioncout << fact(4);

  8. Recursive Functions • Think of a recursive function as having infinitely many copies of itself. • Every call to a recursive function has • its own code and • its own set of parameters and • its own local variables. • After completing a particular recursive call, • Control goes back to the calling environment • That is, the previous call.

  9. Recursive Functions • The current (recursive) call must execute completely before the control goes back to the previous call. • The execution in the previous call begins from the point immediately following the recursive call. • A recursive function in which the last statement executed is the recursive call is called a tail recursive function. • The function fact is an example of a tail recursive function.

  10. Using Recursive Functions • Consider the task of finding the largest element of an array • To find the largest element in list[a]...list[b] • First find the largest element in list[a+1]...list[b] • Then compare this largest element with list[a]. • Note the source code

  11. Using Recursive Functions • Execution ofthe recursivefunctionlargest()

  12. Using Recursive Functions • Example of Fibonacci number • a denotes the first Fibonacci number • b is the second Fibonacci number • n is the nth Fibonacci number:

  13. Using Recursive Functions • Note Fibonacci source code • Note diagram ofrecursiveFibonacci function call • Very inefficient

  14. Recursion or Iteration? • Iterative control structures use a looping structure to repeat a set of statements. • while, • for, • or do...while, • There are usually two ways to solve a particular problem • iteration and • recursion. • The obvious question … Which method is better?

  15. Recursion or Iteration? • Another key factor in determining the best solution method is efficiency. • Recall that whenever a function is called, • Memory space for its formal parameters and local variables is allocated. • When the function terminates, that memory space is then deallocated. • We also know that every (recursive) call has its own set of parameters and (automatic) local variables.

  16. Recursion or Iteration? • Overhead involved with recursion • Memory space • Time of execution • Today’s computers • fast • inexpensive memory • Therefore, the execution of a recursion function is not noticeable.

  17. Recursion or Iteration? • Rule of thumb:If an iterative solution is • more obvious • easier to understand than a recursive solution Use the iterative solution, which would be more efficient. • When the recursive solution is more obvious or easier to construct … use recursion Fibonacci numbers Tower of Hanoi

  18. A Recursive Polynomial • The function below is an approximation for ex • Can be defined recursively

More Related