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

recursive thinking
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

Senior Symposium, Friday

The Weil Zeta Function for Curves Vy Nguyen 3:45, Cleveland L1 Elicitation of Gestures for Image Editing Andreea Bancila 4:15, Cleveland L1

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.

1 2 3 Monday, April 15, 13

slide-2
SLIDE 2

Solution to recursive case Smaller subproblem Base case

private int factorial (int n) { assert n >= 0; if (n == 0) { return 1; } return n * factorial(n-1); }

  • Factorial

http:/ /xkcd.com/244/

Tabletop Roleplaying

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.

4 5 6 Monday, April 15, 13

slide-3
SLIDE 3

Drawing with the Mouse Drawing with the Mouse

Lots of short lines! Call one group of connected lines a “scribble”

What is a Scribble?

A line segment, followed by A shorter Scribble Base case: an “empty” scribble

7 8 9 Monday, April 15, 13

slide-4
SLIDE 4

Scribble Hierarchy

<<interface>> Scribble Empty Scribble Recursive Scribble

RecursiveScribble

public class RecursiveScribble implements Scribble { / / This line private Line2D line; private Color color; / / The lines that follow it private Scribble rest; / / Methods ... }

EmptyScribble

public class EmptyScribble implements Scribble { / / No instance variables!! / / Methods ... }

10 11 12 Monday, April 15, 13

slide-5
SLIDE 5

Scribble Structure

line white

selectedScribble

line white line white line white line white

EmptyScribble

  • bject

RecursiveScribble

  • bjects

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! }

Calling setColor on Scribble Structure

line red

selectedScribble

line white line white line white

color = newColor;

13 14 15 Monday, April 15, 13

slide-6
SLIDE 6

Calling setColor on Scribble Structure

line red

selectedScribble

line red line white line white

rest.setColor (newColor);

Calling setColor on Scribble Structure

line red

selectedScribble

line red line red line white

rest.setColor (newColor);

Calling setColor on Scribble Structure

line red

selectedScribble

line red line red line red

rest.setColor (newColor);

16 17 18 Monday, April 15, 13

slide-7
SLIDE 7

Calling setColor on Scribble Structure

line red

selectedScribble

line red line red line red

setColor in EmptyScribble does nothing!

Broccoli Broccoli

Smaller substructure => 3 More pieces of broccoli Base case => Flower Little data => Stem

19 20 21 Monday, April 15, 13

slide-8
SLIDE 8

Towers of Hanoi

End of the world in 585 billion years! http:/ / en.wikipedia.org/wiki/Tower_of_Hanoi

More Recursion Humor

http:/ / www.thinkgeek.com/ images/products/zoom/ b2ae_recursion.jpg

Finding a Path in a Maze

Start End

22 23 24 Monday, April 15, 13

slide-9
SLIDE 9

Recursion!

What are the subproblems? What is the base case?

Take 1 step, then search from that step to the end. Reached the end

Solving a Maze

public boolean findMazePath() { return findMazePath(0, 0); } 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; } }

Stacks & Backtracking

Start End

findMazePath(0,0) findMazePath(0,1) findMazePath(0,2) findMazePath(0,3) findMazePath(0,4)

Recursing

25 26 27 Monday, April 15, 13

slide-10
SLIDE 10

Stacks & Backtracking

Start End

findMazePath(0,0) findMazePath(0,1) findMazePath(0,2) findMazePath(0,3) findMazePath(0,4)

Dead end - backtrack

Stacks & Backtracking

Start End

findMazePath(0,0) findMazePath(0,1) findMazePath(0,2) findMazePath(0,3) findMazePath(1,3)

Recurse in a different direction

Eight Queens

Place 8 queens on a chessboard such that no queen can capture any other queen.

FAIL!!

28 29 30 Monday, April 15, 13

slide-11
SLIDE 11

Success! Sudoku

31 32 Monday, April 15, 13