Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here...

106
Module 6: Recursion Copyright Ron K. Cytron 2013 Ron K. Cytron * Prepared for 2u Semester Online Department of Computer Science and Engineering Washington University in Saint Louis Thanks to Alan Waldman for comments that improved these slides *

Transcript of Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here...

Page 1: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Module 6: Recursion

Copyright Ron K. Cytron 2013

Ron K. Cytron *

Prepared for 2u

Semester Online

Department of Computer Science and Engineering Washington University in Saint Louis

Thanks to Alan Waldman for comments that improved these slides

*

Page 2: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.0 Introduction

•  The material we study here involves no new syntax

•  Instead, we will be analyzing and writing methods, such as we studied in Module 5

•  We have seen methods calling other methods –  Compute the distance between two points

•  We need the sqrt method –  Multiplication (pedagogical example)

• mpy(c,d) calls add(a,b)!•  Now we examine methods that call themselves

–  Seems impossible to solve problem P using P –  But it works under the right circumstances –  And is a very powerful technique

2

Monolog 0

Page 3: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.0 Introduction

•  Why study recursion? Could we live without knowing recursion? –  Yes: with arrays, iteration, and choice, we know

enough to solve any problem that can be solved on a computer. •  Mechanisms that allow us to write "any program" are

called Turing-complete –  However, recursion is a powerful technique for

specifying computation •  Mathematically satisfying •  And some computations are more naturally specified

using recursion

3

Monolog 0

Page 4: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.0 Introduction

•  Nature offers many examples of recursion –  A tree grows its branches by applying the same

pattern "in the small" as it does "in the large –  Shapes manipulated recursively yield interesting

results

4

http://forums.artistserver.com/messages.cfm?threadid=7D6AF4F8-1143-DBB3-C62D3B1756BD9457 From Sedgewick's book

Page 5: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.0 Introduction

•  Nature offers many examples of recursion •  But recursion sometimes seems to do the

impossible

5 End of Monologue

Reproductive Parts M. C. Escher, 1948

http://farm1.static.flickr.com/151/421498787_4c7f6d99f4.jpg

Page 6: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = n * factorial(n-1), if n > 0 –  factorial(n) = 1, otherwise

6.1 Explicit recursion

6

Oyster 1

Page 7: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = n * factorial(n-1), if n > 0 –  factorial(n) = 1, otherwise

•  This works because a larger problem –  factorial(n)

6.1 Explicit recursion

7

Page 8: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = n * factorial(n-1), if n > 0 –  factorial(n) = 1, otherwise

•  This works because a larger problem –  factorial(n)

•  is phrased in terms of a smaller problem –  factorial(n-1)

6.1 Explicit recursion

8

Page 9: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = n * factorial(n-1), if n > 0 –  factorial(n) = 1, otherwise

•  This works because a larger problem –  factorial(n)

•  is phrased in terms of a smaller problem –  factorial(n-1)

•  and because the computation bottoms out with a base case –  factorial(0) = 1

6.1 Explicit recursion

9

Page 10: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = n * factorial(n-1), if n > 0 –  factorial(n) = 1, otherwise

•  This is the standard way such formulae are defined in math

6.1 Explicit recursion

10

Page 11: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = 1, if n <= 0 –  factorial(n) = n * factorial(n-1), otherwise

•  Rewrote the definition to put the base case first –  Common in computer science

6.1 Explicit recursion

11

Page 12: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Some formula are explicitly recursive –  factorial(n) = 1, if n <= 0 –  factorial(n) = n * factorial(n-1), otherwise

•  These are simply transcribed into recursive functions

public static int factorial(int n) {!!if (n <= 0)!! !return 1;!!else!! !return n * factorial(n-1);!}!

6.1 Explicit recursion

12

Page 13: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  This looks like ordinary code using methods •  The only difference is that the method calls

itself •  Let's look at the structure of a recursive method

in more detail public static int factorial(int n) {!!if (n <= 0)!! !return 1;!!else!! !return n * factorial(n-1);!}!

6.1 Explicit recursion

13

Page 14: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

public static int factorial(int n) {!!if (n <= 0)!! !return 1;!!else!! !return n * factorial(n-1);!}!

6.1 Explicit recursion

14

recursive call

Page 15: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

public static int factorial(int n) {!!if (n <= 0)!! !return 1;!!else!! !return n * factorial(n-1);!}!

6.1 Explicit recursion

15

base case

End of Oyster

Page 16: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.2a Exercise

•  Video intro –  Implement the factorial function –  Write unit tests –  Debugger and tracing through the code

•  Point students to their repo for the upcoming exercise

16

BLT 2a public

Page 17: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.2a Exercise

•  Question card –  You do the same for:

•  sum(n) = sum(n-1) + n, if n > 0 •  sum(n) = 0, otherwise

–  In your code, identify (make comments on) •  Recursive call •  Base case

•  Implement and test

17

Page 18: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.2 Exercise

•  Video response –  Show solution

18 End of BLT

Page 19: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.2b Exercise

•  Same setup as previous BLT so this can be a short video intro –  Insist students look at each others' work before

continuing (is this possible?)

19

BLT 2b public

Page 20: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.2b Exercise

•  Question card –  Now do the same for:

•  add(x,y) = x, if y == 0 •  add(x,y) = add(x+1, y-1), otherwise

–  In your code, identify (make comments on) •  Recursive call •  Base case

•  Implement and test •  Under what circumstances does your method

work? •  How could you generalize it to work for any x

and y? 20

Page 21: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.2b Exercise

•  Video response –  Show solution

21 End of BLT

Page 22: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Sometimes, we have to find the recursion in a problem –  It's not explicitly stated –  This is harder, but more like solving a puzzle

•  Requires a bit of faith and vision –  Trust that a method does what we want it to do –  See the problem in terms of its simplest subproblem

6.3 Finding recursive substructure

22

Oyster 3

Page 23: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

6.3 Finding recursive substructure

23

sum 0 + 1 + … +

Page 24: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4

6.3 Finding recursive substructure

24

sum 0 + 1 + … +

Page 25: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4 –  sum(x) = 0 + 1 + … + (x-1) + x

6.3 Finding recursive substructure

25

sum 0 + 1 + … +

Page 26: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4 –  sum(x) = 0 + 1 + … + (x-1) + x –  sum(x+9) = 0 + 1 + … + x + (x+1) + … + (x+8) + (x+9)

6.3 Finding recursive substructure

26

sum 0 + 1 + … +

Page 27: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4

6.3 Finding recursive substructure

27

sum 0 + 1 + … +

Where in this expression do we see a smaller

example of the expression itself?

Page 28: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4

6.3 Finding recursive substructure

28

sum 0 + 1 + … +

Where in this expression do we see a smaller

example of the expression itself?

Page 29: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4 –  sum(3) = 0 + 1 + 2 + 3

6.3 Finding recursive substructure

29

sum 0 + 1 + … +

Where in this expression do we see a smaller

example of the expression itself?

Page 30: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Example –  sum(n) = 0 + 1 + … + n

•  Think of the sum(n) function as a generator of the expression you see on the right

•  Believe that sum(n) really generates such an expression –  sum(4) = 0 + 1 + 2 + 3 + 4 –  sum(3) = 0 + 1 + 2 + 3 –  sum(4) = sum(3) + 4

6.3 Finding recursive substructure

30

sum 0 + 1 + … +

Where in this expression do we see a smaller

example of the expression itself?

Page 31: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 +… + n

6.3 Finding recursive substructure

31

Page 32: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 +… + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

32

Page 33: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

33

Aha, the boxed portion also looks like sum(something), but sum of what?

Page 34: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

34

sum(n-1)

if we believe sum(x) really generates

0 + 1 + … + x

Page 35: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

–  sum(n) = sum(n-1) + n

6.3 Finding recursive substructure

35

sum(n-1)

if we believe sum(x) really generates

0 + 1 + … + x

Page 36: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

–  sum(n) = sum(n-1) + n

6.3 Finding recursive substructure

36

Now we have an explicitly recursive formula.

Page 37: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

–  sum(n) = sum(n-1) + n –  sum(?) = ?

6.3 Finding recursive substructure

37

Now we have an explicitly recursive formula.

But it is missing a base case.

What is the smallest example of sum(n)?

Page 38: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

–  sum(n) = sum(n-1) + n –  sum(0) = 0

6.3 Finding recursive substructure

38

Now we have an explicitly recursive formula.

But it is missing a base case.

What is the smallest example of sum(n)?

Page 39: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

•  Leads to –  sum(n) = 0, if n == 0 –  sum(n) = sum(n-1) + n, otherwise

6.3 Finding recursive substructure

39

Page 40: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Where is the recursive substructure? –  sum(n) = 0 + 1 + … + n

•  Leads to –  sum(n) = 0, if n == 0 –  sum(n) = sum(n-1) + n, otherwise

private static int sum(int n) {!!if (n == 0)!! !return 0;!!else!! !return sum(n-1) + n;!}!

6.3 Finding recursive substructure

40

Page 41: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Why does the following substructure not work? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

41

Page 42: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Why does the following substructure not work? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

42

Boxed expression is

sum(n) - 0

Page 43: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Why does the following substructure not work? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

43

Leads to:

sum(n) = 0 + sum(n) - 0

Page 44: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Why does the following substructure not work? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

44

Leads to:

sum(n) = 0 + sum(n) – 0

This is true, mathematically

Page 45: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Why does the following substructure not work? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

45

Leads to:

sum(n) = 0 + sum(n) – 0

This is true, mathematically

But it does not lead to a simpler use of the sum function

Page 46: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Why does the following substructure not work? –  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

46

Leads to:

sum(n) = 0 + sum(n) – 0

This is true, mathematically

But it does not lead to a simpler use of the sum function

Page 47: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  To work, we must find a smaller instance of the function –  One that seems easier to compute –  One that points us toward the base case

–  sum(n) = 0 + 1 + … + n

–  sum(n) = 0 + 1 + 2 + …. + (n-1) + n

6.3 Finding recursive substructure

47 End of Oyster

Page 48: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

48

6.3a Visual recursion and base cases

Each box is smaller than its containing

box. Where does this recursion stop?

BLT 3a public

Page 49: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

49

Before we understood human reproduction, it was thought

that a woman was born with all of the complete humans inside of

her that would ever be born

Page 50: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

50

Before we understood human reproduction, it was thought

that a woman was born with all of the complete humans inside of

her that would ever be born

Page 51: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

51

Before we understood human reproduction, it was thought

that a woman was born with all of the complete humans inside of

her that would ever be born. What could possibly go wrong?

Page 52: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

•  Question card –  What is wrong with the procreation story?

•  Write and submit your answer

52

Page 53: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

•  Question card –  Try factorial without its base case

public static int factorial(int n) {!! !return n * factorial(n-1);!}!

–  What do you see when you run this on factorial(2)?

53

Page 54: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

54

What could possibly go wrong?

Response: presumes an infinite amount of matter

Page 55: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3a Visual recursion and base cases

55

•  For factorial

•  We see stack overflow

•  We could make the stack bigger

•  But it would still overflow

•  Recursion without base cases

•  leads to stack overflow

End of BLT

Page 56: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.3b Roundtable

•  For formulae, find substructure –  sum(n) = n + (n-1) + (n-2) + … + 0

•  This is backwards from our earlier example –  Multiplication

•  mpy(x,y) = x + x + .. + x (y times)

•  Do some pictures, finding substructure –  Circles –  Drawing a line –  Sierpinski –  square snowflake from intro –  graph paper

•  Why not ½ horizontally or vertically?

56

Roundtable 3b

Page 57: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  Let's try to generate graph paper recursively

6.4 Example

57

Oyster 4

Page 58: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

58

This is our goal

but how do we

see this

recursively?

Page 59: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

59

This is our goal

but how do we

see this

recursively?

Page 60: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

60

(llx,lly)

size

Page 61: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

61

(llx,lly)

(llx, lly+size/2)

size

Page 62: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

62

(llx,lly)

(llx, lly+size/2) (llx+size, lly+size/2)

size

Page 63: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

63

(llx,lly)

(llx, lly+size/2)

size

(llx+size, lly+size/2)

(llx+size/2, lly)

Page 64: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

64

(llx,lly)

(llx, lly+size/2)

size

(llx+size, lly+size/2)

(llx+size/2, lly)

(llx+size/2, lly+size)

Page 65: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

65

(llx,lly)

size

ul ur

ll lr

ul

(ulx, uly)

Page 66: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

66

(llx,lly)

size

ul ur

ll lr

ur

(urx, ury)

Page 67: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Example

67

size

ul ur

ll lr ll

(llx, lly)

Page 68: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Example

68

(llx,lly)

size

ul ur

ll lr lr

(lrx, lry)

Page 69: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

69

(llx,lly)

size

ul ur

ll lr

ul

(llx, lly+size/2)

size/2

Page 70: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.4 Example

70

(llx,lly)

size

ul ur

ll lr

ur

(llx+size/2,lly+size/2)

size/2

Page 71: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Example

71

size

ul ur

ll lr ll

(llx, lly)

size/2

Page 72: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Example

72

(llx,lly)

size

ul ur

ll lr lr

(llx+size/2, lly)

End of Oyster

size/2

Page 73: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.5 Exercise

•  Video intro –  Review slides and coordinates

73

BLT 5 public

Page 74: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.5 Exercise

•  Question card –  Implement graph paper problem

•  Think about –  Simplest substructure –  Base case

74

Page 75: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.5 Exercise

•  Video response –  Show solution

75 End of BLT

Page 76: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.6 There is no 6.6

76

Page 77: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

•  How do we reason about the recursive behavior of methods?

•  If they are functional –  meaning, they have no side effects

•  Then we can substitute recursive calls mathematically –  wherever we see

•  foo(x) –  we substitute the text of foo using x as its input

•  Let's look at factorial again….

6.7 Substitution model

77

Oyster 7

Page 78: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

78

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) =

Page 79: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

79

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 *

Page 80: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

80

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 *

Page 81: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

81

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1)

Page 82: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

82

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1)

Page 83: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

83

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1)

Page 84: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

84

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1) = 3 * 2 * 1 * fact(0)

Page 85: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

85

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1) = 3 * 2 * 1 * fact(0)

Page 86: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

86

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1) = 3 * 2 * 1 * fact(0) = 3 * 2 * 1 * 1

Page 87: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

87

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1) = 3 * 2 * 1 * fact(0) = 3 * 2 * 1 * 1 = 3 * 2 * 1

Page 88: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

88

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1) = 3 * 2 * 1 * fact(0) = 3 * 2 * 1 * 1 = 3 * 2 * 1 = 3 * 2

Page 89: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

89

public static int fact(n) { if (n==0) return 1; // I tend to put base case first else return n * fact(n-1);

}

fact(3) = 3 * fact(3-1) = 3 * fact(2) = 3 * 2 * fact(2-1) = 3 * 2 * fact(1) = 3 * 2 * 1 * fact(1-1) = 3 * 2 * 1 * fact(0) = 3 * 2 * 1 * 1 = 3 * 2 * 1 = 3 * 2 = 6

Page 90: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

90

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 - 0

….

= 0 ? No, must keep computation in the boxes

Page 91: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

91

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 - 0

….

= 0 ? No, must keep computation in the boxes

Each box deserves a pair of parentheses

Page 92: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

92

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

Page 93: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

93

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

Page 94: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

94

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

mys(0) = 0

Page 95: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

95

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

= 3 – 2 – 1 - 0

mys(0) = 0

mys(1) = 1-0 = 1

Page 96: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

96

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

= 3 – 2 – 1

mys(0) = 0

mys(1) = 1-0 = 1

Page 97: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

97

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

= 3 – 2 – 1 = 3 – 2 - 1

mys(0) = 0

mys(1) = 1-0 = 1

mys(2) = 2-1 = 1

Page 98: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

98

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

= 3 – 2 – 1 = 3 – 1

mys(0) = 0

mys(1) = 1-0 = 1

mys(2) = 2-1 = 1

Page 99: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

99

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

= 3 – 2 – 1 = 3 – 1 = 3 - 1

mys(0) = 0

mys(1) = 1-0 = 1

mys(2) = 2-1 = 1

Page 100: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

Substitution model for recursive evaluation

100

public static int mys(n) { if (n==0) return 0; // I tend to put base case first else return n – mys(n-1);

}

mys(3) = 3 – mys(2)

= 3 – 2 – mys(1)

= 3 – 2 – 1 – mys(0)

= 3 – 2 – 1 – 0

= 3 – 2 – 1 = 3 – 1 = 2

mys(0) = 0

mys(1) = 1-0 = 1

mys(2) = 2-1 = 1

End of Oyster

Page 101: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.7b Practice doing substitution

•  Let's practice substitution •  Always write = down the page •  fact(3) = 3 * fact(2) =

etc

101

BLT 7b public

Page 102: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.7b Practice doing substitution

•  Question card •  For sum(n)

–  sum(4) •  For add(x,y) now show substitution for

–  add(10,3)

102

Page 103: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.7b Practice doing substitution

•  Video response •  For sum(n)

–  sum(4) •  For add(x,y) now show substitution for

–  add(10,3)

103 End of BLT

Page 104: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.7c Tracing recursion

•  Use the debugger to trace recursive calls •  Find the code in your repository

104

Roundtable

Page 105: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.7c Tracing recursion

•  Go over the following with each of the 3 students in roundtable:

•  For sum(n) –  sum(4)

•  For add(x,y) now show substitution for –  add(10,3)

•  For mys(n) –  mys(3)

105 End of Roundtable

Page 106: Module 6: Recursioncytron/cse131/slides/6.pdf · 6.0 Introduction • The material we study here involves no new syntax • Instead, we will be analyzing and writing methods, such

6.8 Conclusion

•  Recursion is a powerful technique –  Nice results with relatively little coding –  Code often directly reflects the specified

computation •  Ingredients of recursion

–  Base case –  Recursive call

•  Sometimes we have to find the substructure –  Best to find the next smallest subcase of the larger

problem •  Substitution model helps us reason about

recursion 106

Monolog 8

End of Monologue