Docsity

Fruitful Recursion: Solving Problems by Breaking Them Down with Java Code Examples, Study notes of Computer Science

Toggle sidebar
bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Fruitful recursion
Recursion with methods that
return values
Friday, October 20, 2006
Fruitful recursion 12-2
Recursion
oRecursion is a solution technique that is effective when a
problem can be divided into smaller subproblems of the
same shape.
oFirst, we studied cases
with no parameters and
no returned values.
oThen we studied
recursive methods with
parameters.
oToday we study
recursive methods that
return values (and may
also take parameters).
bagelsToWall();
tree(4,20,40,0.7);
cornerPush(4,di);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Fruitful Recursion: Solving Problems by Breaking Them Down with Java Code Examples and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Fruitful recursion

Recursion with methods that

return values

Friday, October 20, 2006

Fruitful recursion 12- 2

Recursion

o Recursion is a solution technique that is effective when a

problem can be divided into smaller subproblems of the

same shape.

o First, we studied cases

with no parameters and

no returned values.

o Then we studied

recursive methods with

parameters.

o Today we study

recursive methods that

return values (and may

also take parameters).

bagelsToWall(); tree(4,20,40,0.7); cornerPush(4,di);

Fruitful recursion 12- 3

Recursive Pictures: upPush() upPush(4,di) upPush(5,hex)

Fruitful recursion 12- 4

Designing upPush() with an Invocation Tree Let’s draw an invocation tree for upPush(3,di):

Fruitful recursion 12- 7

Defining rightPush() in Java

Fruitful recursion 12- 8

Recursive Pictures: cornerPush() cornerPush(4,di) cornerPush(5,hex)

Fruitful recursion 12- 9

Defining cornerPush() in Java

Fruitful recursion 12- 10

DistanceWorld Diana buggle wants to find the number of steps to the wall.

o What is the base case for this problem?

o In the general case:

(1) How is the problem made smaller?

(2) How is the solution to the whole problem obtained

from the solution to the smaller problem?

Fruitful recursion 12- 13

position heading

EAST

color red brushDown false The First Invocation

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB

DW .run()

DB .stepsToWall();

Fruitful recursion 12- 14

position heading

EAST

color red brushDown false Making the Problem Smaller

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB

DW .run()

DB .stepsToWall();

Fruitful recursion 12- 15

if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading

EAST

color red brushDown false Smaller Again

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB this (^) DB

DW .run()

DB .stepsToWall(); DB .stepsToWall();

Fruitful recursion 12- 16

if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading

EAST

color red brushDown false Even Smaller

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} this (^) DB

DW .run()

DB .stepsToWall(); DB .stepsToWall(); DB .stepsToWall();

Fruitful recursion 12- 19

if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading

EAST

color red brushDown false Saving the Answer

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1 ; backward(); return ans;} this (^) DB

DW .run()

DB .stepsToWall(); DB .stepsToWall(); DB .stepsToWall();

ans^1

Fruitful recursion 12- 20

if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading

EAST

color red brushDown false Backing Up

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1 ; backward(); return ans;} this (^) DB

DW .run()

DB .stepsToWall(); DB .stepsToWall(); DB .stepsToWall();

ans 1

Fruitful recursion 12- 21

if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading

EAST

color red brushDown false About to Return from the Penultimate Case

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1 ; backward(); return 1 ;} this (^) DB

DW .run()

DB .stepsToWall(); DB .stepsToWall(); DB .stepsToWall();

ans^1

Fruitful recursion 12- 22

if (isFacingWall()){ return 0; } else { forward(); int ans = 1+ 1 ; backward(); return ans;} position heading

EAST

color red brushDown false The Story Continues

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}

DW DB

this (^) DB this (^) DB

DW .run()

DB .stepsToWall(); DB .stepsToWall();

Fruitful recursion 12- 25

position heading

EAST

color red brushDown false The Final Return

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 3 ; backward(); return 3 ;}

DW DB

this (^) DB

DW .run()

DB .stepsToWall();

ans^3

Fruitful recursion 12- 26

position heading

EAST

color red brushDown false The Answer can be Displayed

DistanceBuggle DB

DistanceWorld DW

DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println( 3 ); this diana Object Land Execution Land DW DB

DW .run()

Fruitful recursion 12- 27

Which of the Following Work? public int stepsToWall2() { if (isFacingWall()) { return 0; } else { forward(); int subSteps = stepsToWall2(); backward(); return 1+subSteps; } } public int stepsToWall4() { int n = 0; if (!isFacingWall()) { forward(); n = n + 1; stepsToWall4(); backward(); } return n; } public int stepsToWall3() { int n = 0; if (!isFacingWall()) { forward(); n = 1+stepsToWall3(); backward(); } return n; } public int n = 0; // extra instance variable public int stepsToWall5() { if (!isFacingWall()) { forward(); n = n + 1; stepsToWall5(); backward(); } return n; }

Fruitful recursion 12- 28

BagelCountingWorld

Suppose a buggle wants to count the number of

bagels between it and the wall.

Does it count the bagel under it or not?

We’ll consider both choices:

public int bagelsInFront(); Returns the number of bagels between this buggle and the wall it is facing, excluding any bagel that might be under it. Maintains position, heading, color, and brush state of buggle as invariants. public int bagelsUnderAndInFront(); Returns the number of bagels between this buggle and the wall it is facing, including any bagel that might be under it. Maintains position, heading, color, and brush state of buggle as invariants. Both solutions are similar to stepsToWall(). Here’s a useful helper method:

public int bagelsUnder() {

if (isOverBagel()) return 1; else return 0;