Recursion Big Picture Recursion is a technique for solving problems - - PDF document

recursion big picture
SMART_READER_LITE
LIVE PREVIEW

Recursion Big Picture Recursion is a technique for solving problems - - PDF document

Recursion Big Picture Recursion is a technique for solving problems (akin to Divide, Conquer, and Glue) Recursive Methods That Return Values Recursion is effective when sub-problems have the same shape as the original problem L -


slide-1
SLIDE 1

L - 1

Recursive Methods That Return Values

L - 2

Recursion Big Picture

  • Recursion is a technique for solving problems

(akin to Divide, Conquer, and Glue)

  • Recursion is effective when sub-problems have

the same “shape” as the original problem

L - 3

PictureWorld Example

Suppose we have the following two methods:

public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) p

Goal

How can we generate this picture? Should we use recursion?

L - 4

PictureWorld Example

Suppose we have the following two methods:

public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) p

Goal

If we had a method which generated this picture, then maybe we could create our goal picture...

How can we solve this problem using a smaller version of the same problem?

L - 5

PictureWorld Example

Suppose we have the following two methods:

public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) p

Goal

If we had a method which generated this picture, then maybe we could create our goal picture...

triangleDesign(3,Color.red,Color.blue)

Assume we have such a method... a leap of faith!

L - 6

PictureWorld Example

Suppose we have the following two methods:

public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) p

Goal

Put this picture in the lower left corner... If we had a method which generated this picture, then maybe we could create our goal picture...

triangleDesign(3,Color.red,Color.blue) LL(triangleDesign( 3,Color.red,Color.blue))

slide-2
SLIDE 2

L - 7

PictureWorld Example

Suppose we have the following two methods:

public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) p

Goal

Overlay

LL(triangleDesign( 3,Color.red,Color.blue)) triangles(Color.red,Color.blue) L - 8

triangleDesign Method

L - 9

JEM

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

4 rtw

Execution Land

red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

3 rtw red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

1 rtw red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

2 rtw red blue

L - 10

JEM

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

4 rtw

Execution Land

red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

3 rtw red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

1 rtw red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

2 rtw red blue return

L - 11

JEM

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

4 rtw

Execution Land

red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

3 rtw red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

2 rtw red blue return p2 p1 p1 p2 ? ?

L - 12

JEM

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

4 rtw

Execution Land

red blue

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

3 rtw red blue return p2 p1 p1 p2 ? ?

slide-3
SLIDE 3

L - 13

JEM

if (steps <= 1) { // base case return triangles(c1, c2); } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); }

this steps c1 c2

triangleDesign

4 rtw

Execution Land

red blue return p2 p1 p1 p2 ? ?

L - 14

Comments

  • We now have recursion with returned values,

yippee!!!

  • In this case, the number of times to recurse is

determined by a counter (steps) - explicit.

  • In this case, there are things left to do in method

after recursive call: non-tail recursion.

L - 15

Buggle Example

Given: Suppose we have a random size grid with a random number of bagels in the first row. Problem: Write a method eatBagels such that

  • Buggle walks up to wall eating bagels
  • Method returns number of bagels which Buggle has eaten

return 5

L - 16

eatBagels Method

public int eatBagels() { }

L - 17

Exercise 1

Problem: Write a method bagelForward such that

  • The method has one integer parameter
  • Buggle moves forward the given number of steps dropping

bagels at each cell along the way.

becky.bagelForward(6);

L - 18

Exercise 2

Problem: Write a method distanceToWall such that

  • The method returns an integer representing how far the

Buggle is from the wall

  • The Buggle returns to the cell it started from before the

method was invoked

becky.brushUp(); becky.dropInt(becky.distanceToWall());

7