43
APS105 Recursion

APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Embed Size (px)

Citation preview

Page 1: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

APS105

Recursion

Page 2: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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?

Page 3: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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

Page 4: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Recursion: Walking Across the Room

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

• Recursive solution basic idea:.

Page 5: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Recursion: Cake Cutting

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

.

Page 6: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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

Page 7: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Example: Evaluating Expressions

• BEDMAS• Example:

.

Page 8: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Other Examples

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

Page 9: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Recursive Math Functions

Page 10: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

• Example: f(3):.

Recursive Math Functions

Page 11: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Implementing Recursion in C

Page 12: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Factorial Using a Loop

Page 13: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Factorial Using Recursion

Page 14: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Factorial Using Recursion (again)

Page 15: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Printing Patterns with Recursion

Page 16: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Print a Row of n Stars (recursively)

Page 17: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.Print a Row of n Stars (attempt2)

Page 18: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

**********

.

Print a Triangle of Stars

Page 19: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

**********

.

Print an Inverted Triangle of Stars

Page 20: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

What Will This Print?

Page 21: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Recursion and Strings

Page 22: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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

Page 23: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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

Page 24: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.Function to test Palindrome

Page 25: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Palindrome tester with a Helper

Page 26: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Greatest Common Divisor (GCD)

Page 27: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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

Page 28: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Formalized GCD Alg., and Code

.

.

Page 29: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Determining Powers Efficiently

Page 30: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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

Page 31: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Formula for Determining Powers

• .

Page 32: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.

Recursively Determining Power

Page 33: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Ackermann’s Function and

Maze Routing

Page 34: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Ackermann’s Function

• .

Page 35: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Maze Routing

• Basic idea (see Ch8)

.

Page 36: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Towers of Hanoi

Page 37: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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:

Page 38: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Towers of Hanoi: Outer Problem

=

Page 39: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Towers of Hanoi: 1st Inner Problem

=

Page 40: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Towers of Hanoi: Ex. base problem

=

Page 41: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

Towers of Hanoi

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

Page 42: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

.Towers of Hanoi

Page 43: APS105 Recursion. A function is allowed to call itself! –Example:. A function calling itself is called “recursion” Such a function is called a “recursive”

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!