Recursion - GopherState-CSTA

Recursion
Recursion
Recursion
Recursion
Recursion
Recursion
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
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.
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.
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.
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.
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
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.
}
}
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
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;
}
}
Glen Martin - School for the Talented and Gifted - DISD
Recursion Example (cont)
Fibonacci is both a good and bad choice
for a recursive solution.
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
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)
Glen Martin - School for the Talented and Gifted - DISD
Why Use Recursion
• Faster?
• More space efficient?
• Easier to understand?
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
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.
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”
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.
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
…
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.
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
Glen Martin - School for the Talented and Gifted - DISD
Final Thought
Base Case
should be
Simplest Possible
!
Glen Martin - School for the Talented and Gifted - DISD