recursive methods

48
Recursive methods

Upload: minnie

Post on 19-Jan-2016

80 views

Category:

Documents


2 download

DESCRIPTION

Recursive methods. Recursion. A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is awkward or “inelegant” Each recursive call is given a smaller portion of the original problem - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursive methods

Recursive methods

Page 2: Recursive methods

Recursion

• A recursive method is a method that contains a call to itself

• Often used as an alternative to iteration when iteration is awkward or “inelegant”

• Each recursive call is given a smaller portion of the original problem

• Last recursive call solves diminished problem without recursion

Page 3: Recursive methods

Example

// method writes digits of a non-negative number, stacked vertically

public void write_vertical(int n){

n = Math.abs(n);if (n < 10)

System.out.println(“” + n);else{

write_vertical(n / 10);System.out.println(“” + n % 10);

}}

Page 4: Recursive methods

Tracing recursive method write_vertical

1. write_vertical(52406); n=52406 n/10=5240

2. write_vertical(n/10) ; n=5240 n/10=524

3. write_vertical(n/10); n=524 n/10=52

4. write_vertical(n/10); n=52 n/10=5

5. write_vertical(n/10); n=5 Stop recursion!

Output:

6. System.out.println(“” + n);

5

2

4

0

67. System.out.println (“” + n%10);

8. System.out.println (“” + n%10);

9. System.out.println(n%10);

10. System.out.println (“” + n%10);

Page 5: Recursive methods

Elements of recursive method

• Base caseBase case: problem is simplified to the point where recursion becomes unnecessary: n < 10

• Variant expressionVariant expression: the part of the problem that changes, making the problem smaller: n

• Recursive callRecursive call: method calls itself: write_vertical (n / 10);

Page 6: Recursive methods

How recursion works

• Activation record: memory block that stores all the information a method needs to work:– values of local variables & parameters– where method should return to (so calling

method can resume execution)

Page 7: Recursive methods

How recursion works

• When a method call is encountered, execution of current method ceases

• Information for newly called method is stored in an activation record

• Method executes

Page 8: Recursive methods

How recursion works• If new method contains another method call,

the process just described repeats • Each recursive call generates its own

activation record– as each recursive call is encountered, previous

activation record is stored on run-time stack– when last call fails to generate a new activation

record, stacked calls are removed (in reverse of the order they were stored), and each process continues in succession

Page 9: Recursive methods

Remember, for a recursive method to be successful ...

• Must be a problem with one or more cases in which some subtasks are simpler versions of the original problem - use recursion for these

• Must also have one or more cases in which entire computation is accomplished without recursion (base case)

Page 10: Recursive methods

Another example: the powers method

• Suppose you wanted to find the value of Xn

• For most values of n, we can solve this iteratively using a simple loop:int answer = 1;for (int c = 1; c <= n; c++)

answer *= X;• We can take care of the cases of n=1 or

n=0 with simple if statements; but what about a negative value of n?

Page 11: Recursive methods

Finding a recursive solution

• We can observe that for any value of n, Xn is equal to X * X(n-1)

• Armed with this information, we can easily develop a recursive solution that covers all values of X and n

Page 12: Recursive methods

Recursive power method

double rpower (double X, int n){

if (X == 0)return 0;

else if (n == 0)return 1;

else if (n > 0)return X * rpower(X, n-1);

else // n < 0return 1 / rpower(X, -n);

}

Page 13: Recursive methods

Another example: Factorial

• The factorial of N is the product of the first N positive integers:

N * (N – 1) * (N – 2 ) * . . . * 2 * 1

• The factorial of N can be defined recursively as

1 if N = 1

factorial( N ) =

N * factorial( N-1 ) otherwise

Page 14: Recursive methods

Recursive Method

• An recursive method is a method that contains a statement (or statements) that makes a call to itself.

• Implementing the factorial of N recursively will result in the following method.

public int factorial( int N ) {

if ( N == 1 ) {

return 1;}else {

return N * factorial( N-1 );

}}

Test to stop or continue.

Test to stop or continue.

Recursive case: recursion continues.

Recursive case: recursion continues.

End case: recursion stops.

End case: recursion stops.

Page 15: Recursive methods

Another example: Anagram

• Anagram: A word or phrase formed by rearranging the letters of another word or phrase. For example, Elvis to Lives.

• We can use a recursive method to derive all anagrams of a given word.

• When we find the end case (an anagram), we will print it out.

Page 16: Recursive methods

Anagram• List all anagrams of a given word.

WordWord C A T

C T A

A T C

A C T

T C A

T A C

AnagramsAnagrams

Page 17: Recursive methods

Anagram Solution• The basic idea is to make recursive calls on a sub-word

after every rotation. Here’s how:

CC AA TT RecursionRecursion

AA TT CC

TT CC AA

RecursionRecursion

RecursionRecursion

Rotate Left

Rotate Left

C A T

C T A

A T C

A C T

T C A

T A C

Page 18: Recursive methods

4-letter word example

Page 19: Recursive methods

Anagram Algorithm

• We find the anagrams by rotating the positions of the letters.

• The trick is, when the method is called recursively, we pass a word with the first letter cut off. So the words being passed to successive calls are getting shorter and shorter.

• However, we must access all letters of the word in order to print it out.

Page 20: Recursive methods

Anagram Algorithm

• We solve this problem by passing two parameters: the prefix and the suffix of a word.

• In each successive call, the prefix increases by one letter and the suffix decreases by one letter.

• When the suffix becomes one letter only, the recursion stops.

Page 21: Recursive methods

Anagram Method

End caseEnd case

TestTest

Recursive caseRecursive case

public void anagram( String prefix, String suffix ) {String newPrefix, newSuffix;int numOfChars = suffix.length();

if (numOfChars == 1) {//End case: print out one anagramSystem.out.println( prefix + suffix );

} else {for (int i = 1; i <= numOfChars; i++ ) {

newSuffix = suffix.substring(1, numOfChars);newPrefix = prefix + suffix.charAt(0);anagram( newPrefix, newSuffix );//recursive call//rotate left to create a rearranged suffixsuffix = newSuffix + suffix.charAt(0);

}}

}

Page 22: Recursive methods

A classic: the Towers of Hanoi

• Initial situation: a stack of donut-shaped disks stacked in in order of decreasing size from bottom to top on a wooden peg

• Object: move all the disks to a second peg, one at a time– third peg available as temporary holding area– at no time may a larger disk be placed on top

of a smaller disk

Page 23: Recursive methods

Solving towers of Hanoi

• Simplest case: tower of one disk -- move disk to destination peg

• With stack of two disks:– Move top disk to third peg– Move next disk to destination– Move disk from third peg to destination

Page 24: Recursive methods

Solving towers of Hanoi

• Simplest case: tower of one disk -- move disk to destination peg

• With stack of two disks:– Move top disk to third peg– Move next disk to destination– Move disk from third peg to destination

Page 25: Recursive methods

Solving towers of Hanoi

• Simplest case: tower of one disk -- move disk to destination peg

• With stack of two disks:– Move top disk to third peg– Move next disk to destination– Move disk from third peg to destination

Page 26: Recursive methods

Solving towers of Hanoi

• Simplest case: tower of one disk -- move disk to destination peg

• With stack of two disks:– Move top disk to third peg– Move next disk to destination– Move disk from third peg to destination

Page 27: Recursive methods

Solving towers of Hanoi

• Simplest case: tower of one disk -- move disk to destination peg

• With stack of two disks:– Move top disk to third peg– Move next disk to destination– Move disk from third peg to destination

Page 28: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 29: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 30: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 31: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 32: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 33: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 34: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 35: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 36: Recursive methods

Solving towers of Hanoi

• Stack of 3:– Move 2 disks from 1st peg to 3rd peg, using

method already described– Move 1 disk from 1st to 2nd– Move 2 disks from 3rd to 2nd

Page 37: Recursive methods

Solving towers of Hanoi

• When there are two or more disks to move, always use third peg

• With more than two disks, we can solve the problem recursively by recognizing that the pegs are interchangeable -- that is, any peg can be used as source, destination, or “spare”

Page 38: Recursive methods

Solving towers of Hanoi

• In general, for a stack of n disks:– Move n-1 disks from peg 1 to peg 3– Move 1 disk from peg 1 to peg 2– Move n-1 disks from peg 3 to peg 2

Page 39: Recursive methods

15.4 Towers of Hanoi

• The goal of the Towers of Hanoi puzzle is to move N disks from peg 1 to peg 3:

– You must move one disk at a time.– You must never place a larger disk on top of a smaller

disk.

Page 40: Recursive methods

15.4 Towers of Hanoi

• This puzzle can be solved effectively using recursion.

• The top N-1 disks must be moved to peg 2, allowing you to then move the largest disk from peg 1 to peg 3.

• You can then move the N-1 disks from peg 2 to peg 3.

Page 41: Recursive methods

15.4 Towers of Hanoipublic void towersOfHanoi (int N, int from, int to, int spare){

if (N == 1) {moveOne(from, to);

} else {towersOfHanoi(N-1, from, spare, to);moveOne (from, to);towersOfHanoi(N-1, spare, to, from);

}}private void moveOne(int from, int to) {

System.out.println(from + “--->” + to);}

Page 42: Recursive methods

When Not to Use Recursion• When recursive algorithms are designed carelessly, it can

lead to very inefficient and unacceptable solutions.• For example, consider the following:

public int fibonacci( int N ) {

if (N == 0 || N == 1) {return 1;

} else {return fibonacci(N-1) + fibonacci(N-2);

}}

Page 43: Recursive methods

Excessive Repetition• Recursive Fibonacci ends up repeating the same

computation numerous times.

Page 44: Recursive methods

Nonrecursive Fibonaccipublic int fibonacci( int N ) {

int fibN, fibN1, fibN2, cnt;

if (N == 0 || N == 1 ) {return 1;

} else {

fibN1 = fibN2 = 1;cnt = 2;while ( cnt <= N ) {

fibN = fibN1 + fibN2; //get the next fib no.fibN1 = fibN2;fibN2 = fibN;cnt ++;

}return fibN;

}}

Page 45: Recursive methods

When Not to Use Recursion

• In general, use recursion if– A recursive solution is natural and easy to

understand.– A recursive solution does not result in excessive

duplicate computation.– The equivalent iterative solution is too complex.

Page 46: Recursive methods

Preventing infinite recursion

• One-level recursion: every case is either a stopping case or makes a recursive call to a stopping case

• Since most recursive functions are, or have the potential to be, recursive beyond just one level, need more general method for determining whether or not recursion will stop

Page 47: Recursive methods

Preventing infinite recursion

• Define a variant expressionvariant expression– numeric quantity that decreases by a fixed

amount on each recursive call– in towers of Hanoi, variant expression was

height -- each recursive call used height -1 as parameter

• Base case is when variant expression is less than or equal to its threshold valuethreshold value

Page 48: Recursive methods

Proving correctness of a recursive function

• Show that there is no infinite recursion:– define the variant expression– ensure that it reaches its threshold value

• In other words, show that the function always terminates eventually