Top Banner
Recursion: How It Works 7 January 2019 OSU CSE 1
50

Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

Oct 04, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

Recursion: How It Works

7 January 2019 OSU CSE 1

Page 2: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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 FreeLunchclass 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

Page 3: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 4: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 5: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 6: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 7: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 8: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

Exampleprivate 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

Page 9: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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);

Page 10: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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?

Page 11: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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!

Page 12: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 13: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 14: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 15: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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 of suspended tables is here.

Page 16: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 17: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 17

Trace reversedString("OSU")... the suspended tracing table is pushed ...

Page 18: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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.

Page 19: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 20: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 21: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 21

Trace reversedString("OSU")... its results are reflected in the calling table ...

Page 22: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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 poppedto resume execution.

Page 23: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 24: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 25: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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.

Page 26: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 26

Trace reversedString("OSU")The current tracing table is suspended and pushed ...

Page 27: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 28: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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.

Page 29: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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!

Page 30: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 30

Trace reversedString("OSU")The current tracing table is suspended and pushed ...

Page 31: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 32: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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.

Page 33: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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!

Page 34: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 34

Trace reversedString("OSU")The current tracing table is suspended and pushed ...

Page 35: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 36: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 36

s = ""

if (s.length() == 0) {

return s;

Trace reversedString("OSU")... begins with its own variables and values.

Page 37: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 37

Trace reversedString("OSU")s = ""

if (s.length() == 0) {

s = ""

return s;

Soon, this tracing table returns ...

Page 38: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 38

Trace reversedString("OSU")... its results are reflected in the calling location ...

Page 39: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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 poppedto resume execution.

Page 40: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 41: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 41

Trace reversedString("OSU")... its results are reflected in the calling location ...

Page 42: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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 poppedto resume execution.

Page 43: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 44: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

7 January 2019 OSU CSE 44

Trace reversedString("OSU")... its results are reflected in the calling location ...

Page 45: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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 poppedto resume execution.

Page 46: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 47: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 48: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 49: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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

Page 50: Recursion: How It Works - Computer Scienceweb.cse.ohio-state.edu/.../slides/33.Recursion-How.pdf · Question Considered Before • How should you think about . recursion so you can

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.