- 128 Views
- Uploaded on
- Presentation posted in: General

Recursion

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Recursion

Gordon College

CPS212

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

- 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?

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

int series(int n)

{

if (n == 1) return 5;

return 4 * series(n-1);

}

Base case

Recursive case

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)

- 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?

- Note the results of a call
- Recursivecalls
- Resolutionof thecalls

- 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

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

- Why is this inefficient?
- Note the recursion tree

- Binary Search
- See source code
- Note results of recursive call

- 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

- if they do not match return false

- A palindrome has same value with characters reversed1234321 racecar

- 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

- 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

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

}

}

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

- Note the graphical steps to the solution

- 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

- 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

- 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

- Activation record created for each function call
- Activation records placed on run-time stack
- Recursive calls generate stack of similar activation records

- When base case reached and successive calls resolved
- Activation records are popped off the stack