Recursion
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

Recursion PowerPoint PPT Presentation


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

Recursion. Gordon College CPS212. Adapted from Nyhoff: ADTs, Data Structures, and Problem Solving. Recursive Functions. Recursive programming is based off of Recursive Formulas Definition of Recursive Formula

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

Recursion

Gordon College

CPS212

Adapted from Nyhoff: ADTs, Data Structures, and Problem Solving


Recursive functions

Recursive Functions

  • Recursive programming is based off of Recursive Formulas

  • Definition of Recursive Formula

    • a formula that is used to determine the next term of a sequence using one or more of the preceding terms.

  • Example of Recursive Formula

    • The recursive formula for the sequence 5, 20, 80, 320, ... is an = 4 an-1

      How would you program such a thing?


Program solution

Program Solution

int series(int n)

{

if (n == 1) return 5;

return 4 * series(n-1);

}

Base case

Recursive case


Recursion1

Recursion

A function is defined recursively if it has the following two parts:

  • An anchor or base case

    • The function is defined for one or more specific values of the parameter(s)

  • An inductive or recursive case

    • The function's value for current parameter(s) is defined in terms of previously defined function values and/or parameter(s)


Recursive example

Recursive Example

  • Consider a recursive power functiondouble power (double x, unsigned n){ if ( n == 0 ) return 1.0; else return x * power (x, n-1); }

  • Which is the anchor?

  • Which is the inductive or recursive part?

  • How does the anchor keep it from going forever?


Recursive example1

Recursive Example

  • Note the results of a call

    • Recursivecalls

    • Resolutionof thecalls


Recursive factorial another example

Recursive Factorialanother example

  • n! = 1 x 2 x …x n, for n > 0

    n! = (n – 1)! X n

    5! = 5 x 4!120

    4! = 4 x 3!24

    3! = 3 x 2!6

    2! = 2 x 1!2

    1! = 11


Another example of recursion

Another Example of Recursion

  • Fibonacci numbers1, 1, 2, 3, 5, 8, 13, 21, 34f1 = 1, f2 = 1 … fn = fn -2 + fn -1

    • A recursive functiondouble Fib (unsigned n){ if (n <= 2) return 1; else return Fib (n – 1) + Fib (n – 2); }


A bad use of recursion

A Bad Use of Recursion

  • Why is this inefficient?

    • Note the recursion tree


Uses of recursion

Uses of Recursion

  • Binary Search

    • See source code

    • Note results of recursive call


Uses of recursion1

Uses of Recursion

  • Palindrome checker

    • A palindrome has same value with characters reversed1234321 racecar

      Recursive algorithm for an integer palindrome checker

      If numDigits <= 1 return true

      Else check first and last digits num/10numDigits-1and num % 10

      • if they do not match return false

        If they match, check more digitsApply algorithm recursively to:(num % 10numDigits-1)/10 and numDigits - 2


Recursion example towers of hanoi

Recursion Example: Towers of Hanoi

  • Think Recursive algorithm

  • Task

    • Move disks from left peg to right peg

    • When disk moved, must be placed on a peg

    • Only one disk (top disk on a peg) moved at a time

    • Larger disk may never be placed on a smaller disk


Recursion example towers of hanoi1

Recursion Example: Towers of Hanoi

  • Identify base case:If there is one disk move from A to C

  • Inductive solution for n > 1 disks

    • Move topmost n – 1 disks from A to B, using C for temporary storage

    • Move final disk remaining on A to C

    • Move the n – 1 disk from B to C using A for temporary storage


Towers of hanoi solution

Towers of Hanoi: Solution

voidhanoi(int n, conststring& initNeedle,

conststring& endNeedle, conststring& tempNeedle)

{

if (n == 1)

cout << "move " << initNeedle << " to "

<< endNeedle << endl;

else

{

hanoi(n-1,initNeedle,tempNeedle,endNeedle);

cout << "move " << initNeedle << " to "

<< endNeedle << endl;

hanoi(n-1,tempNeedle,endNeedle,initNeedle);

}

}


Towers of hanoi solution1

Towers of Hanoi: Solution

string beginneedle = "A", middleneedle = "B", endneedle = "C";

hanoi(3, beginneedle, endneedle, middleneedle);

The solution for n = 3

move A to C

move A to B

move C to B

move A to C

move B to A

move B to C

move A to C

output


Recursion example towers of hanoi2

Recursion Example: Towers of Hanoi

  • Note the graphical steps to the solution


Recursion example parsing

Recursion Example: Parsing

  • Examples so far are direct recursion

    • Function calls itself directly

  • Indirect recursion occurs when

    • A function calls other functions

    • Some chain of function calls eventually results in a call to original function again

  • An example of this is the problem of processing arithmetic expressions


Recursion example parsing1

Recursion Example: Parsing

  • Parser is part of the compiler

  • Input to a compiler is characters

    • Broken up into meaningful groups

    • Identifiers, reserved words, constants, operators

  • These units are called tokens

    • Recognized by lexical analyzer

    • Syntax rules applied


Recursion example parsing2

Recursion Example: Parsing

  • Parser generates a parse tree using the tokens according torules below:

  • An expression:term + term | term – term | term

  • A term:factor * factor | factor / factor | factor

  • A factor:( expression ) | letter | digit

Note the indirect recursion


Implementing recursion the runtime stack

Implementing Recursion:The Runtime Stack

  • Activation record created for each function call

  • Activation records placed on run-time stack

  • Recursive calls generate stack of similar activation records


Implementing recursion the runtime stack1

Implementing Recursion:The Runtime Stack

  • When base case reached and successive calls resolved

    • Activation records are popped off the stack


  • Login