aps105 recursion. a function is allowed to call itself! –example:. a function calling itself is...

Post on 23-Dec-2015

240 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

APS105

Recursion

Recursion

• A function is allowed to call itself!– Example:

.

• A function calling itself is called “recursion”• Such a function is called a “recursive” function

– Why would you want to do this?

Recursion: Motivation

• Sometimes a prob has self-similar sub-probs– E.g., problems-within-a-problem– “inner” problems have same description as outer

• but inner problems are smaller in some way

– inner problems themselves have inner problems

• Recursion continues until:– some small indivisible problem reached– or some other stopping condition is reached– called the “base case” or “terminating case”

• Note:– sometimes recursion can be expressed as a loop– or vice-versa

Recursion: Walking Across the Room

• Loop-based solution basic idea:– while not at the wall take another step

• Recursive solution basic idea:.

Recursion: Cake Cutting

• Cut a jelly-roll into equal parts < 100g each

.

Reading/Evaluating Expressions

• Recall: BEDMAS: order of evaluation– Brackets– Exponents– Division & Multiplication– Addition & Subtraction

• Apply BEDMAS to every expression– and to every sub-expression within the expression

Example: Evaluating Expressions

• BEDMAS• Example:

.

Other Examples

• Finding your way out of a maze• Fractals• Solving a sudoku

Recursive Math Functions

• Example: f(3):.

Recursive Math Functions

.

Implementing Recursion in C

.

Factorial Using a Loop

.

Factorial Using Recursion

.

Factorial Using Recursion (again)

Printing Patterns with Recursion

.

Print a Row of n Stars (recursively)

.Print a Row of n Stars (attempt2)

**********

.

Print a Triangle of Stars

**********

.

Print an Inverted Triangle of Stars

.

What Will This Print?

Recursion and Strings

Recursion and Strings

• Can think of strings using a recursive definition– Example: one of these recursive definitions:– a string is a character followed by a string– a string is a character preceded by a string– a string is two characters with a string in the middle

Palindromes

• Palindrome: – a string that is the same backwards and forwards– Examples: racecar, level, civic, madam, noon

• Write a function to determine if a palindrome

.Function to test Palindrome

.

Palindrome tester with a Helper

Greatest Common Divisor (GCD)

GCD Algorithm

• GCD of two numbers is – if the numbers are the same:

• the GCD is either number

– if the numbers are different: • the GCD of the smaller of the two and the

difference between the two

• Example: GCD(20,8)= GCD(8,12)

= GCD(8,4)

= GCD(4,4)

The GCD is 4

Formalized GCD Alg., and Code

.

.

Determining Powers Efficiently

Determining Powers

• Computing powers the easy (but slow) way:X5 = X*X*X*X*X

X20 = X*X*X*X*X*X*X*X*X*X....

• The more efficient way:.

Formula for Determining Powers

• .

.

Recursively Determining Power

Ackermann’s Function and

Maze Routing

Ackermann’s Function

• .

Maze Routing

• Basic idea (see Ch8)

.

Towers of Hanoi

Towers of Hanoi

• Move all disks to another rod, but:– Only one disk may be moved at a time.– No disk may be placed on a smaller disk.

Initial: Goal:

Towers of Hanoi: Outer Problem

=

Towers of Hanoi: 1st Inner Problem

=

Towers of Hanoi: Ex. base problem

=

Towers of Hanoi

• Write a program that prints a solution– assume rods 1,2,3– assume some number of discs given by height

.Towers of Hanoi

Towers Algorithm

• Legend:– Monks found 64 disk tower-game– Universe will end when they finish the game

• number of moves is 2n-1– for n=3: 7 moves– for n=20: 1,048,575 moves– for n=64: 1.8*1019 moves

• == 585billion years at one move per second• note: 14billion years since big bang

• The algorithm is “exponential”– roughly Xn moves where n is size of problem– exponential algorithms are impractical!

top related