And now for something completely different: recursion.

17
And now for something completely different: recursion
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    2

Transcript of And now for something completely different: recursion.

Page 1: And now for something completely different: recursion.

And now for something completely different: recursion

Page 2: And now for something completely different: recursion.

OverviewRecursive ThinkingRecursive JavaExample: maze searchAnalysis of recursive algorithms

Recursion p. 2/17

Page 3: And now for something completely different: recursion.

Recursive ThinkingA journey of 1000 miles begins with a single step.

– Ancient proverb

Algorithm: when you have a big job to do, divide it into parts. Pick the easiest one. Can you do it easily? If yes, do it. If not, divide it into parts. Pick the easiest one. Can you do it easily? If yes, do it, If not …

Recursion p. 3/17

Page 4: And now for something completely different: recursion.

Three requirements for recursion1. A self-similar problem2. A version of the problem that can be solved without

recursion (the “base case”)3. A way to move from a “larger” problem to a smaller one

Recursion p. 4/17

Page 5: And now for something completely different: recursion.

Using recursionCan we use recursion to: Search a list? Print a list? Draw a spiral? Draw a set of concentric circles? Compile a program?Why or why not?

Recursion p. 5/17

Page 6: And now for something completely different: recursion.

Recursive JavaConsider the example on p. 189 of your text.What’s the base case?How does the code move from a larger to a

smaller problem?In what way is this problem “self-similar”?

Recursion p. 6/17

Page 7: And now for something completely different: recursion.

When to use recursionWhen a problem meets the requirements for a

recursive solution, should you always use recursion? Always use a loop? How do you justify your decision?

Stay tuned …

Recursion p. 7/17

Page 8: And now for something completely different: recursion.

Example: maze searchConsider the maze search example on pp. 193—196.Where are the recursive call(s)?What are the base cases?How does the problem get smaller?Hand-trace the program through the first few

recursive calls. Check with your neighbor and see what they have. Then reconsider the above questions.

Recursion p. 8/17

Page 9: And now for something completely different: recursion.

Debugging recursionThe bug in the following code (if any) is:public boolean search (T item, MyList<T> list) {

this.search(item, list.getRest());

} // Note: // MyList may or may not be orderedA. There is no base caseB. The problem is not self-similarC. There is a base case, but the problem does not

get smallerD. Some other bugE. There are no bugs

Recursion p. 9/17

Page 10: And now for something completely different: recursion.

Debugging recursion (2)The bug in the following code (if any) is:public boolean search (T item, MyList<T> list) {

if (list == null) return false;

else if (list.getFirst()==item)return true;

else return (this.search(item, list);

}

A. There is no base caseB. The problem is not self-similarC. There is a base case, but the problem does not get smallerD. Some other bugE. There are no bugs

Recursion p. 10/17

Page 11: And now for something completely different: recursion.

Debugging recursion (3)The bug in the following code (if any) is:public boolean search (T item, MyList<T> list) {

if (list == null) return false;

else if (list.getFirst()==item)return true;

else return (this.search(item, list.getRest());

}

A. There is no base caseB. The problem is not self-similarC. There is a base case, but the problem does not get smallerD. Some other bugE. There are no bugs

Recursion p. 11/17

Page 12: And now for something completely different: recursion.

Analyzing recursive algorithmspublic int factorial (int n) {

if (n==0) return 1;

else if (n>0)

return n*factorial(n-1);

else throw InvalidInputException(“Negative input”);

}

The Big-Oh time complexity of this method is:A. O(1)B. O(log2n)

C. O(n)D.O(n2)E. None of the above

Recursion p. 12/17

Page 13: And now for something completely different: recursion.

Analyzing recursive algorithmsRewrite the factorial method using a loop instead of

recursion:

public int factorial (int n) {

if (n==0) return 1;

else if (n>0)

return n*factorial(n-1);

else throw InvalidInputException(“Negative input”);

}

What is the time complexity of your iterative method?

Recursion p. 13/17

Page 14: And now for something completely different: recursion.

Analyzing recursive algorithmspublic int fibonacci (int n) {

if (n==1) return 1;

else if (n==2) return 1;

else if (n>2) return fibonacci(n-1)+fibonacci(n-2);

else throw

InvalidInputException(“Must be positive”);

}

The Big-Oh time complexity of this method is:A. O(1)B. O(log2n)

C. O(n)D.O(n2)E. None of the above Recursion p. 14/17

Page 15: And now for something completely different: recursion.

Analyzing recursive algorithmsCan we rewrite this method without recursion?

public int fibonacci (int n) {

if (n==1) return 1;

else if (n==2) return 1;

else if (n>2) return fibonacci(n-1)+fibonacci(n-2);

else throw

InvalidInputException(“Must be positive”);

}

Recursion p. 15/17

Page 16: And now for something completely different: recursion.

Recursion: a summaryWhat are the three requirements for using

recursion?Given a problem, can you determine whether it

can be solved with recursion?Can you analyze a recursive program?

Recursion p. 16/17

Page 17: And now for something completely different: recursion.

Coming attractionsNext week, we’ll review searching and sortingThen we’ll put recursion, searching, and

sorting together in the context of our next data structure, Trees

 Homework: read chapter 8 (or equivalent)

Recursion p. 17/17