recursion. what is recursion? solving a problem in terms of itself or repeating objects in a...

81
Recursion

Upload: philippa-glenn

Post on 29-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Recursion

Page 2: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

What is recursion?

• Solving a problem in terms of itself or repeating objects in a “self-similar” way

• A recursive function is a function that calls itself!• Another way to repeat code• Takes advantage of the “activation stack”

Page 3: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that
Page 4: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

You just moved in to a new house on a dead end street…

In celebration you shout: “Woo hoo, my new house is awesome and the house number is 1282!”

Page 5: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

You just moved in to a new house on a dead end street…

• But you don’t know the house number!

• You do know that your house number is 2 plus the address of your neighbor to the left

• So you decide to call your neighbor…

Page 6: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Call your neighbor and ask them for their house number. Add two to get your house number.

You

Page 7: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Call your neighbor and ask them for their house number.

Howdy neighbor. What is your

house number?

Neighbor 1 You

Page 8: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Wait for the neighbor to give you an answer.

42.

Neighbor 1 You

Page 9: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Kindly thank them and hang up.

Sweet, thanks! <Hang up>

YouNeighbor 1

Page 10: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Add two to your neighbor’s address to get your house number.

So my address is 42 + 2 =

44!

Page 11: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

What if your neighbor doesn’t know their address?

Page 12: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Call your neighbor and ask for their house number.

Howdy neighbor. What is your

house number?

Neighbor 1 You

Page 13: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

But they don’t know it!

Hm, that’s a good question. Hold

please!

Neighbor 1 You

Page 14: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 1 puts you on hold and calls their neighbor.

<holding…>

Neighbor 1Neighbor 2 You

Page 15: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 1 puts you on hold and calls their neighbor.

Howdy neighbor. What is your

house number?

<holding…>

Neighbor 1Neighbor 2 You

Page 16: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

But their neighbor doesn’t know their house number either! (Seriously, who are these people?!)

Hm, that’s a good question. Hold

please!

<holding…>

Neighbor 1Neighbor 2 You

Page 17: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 2 puts neighbor 1 on hold and calls neighbor 3.

<holding…><holding…>

Howdy neighbor. What is your

house number?

Neighbor 1Neighbor 2 YouNeighbor 3

Page 18: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 3 tells neighbor 2 their house number (phew!)

<holding…><holding…>

22.

Neighbor 1Neighbor 2 YouNeighbor 3

Page 19: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 2 thanks neighbor 3 and hangs up.

<holding…><holding…>

Sweet, thanks! <Hang up>

Neighbor 1Neighbor 2 YouNeighbor 3

Page 20: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 2 adds two to get address.

<holding…><holding…>

So my address is 22 + 2 =

24!

Neighbor 1Neighbor 2 You

Page 21: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 2 takes neighbor 3 off of hold and tells them their address.

<holding…>

24.

Neighbor 1Neighbor 2 You

Page 22: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Neighbor 1 thanks neighbor 2 and hangs up.

<holding…>

Sweet, thanks! <Hang up>

Neighbor 1Neighbor 2 You

Page 23: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Your neighbor adds two to get their address.

<holding…>

So my address is 24 + 2 =

26!

Neighbor 1

Page 24: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Finally your neighbor takes you off hold and tells you their address (what took them so long?!)

Sweet, thanks! <Hang up>

Neighbor 1

Page 25: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

You add two to your neighbors address to get your house number!

So my address is 26 + 2 =

28!

Page 26: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Let’s analyze what just happened

• How many phone calls were made?• When did the calling chain finally stop?• From “your” perspective, what happened

when you called your neighbor?

Page 27: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Algorithm in writing

• If you already know your house number, say it loud!

• If you don’t know your house number…– Call neighbor to your left and ask for their house

number– When you get the number, add two to it, then say

it loud!

Page 28: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

But…

• What if you don’t have any neighbors?

Page 29: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Algorithm in writing

• If you already know your house number, say it loud

• If you don’t have a neighbor to the left and don’t know your house number, say 0

• If you don’t know your house number…– Call neighbor to your left and ask for their house

number– When you get the number, add two to it, then say

it loud!

Page 30: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You

Page 31: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

Page 32: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<answers call, decides what to do>

Page 33: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

Page 34: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<answers call, decides what to do>

Page 35: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<calling neighbor, waiting for answer>

Page 36: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<calling neighbor, waiting for answer>

N3<answers call, decides what to do>

Page 37: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<calling neighbor, waiting for answer>

N3<knows answer! Tells N2 and hangs up>

Page 38: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<calling neighbor, waiting for answer>

N3<knows answer! Tells N2 and hangs up>

22

Page 39: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<adds two to neighbor’s answer>

22 + 2 = 24

Page 40: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<calling neighbor, waiting for answer>

N2<knows answer! Tells N1 and hangs up>

22 + 2 = 24

24

Page 41: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<adds two to neighbor’s answer>

24 + 2 = 26

Page 42: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <calling neighbor, waiting for answer>

N1<knows answer! Tells you and hangs up>

24 + 2 = 26

26

Page 43: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

- If you already know your house number, say it loud

- If you don’t have a neighbor to the left and don’t know your house number, say 0

- If you don’t know your house number…

-Call neighbor to your left and ask for their house number

-When you get the number, add two to it, then say it loud!

22 You <add two to neighbor’s answer>

26 + 2 = 28

Page 44: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

22 You <done! Your address is 28>

Page 45: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

But…

• What if no-one on your street knows their house number?

Page 46: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

But…

• What if no-one on your street knows their house number?– Think about it….don’t we cover this case by adding

a check to see if you have a neighbor to the left and returning 0 if you don’t?

Page 47: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Three “Pillars” of Recursion

• A recursive function…– Calls clone of itself– Has terminating condition(s)– Moves toward terminating condition(s)

Page 48: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

House number problem components

• Calling clone of itself?• Terminating condition(s)?• Moving toward the terminating conditions?

• If you already know your house number, say it loud• If you don’t have a neighbor to the left and don’t know

your house number, say 0• If you don’t know your house number…

– Call neighbor to your left and ask for their house number

– When you get the number, add two to it, then say it loud!

Page 49: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

House number problem components

• Calling clone of itself?• Terminating condition(s)?• Moving toward the terminating conditions?

• If you already know your house number, say it loud• If you don’t have a neighbor to the left and don’t know

your house number, say 0• If you don’t know your house number…

– Call neighbor to your left and ask for their house number

– When you get the number, add two to it, then say it loud!

Terminating conditions

Call clone andmove towardterm cond

Page 50: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Another problem: Factorial

• How do you calculate 5!5*4*3*2*1

Page 51: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Another problem: Factorial

• How do you calculate 5!5! = 5*4! 4! = 4*3!3! = 3*2! 2! = 2*1! 1! = 1 (what about 0!)

Page 52: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

In general…

• n! = n * (n-1)!• If we had a factorial function instead of !, it

would be fact(n) = n * fact(n-1)

Page 53: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Factorial Recursive Algorithm

• What are are terminating (aka base) conditions? What is the simplest factorial to calculate?

• What inputs does the function need? How do we move toward the terminating condition?

Page 54: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Factorial Recursive Algorithm

• If number is 0 or 1– Answer is 1

• Else– Answer is number * factorial of number-1

Page 55: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Call fact(3) from command window…

>> myans = fact(3)

Page 56: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

>> myans = fact(3)

Page 57: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3)

Page 58: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3) fact(2)if 2 == 1 | 2==0

ans = 1else

ans = 2 * fact(2-1)

Page 59: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3) fact(2)if 2 == 1 | 2==0

ans = 1else

ans = 2 * fact(2-1)

Page 60: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3) fact(2)if 2 == 1 | 2==0

ans = 1else

ans = 2 * fact(2-1)

fact(1)if 1 == 1 | 1==0

ans = 1else

ans = 1 * fact(1-1)

Page 61: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3) fact(2)if 2 == 1 | 2==0

ans = 1else

ans = 2 * fact(2-1)

fact(1)if 1 == 1 | 1==0

ans = 1else

ans = 1 * fact(1-1)1

Page 62: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3) fact(2)if 2 == 1 | 2==0

ans = 1else

ans = 2 * fact(2-1)

1

Page 63: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3) fact(2)if 2 == 1 | 2==0

ans = 1else

ans = 2 * fact(2-1)

12

Page 64: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3)

2

Page 65: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

fact(3)if 3 == 1 | 3==0

ans = 1else

ans = 3 * fact(3-1)

Cal fact(3) from command window…

>> myans = fact(3)

26

Page 66: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Cal fact(3) from command window…

>> myans = fact(3)6

Page 67: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Cal fact(3) from command window…

>> myans = fact(3)myans =

6>>

6

Page 68: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Answer is returned to place that called it, which in this case was the command window!

Page 69: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

What about bad inputs?

• What happens if input is -1?• If input is -5?

Page 70: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

What about bad inputs?

• What happens if input is -1?• If input is -5?• We theoretically recurse forever! (though our

activation stack keeping track of all the temporary workspaces will eventually run out of memory and we will get an error)

Page 71: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Wrapper Functions• Use a “wrapper function” to preprocess your data!• A wrapper function checks input(s), and if they are

value, makes the first call to the recursive function• Captures result from recursive function and then

returns it back to where called the wrapper function originally

• Note, the wrapper function should usually only run once per call (unlike the recursive helper function)

Page 72: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Why recursion?

• In general, any recursive function could be written as a loop (likely a while loop)

• Loops are usually faster (because Matlab doesn’t have to make all those temporary workspaces and copy the parameter values in)

• But some problems are solved more simply using recursion– Printing out structures (e.g. nested structures)– Listing out directories and files– Crawling the web

Page 73: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Recursively List Contents of Directory

Page 74: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Recursively List Contents of Directory

• Given a directory, we want to list all items in that folder, including items in

• Don’t even worry about matlab yet…let’s think about the steps involved in general

• Given a directory (you, yourself), what would you do to start listing out the filenames?

• What would you do when you encountered another folder?

Page 75: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Recursively Listing Directories

• Open folder to see list of items• For each item listed in folder– Check to see if it is a directory• If it is a directory, we need to search it (start whole

process over)• Else, if not a directory (meaning it’s just a file name),

print it out to command window and continue on

Page 76: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Translating to Matlabfunction mydir(dirname)% recursive dir traversal lst = dir(dirname); % returns a structure array for pos = 3:length(lst) %skip over first two items, as they are always '.' and '..' if lst(pos).isdir % check if current is a directory mydir([dirname '/' lst(pos).name]) else fprintf('%s\n', lst(pos).name); % print out file name end endend

Page 77: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

When you shouldn’t use recursion

• Consider Fibonacci numbers

• fib(n) = fib(n-1) + fib(n-2)• fib(0) = 0• fib(1) = 1

Page 78: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Redundant Calculations

• To compute fib(n), we recursively compute fib(n-1). When that recursive call returns, we compute fib(n-2) using another recursive call– We have already computed fib(n-2) in the process

of computing fib(n-1)– We make two calls to fib(n-2)

Page 79: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Redundant Calculations

• Making two method calls would double the running time

• Compounding effect: each recursive call does more and more redundant work– Each call to fib(n-1) and each call to fib(n-2) makes

a call to fib(n-3); there are 3 calls to fib(n-3)– Each call to fib(n-2) or fib(n-3) results in a call to

fib(n-4), so 5 calls to fib(n-4)

Page 80: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that

Redundant Calculations III

• The recursive routine fib is exponential– Meaning the amount of work done grows

exponentially based on the input value n

Page 81: Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that