Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion."— Presentation transcript:

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

2 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

3 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.

4 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.

5 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.

6 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.

7 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

8 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. }

9 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

10 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; }

11 Glen Martin - School for the Talented and Gifted - DISD Recursion Example (cont) Fibonacci is both a good and bad choice for a recursive solution.

12 Glen Martin - School for the Talented and Gifted - DISD Recursion Example (cont) Fibonacci is both a good and bad choice for a recursive solution. Good – it has a straightforward, elegant recursive solution

13 Glen Martin - School for the Talented and Gifted - DISD Recursion Example (cont) Fibonacci is both a good and bad choice for a recursive solution. Good – it has a straightforward, elegant recursive solution Bad – the algorithm runs in exponential time - O(2^n)

14 Glen Martin - School for the Talented and Gifted - DISD Why Use Recursion Faster? More space efficient? Easier to understand?

15 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

16 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.

17 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”

18 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.

19 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 …

20 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.

21 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

22 Glen Martin - School for the Talented and Gifted - DISD Final Thought Base Case should be Simplest Possible !


Download ppt "Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion."

Similar presentations


Ads by Google