Recursion: How It Works 7 January 2019 OSU CSE 1 Question - - PowerPoint PPT Presentation

recursion how it works
SMART_READER_LITE
LIVE PREVIEW

Recursion: How It Works 7 January 2019 OSU CSE 1 Question - - PowerPoint PPT Presentation

Recursion: How It Works 7 January 2019 OSU CSE 1 Question Considered Before How should you think about recursion so you can use it to develop elegant recursive methods to solve certain problems? Answer : Pretend there is a FreeLunch


slide-1
SLIDE 1

Recursion: How It Works

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Question Considered Before

  • How should you think about recursion

so you can use it to develop elegant recursive methods to solve certain problems?

  • Answer: Pretend there is a FreeLunch

class with a method that has the same contract as the code you’re trying to write (but it works only for smaller problems)

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

Question Considered Before

  • Why do those recursive methods work?
  • Answer: Following the “confidence-

building” approach, you can argue as follows:

– Does it work on all “smallest” cases?

7 January 2019 OSU CSE 3

slide-4
SLIDE 4

Question Considered Before

  • Why do those recursive methods work?
  • Answer: Following the “confidence-

building” approach, you can argue as follows:

– Does it work on all “smallest” cases? ✓ – Does it work on all “next smallest” cases?

7 January 2019 OSU CSE 4

slide-5
SLIDE 5

Question Considered Before

  • Why do those recursive methods work?
  • Answer: Following the “confidence-

building” approach, you can argue as follows:

– Does it work on all “smallest” cases? ✓ – Does it work on all “next smallest” cases? ✓ – Does it work on all “next smallest” cases?

7 January 2019 OSU CSE 5

slide-6
SLIDE 6

Question Considered Before

  • Why do those recursive methods work?
  • Answer: Following the “confidence-

building” approach, you can argue as follows:

– Does it work on all “smallest” cases? ✓ – Does it work on all “next smallest” cases? ✓ – Does it work on all “next smallest” cases? ✓ – ... (Formally, proof by mathematical induction)

7 January 2019 OSU CSE 6

slide-7
SLIDE 7

Question Considered Now

  • How do those recursive methods work?

– As promised, we have come back to this, but we continue to advise... – If you insist on thinking about recursion this way (rather than simply sating your curiosity about how it works), you may never be fully capable of developing elegant recursive solutions to problems!

7 January 2019 OSU CSE 7

slide-8
SLIDE 8

Example

private static String reversedString(String s) {

if (s.length() == 0) { return s; } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0); }

}

7 January 2019 OSU CSE 8

slide-9
SLIDE 9

Trace reversedString("OSU")

7 January 2019 OSU CSE 9

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); return rSub + s.charAt(0);

slide-10
SLIDE 10

7 January 2019 OSU CSE 10

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

Question: This is a recursive call, so how does it work?

slide-11
SLIDE 11

7 January 2019 OSU CSE 11

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

Answer: Exactly like this one, and every other call!

slide-12
SLIDE 12

How Every Call Works

  • First, the tracing table for the code making

the call is suspended and that tracing table is pushed onto the runtime stack

– The runtime stack, often called simply “the stack”, is effectively just a stack of tracing tables (think Stack<TracingTable>), each partially filled in with the results of the code in that tracing table as executed so far

7 January 2019 OSU CSE 12

slide-13
SLIDE 13

How Every Call Works

  • A new tracing table is created, containing

the code for the method body being called

  • The argument values are copied from the

suspended tracing table into the formal parameters to start the new tracing table

  • Execution in the new tracing table

continues until it calls a method...

7 January 2019 OSU CSE 13

slide-14
SLIDE 14

7 January 2019 OSU CSE 14

s = "OSU" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

The currently executing tracing table gets to here ...

slide-15
SLIDE 15

7 January 2019 OSU CSE 15

s = "OSU" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... and the (top of the) stack

  • f suspended tables is here.
slide-16
SLIDE 16

7 January 2019 OSU CSE 16

s = "OSU" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

This call suspends the current tracing table ...

slide-17
SLIDE 17

7 January 2019 OSU CSE 17

Trace reversedString("OSU")

... the suspended tracing table is pushed ...

slide-18
SLIDE 18

7 January 2019 OSU CSE 18

this = "OSU" /* * Body for length method * from class String; we * do not have this, so how * do we know what it does? * We look at its contract! * When it finishes, we know * it has not changed this * (it could not even if it * wanted to), and it returns * the length of this. */ this = "OSU" length = 3

Trace reversedString("OSU")

... and the tracing table for length begins.

slide-19
SLIDE 19

How Every Return Works

  • When the currently executing tracing table

reaches a return statement, or for a void method falls off the end of the body, the results of the call are reflected in the tracing table on the top of the stack

  • That tracing table is popped off the stack

and it becomes the currently executing tracing table, resuming execution from the point where it was suspended

7 January 2019 OSU CSE 19

slide-20
SLIDE 20

7 January 2019 OSU CSE 20

this = "OSU" /* * Body for length method * from class String; we * do not have this, so how * do we know what it does? * We look at its contract! * When it finishes executing * it has not changed this * (it could not even if it * wanted to), and it returns * the length of this */ this = "OSU" length = 3

Trace reversedString("OSU")

When this call returns ...

slide-21
SLIDE 21

7 January 2019 OSU CSE 21

Trace reversedString("OSU")

... its results are reflected in the calling table ...

slide-22
SLIDE 22

7 January 2019 OSU CSE 22

s = "OSU" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... and that table is popped to resume execution.

slide-23
SLIDE 23

7 January 2019 OSU CSE 23

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

Execution continues to the next call ...

slide-24
SLIDE 24

7 January 2019 OSU CSE 24

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... and when that call returns, to the next call ...

slide-25
SLIDE 25

7 January 2019 OSU CSE 25

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... which is a recursive call! But it is nothing special.

slide-26
SLIDE 26

7 January 2019 OSU CSE 26

Trace reversedString("OSU")

The current tracing table is suspended and pushed ...

slide-27
SLIDE 27

7 January 2019 OSU CSE 27

s = "SU" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... and a new table for the body of the called method ...

slide-28
SLIDE 28

7 January 2019 OSU CSE 28

s = "SU" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... begins with its own variables and values.

slide-29
SLIDE 29

7 January 2019 OSU CSE 29

s = "SU" if (s.length() == 0) { ... } else { s = "SU" String sub = s.substring(1); s = "SU" sub = "U" String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

Soon, this tracing table reaches a recursive call!

slide-30
SLIDE 30

7 January 2019 OSU CSE 30

Trace reversedString("OSU")

The current tracing table is suspended and pushed ...

slide-31
SLIDE 31

7 January 2019 OSU CSE 31

s = "U" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... and a new table for the body of the called method ...

slide-32
SLIDE 32

7 January 2019 OSU CSE 32

s = "U" if (s.length() == 0) { ... } else { String sub = s.substring(1); String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

... begins with its own variables and values.

slide-33
SLIDE 33

7 January 2019 OSU CSE 33

s = "U" if (s.length() == 0) { ... } else { s = "U" String sub = s.substring(1); s = "U" sub = "" String rSub = reversedString(sub); return rSub + s.charAt(0);

Trace reversedString("OSU")

Soon, this tracing table reaches a recursive call!

slide-34
SLIDE 34

7 January 2019 OSU CSE 34

Trace reversedString("OSU")

The current tracing table is suspended and pushed ...

slide-35
SLIDE 35

7 January 2019 OSU CSE 35

Trace reversedString("OSU")

s = "" if (s.length() == 0) { return s;

... and a new table for the body of the called method ...

slide-36
SLIDE 36

7 January 2019 OSU CSE 36

s = "" if (s.length() == 0) { return s;

Trace reversedString("OSU")

... begins with its own variables and values.

slide-37
SLIDE 37

7 January 2019 OSU CSE 37

Trace reversedString("OSU")

s = "" if (s.length() == 0) { s = "" return s;

Soon, this tracing table returns ...

slide-38
SLIDE 38

7 January 2019 OSU CSE 38

Trace reversedString("OSU")

... its results are reflected in the calling location ...

slide-39
SLIDE 39

7 January 2019 OSU CSE 39

Trace reversedString("OSU")

s = "U" if (s.length() == 0) { ... } else { s = "U" String sub = s.substring(1); s = "U" sub = "" String rSub = reversedString(sub); return rSub + s.charAt(0);

... and that table is popped to resume execution.

slide-40
SLIDE 40

7 January 2019 OSU CSE 40

Trace reversedString("OSU")

s = "U" if (s.length() == 0) { ... } else { s = "U" String sub = s.substring(1); s = "U" sub = "" String rSub = reversedString(sub); s = "U" sub = "" rSub = "" return rSub + s.charAt(0);

Soon, this tracing table returns ...

slide-41
SLIDE 41

7 January 2019 OSU CSE 41

Trace reversedString("OSU")

... its results are reflected in the calling location ...

slide-42
SLIDE 42

7 January 2019 OSU CSE 42

Trace reversedString("OSU")

s = "SU" if (s.length() == 0) { ... } else { s = "SU" String sub = s.substring(1); s = "SU" sub = "U" String rSub = reversedString(sub); return rSub + s.charAt(0);

... and that table is popped to resume execution.

slide-43
SLIDE 43

7 January 2019 OSU CSE 43

Trace reversedString("OSU")

s = "SU" if (s.length() == 0) { ... } else { s = "SU" String sub = s.substring(1); s = "SU" sub = "U" String rSub = reversedString(sub); s = "SU" sub = "U" rSub = "U" return rSub + s.charAt(0);

Soon, this tracing table returns ...

slide-44
SLIDE 44

7 January 2019 OSU CSE 44

Trace reversedString("OSU")

... its results are reflected in the calling location ...

slide-45
SLIDE 45

7 January 2019 OSU CSE 45

Trace reversedString("OSU")

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); return rSub + s.charAt(0);

... and that table is popped to resume execution.

slide-46
SLIDE 46

7 January 2019 OSU CSE 46

Trace reversedString("OSU")

s = "OSU" if (s.length() == 0) { ... } else { s = "OSU" String sub = s.substring(1); s = "OSU" sub = "SU" String rSub = reversedString(sub); s = "OSU" sub = "SU" rSub = "US" return rSub + s.charAt(0);

Soon, this tracing table returns ...

slide-47
SLIDE 47

Finally!

  • The value returned to the original calling

program is the string "USO"

– Phew! – And it is even correct: the result of reversing the string "OSU" is the string "USO"

7 January 2019 OSU CSE 47

slide-48
SLIDE 48

Conclusion

  • Each call to a method—whether

recursive or not—effectively results in the creation of a new tracing table containing the body of the called method

  • Each tracing table has its own variables:

– Its own formal parameters – Its own local variables

7 January 2019 OSU CSE 48

slide-49
SLIDE 49

Conclusion

  • If you really think you can reason about

recursive code by mentally executing this kind of a series of events to check your thinking, then ... you’re deluding yourself

7 January 2019 OSU CSE 49

slide-50
SLIDE 50

Conclusion

  • If you really think you can reason about

recursive code by mentally executing this kind of a series of events to check your thinking, then ... you’re deluding yourself

7 January 2019 OSU CSE 50

And if you don’t believe it yet, try mentally executing this way for code that makes multiple recursive calls from each tracing table.