recursion big picture
play

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 -


  1. 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 - 1 L - 2 PictureWorld Example PictureWorld Example Suppose we have the following two methods: Suppose we have the following two methods: public Picture triangles(Color c1, Color c2) public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) public Picture LL(Picture p) p p If we had a method which generated this picture, then maybe we could Goal Goal create our goal picture... How can we solve this How can we generate this picture? problem using a smaller version of the same Should we use recursion? problem? L - 3 L - 4 PictureWorld Example PictureWorld Example Suppose we have the following two methods: Suppose we have the following two methods: public Picture triangles(Color c1, Color c2) public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) public Picture LL(Picture p) p p If we had a method which generated If we had a method which generated this picture, then maybe we could this picture, then maybe we could Goal Goal create our goal picture... create our goal picture... LL(triangleDesign( 3,Color.red,Color.blue)) triangleDesign(3,Color.red,Color.blue) triangleDesign(3,Color.red,Color.blue) Put this Assume we have such a picture in the lower method... a leap of faith! left corner... L - 5 L - 6

  2. PictureWorld Example triangleDesign Method Suppose we have the following two methods: public Picture triangles(Color c1, Color c2) public Picture LL(Picture p) p Goal LL(triangleDesign( 3,Color.red,Color.blue)) triangles(Color.red,Color.blue) Overlay L - 7 L - 8 return JEM JEM Execution Land Execution Land triangleDesign triangleDesign triangleDesign triangleDesign rtw red blue rtw red blue rtw red blue rtw red blue this steps 4 c1 c2 this steps 3 c1 c2 this steps 4 c1 c2 this steps 3 c1 c2 if (steps <= 1) { // base case if (steps <= 1) { // base case if (steps <= 1) { // base case if (steps <= 1) { // base case return triangles(c1, c2); return triangles(c1, c2); return triangles(c1, c2); return triangles(c1, c2); } else { // recursive case } else { // recursive case } else { // recursive case } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); } } } } triangleDesign triangleDesign triangleDesign triangleDesign rtw red blue rtw red blue rtw red blue rtw red blue this steps 1 c1 c2 this steps 2 c1 c2 this steps 1 c1 c2 this steps 2 c1 c2 if (steps <= 1) { // base case if (steps <= 1) { // base case if (steps <= 1) { // base case if (steps <= 1) { // base case return triangles(c1, c2); return triangles(c1, c2); return triangles(c1, c2); return triangles(c1, c2); } else { // recursive case } else { // recursive case } else { // recursive case } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); } } L - 9 } } L - 10 p1 p2 return p1 p2 return JEM JEM Execution Land Execution Land triangleDesign triangleDesign triangleDesign triangleDesign rtw red blue rtw red blue rtw red blue rtw red blue this steps 4 c1 c2 this steps 3 c1 c2 this steps 4 c1 c2 this steps 3 c1 c2 if (steps <= 1) { // base case if (steps <= 1) { // base case if (steps <= 1) { // base case if (steps <= 1) { // base case p1 ? return triangles(c1, c2); return triangles(c1, c2); return triangles(c1, c2); return triangles(c1, c2); p2 ? } else { // recursive case } else { // recursive case } else { // recursive case } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); return overlay(LL(p1), p2); } } } } triangleDesign rtw red blue this steps 2 c1 c2 if (steps <= 1) { // base case p1 ? return triangles(c1, c2); p2 ? } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); } L - 11 L - 12

  3. p1 p2 return JEM Comments Execution Land triangleDesign rtw red blue this steps 4 c1 c2 • We now have recursion with returned values, if (steps <= 1) { // base case p1 ? return triangles(c1, c2); yippee!!! p2 ? } else { // recursive case Picture p1 = triangleDesign(steps-1, c1, c2); • In this case, the number of times to recurse is Picture p2 = triangles(c1, c2); return overlay(LL(p1), p2); determined by a counter (steps) - explicit. } • In this case, there are things left to do in method after recursive call: non-tail recursion. L - 13 L - 14 Buggle Example eatBagels Method public int eatBagels() { Given: Suppose we have a random size grid with a random number of bagels in the first row. return 5 Problem: Write a method eatBagels such that - Buggle walks up to wall eating bagels - Method returns number of bagels which Buggle has eaten } L - 15 L - 16 Exercise 1 Exercise 2 Problem: Write a method bagelForward such that Problem: Write a method distanceToWall such that - The method has one integer parameter - The method returns an integer representing how far the Buggle is from the wall - Buggle moves forward the given number of steps dropping bagels at each cell along the way. - The Buggle returns to the cell it started from before the method was invoked becky.brushUp(); becky.dropInt(becky.distanceToWall()); becky.bagelForward(6); 7 L - 17 L - 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend