glen martin - school for the talented and gifted - disd recursion recursion recursion recursion...

22
Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion

Upload: felicity-harrison

Post on 12-Jan-2016

248 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

RecursionRecursion

RecursionRecursion

Recursion Recursion

RecursionRecursion

Recursion Recursion

Recursion Recursion

Page 2: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Attribution

With insights from

Thinking Recursively With Java

Eric Roberts

Copyright 2006

John Wiley and Sons, Inc.

0-471-70146-7

Page 3: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Informal Definition

Recursion is the process of solving a problem by reducing it to one or more sub-problems problems that …

are identical in structure to the initial problem.

-- and –

are somewhat simpler to solve.

Page 4: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Informal Definition (cont)

The same reduction technique is continued on each of the sub-problems to make new sub-problems that are even simpler until …

the sub-problems are so simple to solve that there’s no reason to subdivide them further.

Page 5: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Informal Definition (cont)

Then the complete solution is constructed by …

assembling each of the sub-problem solutions.

Page 6: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Observations

• In most cases, the decision to use recursion is suggested by the nature of the problem.

• However, “recursiveness” is a property of the solution, not the problem.

Page 7: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Requirements

To solve a problem recursively …

1. the problem must be successively decomposable into simpler instances of the same problem.

2. the sub-problems must eventually become simple (they can be solved without further subdivision).

3. it must be possible to combine all the sub-problem solutions to produce a solution to the original problem

Page 8: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Recursion Pseudocode

public void solve(ProblemClass instance){ if (instance is simple) Solve instance directly. else { Divide instance into sub-instances i1, i2, i3, … solve(i1); solve(i2); solve(i3), … Reassemble the sub-instance solutions to solve the entire problem. }}

Page 9: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Recursion Example

Fibonacci Numbers

fib(0) is 1

fib(1) is 1

fib(n) is fib(n-1) + fib(n-2) for n > 1

Page 10: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Recursion Example (cont)

public int fib(int n){ if (n <= 1) return 1; else { int fib1 = fib(n – 1); int fib2 = fib(n – 2); return fib1 + fib2; }}

Page 11: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Recursion Example (cont)

Fibonacci is both a good and bad choicefor a recursive solution.

Page 12: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Recursion Example (cont)

Fibonacci is both a good and bad choicefor a recursive solution.

• Good – it has a straightforward, elegant recursive solution

Page 13: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Recursion Example (cont)

Fibonacci is both a good and bad choicefor a recursive solution.

• Good – it has a straightforward, elegant recursive solution

• Bad – the algorithm runs in exponential time - O(2^n)

Page 14: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Why Use Recursion

• Faster?

• More space efficient?

• Easier to understand?

Page 15: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Why Use Recursion

• Faster?– No, extra time required for recursive calls.– Speed lost is typically acceptable though.

• More space efficient?– No, extra storage required for recursive calls.– Additional storage is typically acceptable though.

• Easier to understand? – Yes

Page 16: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Easy to Understand?

• Recursion is a somewhat uncommon experience.

• Recursion requires belief in magic (a leap of faith).– Try to trace fib(10)– Have to be “lazy”. Only think about problems

at a single level. Let “someone else” handle the other levels.

Page 17: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

How to teach “laziness”

• Student distance to the wall enactment.

• Student problems with a stack of number cards (i.e. sum, maximum, …)

• KEY – each student is responsible for ONE recursive “method execution”

Page 18: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Stupid Recursion

• Calculate n!

• Calculate 1 + 2 + … + n

• Calculate fib(n)

• …

• Easy to show, but not motivating.

Page 19: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Not Stupid Recursion

• Many List and Tree problems (CS AB)

• Permutations

• Maze Solving

• Sorting (Merge, Quick)

• Fractal Drawing

• …

Page 20: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

First Recursion Lab

• Should be– Simple enough for students to solve.– Not stupid (doesn’t have an easy iterative

solution).– Real world.

Page 21: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

First Recursion Lab

• Should be– Simple enough for students to solve.– Not stupid (doesn’t have an easy iterative

solution).– Real world.

• Answer– Recursive File Lister, N Queens, Nine Squares,

Numbrix

Page 22: Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion

Glen Martin - School for the Talented and Gifted - DISD

Final Thought

Base Case

should be

Simplest Possible

!