Recursion and Recursive Algorithms - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Recursion and Recursive Algorithms

Description:

A recursive method is a method that calls itself repeatedly with different input ... to cause a StackOverflowError by taking waaaay too many recursive calls to reach ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 20
Provided by: jonc7
Category:

less

Transcript and Presenter's Notes

Title: Recursion and Recursive Algorithms


1
Recursion and Recursive Algorithms
2
Definitions
  • An iterative method (like we coded last class) is
    one that uses loops
  • A recursive method is a method that calls itself
    repeatedly with different input parameters, until
    the problem reaches a "base case" stopping point

3
Geeky definition of recursion
  • Recursion - see Recursion.
  • A more accurate definition
  • Recursion If you understand, stop. Otherwise,
    see Recursion.

4
This looks familiar
  • You saw recursion last year in CS1 when we had
    methods call themselves as a way to get it to run
    more than one time before we learned loops. For
    instance, this causes an infinite loop that will
    eventually cause Java to crash
  • public class Test
  • public static void main(String args)
    System.out.println(Hi) Test.main(args)
  • This "crash" was actually a StackOverflowError,
    which we'll look at in a few minutes.

5
The parts of a Recursive Algorithm
  • A Recursive algorithm has two parts
  • If the problem is easy, solve it immediately and
    stop (the base case)
  • If the problem can't be solved immediately,
    divide it into smaller problems, then solve the
    smaller problems by applying this procedure to
    each of them (the recursive case)
  • So a Recursive Algorithm is really made up of
  • At least one base case.
  • At least one recursive case.

6
Basic outline for a recursive algorithm
  • ltvisibilityModifiergt ltreturnTypegt
    ltmethodNamegt(ltparametersgt)
  • if (ltbaseCasegt)
  • // return simple solution
  • else
  • // divide the problem into sub-problems,
  • // getting closer to the base case.
  • // return the result of the simpler case
  • Note recursive methods may have more than one
    base case or more than
  • one recursive case.

7
How recursion works
  • Each time a method is called, it gets placed at
    the top of a "call stack".
  • The methods that do the calling are underneath
    the method they called, waiting for it to finish,
    so they can be done as well

8
public static int mystery(int n) if(n gt 0)
return (n mystery(n - 1)) else return 0
START
START
END
9
StackOverflowError
  • Consider the following
  • public int infinite(int n)
  • return infinite(n1)
  • Whats wrong with this? Wheres the base case?
    If we forget to include a base case or our base
    case is unreachable, then we create infinite
    recursion. In Java, when we have infinite
    recursion, we get a StackOverFlowError.
  • The methods keeps calling itself, and the methods
    that do the calling never get to be removed from
    the stack. This means that we have created so
    many activations of the method that we have run
    out of system resources. This is not good.

10
Example (youll see a LOT like this on the test
next in two weeks)
  • public static int mystery(int n)
  • if(n gt 0)
  • return (n mystery(n - 1))
  • else
  • return 0
  • What happens when mystery(4) is called?
  • mystery(-1)?

11
Example
  • public static int mystery(int x)   if(x1)
         return 1    else     return
    (2mystery(x-1)) x
  • What happens when mystery(4) is called?
  • mystery(-1)?

12
Example
  • public static int mystery(int x)   if(xlt0)
  • return 0
  • else if(x21)       return mystery(x-1)
       else      return xmystery(x-2)
  • What happens when mystery(11) is called?
  • mystery(10)?

13
Tail (or "tail-end") recursion
  • Tail recursion is when the call to the recursive
    part is the last thing in the recursive case (you
    are finished with calculations after you get to
    the base).
  • //tail-end recursion
  • public int recursiveCall(int x)
  • if (ltbaseCasegt)
  • // return simple solution
  • else
  • return recursiveCall(x-1)
  • //not tail-end recursion
  • public int recursiveCall(int x)
  • if (ltbaseCasegt)
  • // return simple solution
  • else

14
Factorials Example
  • A factorial (which we have seen before) is a math
    concept signified by the exclamation mark symbol.
  • Iteratively, a factorial is defined asn! n
    (n-1) (n-2) . (1)
  • Recursively, it looks like this
  • 1 if n 0 n (n-1)!
    if ngt1

n!
15
Recursion vs. Iteration
  • Virtually all recursive algorithms can be
    re-written as iterative, and vice versa.

16
Why use recursion?
  • Some problems are naturally recursive. That means
    recursion is sometimes the natural solution most
    people come up with to solve a problem it is
    often easier to understand and write. If you can
    easily identify a base case and a recursive case,
    then a recursive method might be easier to
    understand. Sometimes a recursive method just
    makes more sense than an iterative method.
  • However, recursive methods come at a cost.
    Recursive methods use a lot of memory because
    each activation requires more memory to be
    allocated for each parameter and local variable.
    So for algorithms that are easily implemented
    iteratively, recursion does not make a lot of
    sense to use.
  • Basically, you should use recursion if it seems
    like a natural solution to the problem, and isnt
    likely to cause a StackOverflowError by taking
    waaaay too many recursive calls to reach the
    solution.

17
Helper methods
  • Because they have to keep track of the thing that
    is changing in each recursive call, recursive
    methods generally have more input parameters than
    iterative methods (one input parameter for each
    thing that needs to be kept track of).
  • To make it simpler for the user to call, you
    generally have a simple public started method
    they can call, and from that method you call a
    private, hidden recursive method that will do the
    real work.
  • The following methods are recursive
    implementations of the searching algorithms we
    looked at last class. Both use used "helper"
    methods to make it easier for end users to call
    without having to worry about what input
    parameter does what.

18
Recursive Sequential Search
  • A recursive version of Sequential Search (where
    the method calls itself, with different input
    parameters) seems a little more confusing and
    anti-intuitive to me, but the basic algorithm is
    still the same.

19
Recursive Binary Search
  • Recursive Binary Search makes a lot more sense to
    me. The max and min indices from last time become
    input parameters, you call the method recursively
    on the left or right side depending on the value
    at the current index you are looking at.
Write a Comment
User Comments (0)
About PowerShow.com