recursive thinking
play

Recursive Thinking Define the solution to a problem in terms of a - PowerPoint PPT Presentation

CS Lunch Mark Floryan UMass How can video games help humans and computers learn from one another? Wednesday, April 17, 12:15 Kendade 307 1 Senior Symposium, Friday Elicitation of Gestures for The Weil Zeta Function for Image Editing


  1. CS Lunch Mark Floryan UMass How can video games help humans and computers learn from one another? Wednesday, April 17, 12:15 Kendade 307 1 Senior Symposium, Friday Elicitation of Gestures for The Weil Zeta Function for Image Editing Curves Andreea Bancila Vy Nguyen 4:15, Cleveland L1 3:45, Cleveland L1 2 Recursive Thinking Define the solution to a problem in terms of a solution to one or more “smaller” subproblems Define a base case, a subproblem that can be solved directly. 3 Monday, April 15, 13

  2. Factorial � private int factorial (int n) { � � assert n >= 0; � � � � if (n == 0) { Base case � � � return 1; � � } � � � � return n * factorial(n-1); Smaller � } subproblem � Solution to recursive case 4 Tabletop Roleplaying http:/ /xkcd.com/244/ 5 Recursive Structures Define a data structure as a piece of data followed by a smaller data structure of the same type Define a base case, a trivial piece of data with nothing following it. 6 Monday, April 15, 13

  3. Drawing with the Mouse 7 Drawing with the Mouse Lots of short lines! Call one group of connected lines a “scribble” 8 What is a Scribble? A line segment, followed by A shorter Scribble Base case: an “empty” scribble 9 Monday, April 15, 13

  4. Scribble Hierarchy <<interface>> Scribble Recursive Empty Scribble Scribble 10 RecursiveScribble public class RecursiveScribble implements Scribble { / / This line private Line2D line; private Color color; / / The lines that follow it private Scribble rest; / / Methods ... } 11 EmptyScribble public class EmptyScribble implements Scribble { / / No instance variables!! / / Methods ... } 12 Monday, April 15, 13

  5. Scribble Structure selectedScribble RecursiveScribble line white line white objects line white line white EmptyScribble object line white 13 Changing the Color of a Scribble in RecursiveScribble: public void setColor (Color newColor) { / / Change the color of this line color = newColor; / / Recursively change the rest of the scribble rest.setColor (newColor); } in EmptyScribble: public void setColor (Color newColor) { / / Nothing to do! } 14 Calling setColor on Scribble Structure selectedScribble line red color = newColor; line white line white line white 15 Monday, April 15, 13

  6. Calling setColor on Scribble Structure selectedScribble line red rest.setColor (newColor); line red line white line white 16 Calling setColor on Scribble Structure selectedScribble line red rest.setColor (newColor); line red line red line white 17 Calling setColor on Scribble Structure selectedScribble line red rest.setColor (newColor); line red line red line red 18 Monday, April 15, 13

  7. Calling setColor on Scribble Structure selectedScribble line red line red setColor in EmptyScribble does line red nothing! line red 19 Broccoli 20 Broccoli Base case => Flower Smaller substructure => 3 More pieces of broccoli Little data => Stem 21 Monday, April 15, 13

  8. Towers of Hanoi End of the world in 585 billion years! http:/ / en.wikipedia.org/wiki/Tower_of_Hanoi 22 More Recursion Humor http:/ / www.thinkgeek.com/ images/products/zoom/ b2ae_recursion.jpg 23 Finding a Path in a Maze Start End 24 Monday, April 15, 13

  9. Recursion! What are the subproblems? Take 1 step, then search from that step to the end. What is the base case? Reached the end 25 Solving public boolean findMazePath() { return findMazePath(0, 0); a Maze } public boolean findMazePath (int x, int y) { if (no new neighbors to visit) { return false; } if ( (x, y) is at the goal) { return true; } else if (findMazePath (x-1, y) || findMazePath (x+1, y) || …) { return true; } else { return false; } 26 } Stacks & Start Backtracking Recursing findMazePath(0,4) findMazePath(0,3) findMazePath(0,2) End findMazePath(0,1) findMazePath(0,0) 27 Monday, April 15, 13

  10. Stacks & Start Backtracking Dead end - backtrack findMazePath(0,4) findMazePath(0,3) findMazePath(0,2) End findMazePath(0,1) findMazePath(0,0) 28 Stacks & Start Backtracking Recurse in a different direction findMazePath(1,3) findMazePath(0,3) findMazePath(0,2) End findMazePath(0,1) findMazePath(0,0) 29 Eight Queens Place 8 queens on a chessboard such that no queen can capture any other queen. FAIL!! 30 Monday, April 15, 13

  11. Success! 31 Sudoku 32 Monday, April 15, 13

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