Recursion l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

Recursion PowerPoint PPT Presentation


  • 145 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Recursion l.jpg

Recursion

Chapter 11


Chapter contents l.jpg

Chapter Contents

  • Recursive Definitions

  • Base and General Cases of Recursion

  • What is a Recursive Algorithm

  • Recursive Functions

  • Using Recursive Functions


Recursive definitions l.jpg

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


Recursive algorithm l.jpg

Base Case

General Case

Recursive Algorithm

  • The factorial of a number could be defined recursively


Base and general cases of recursion l.jpg

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


Recursive functions l.jpg

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);}


Recursive functions7 l.jpg

Recursive Functions

  • Execution ofa call to therecursive factorial functioncout << fact(4);


Recursive functions8 l.jpg

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.


Recursive functions9 l.jpg

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.


Using recursive functions l.jpg

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


Using recursive functions11 l.jpg

Using Recursive Functions

  • Execution ofthe recursivefunctionlargest()


Using recursive functions12 l.jpg

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:


Using recursive functions13 l.jpg

Using Recursive Functions

  • Note Fibonacci source code

  • Note diagram ofrecursiveFibonacci function call

    • Very inefficient


Recursion or iteration l.jpg

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?


Recursion or iteration15 l.jpg

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.


Recursion or iteration16 l.jpg

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.


Recursion or iteration17 l.jpg

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


  • Login