chapter 15 recursive algorithms. 2 recursion recursion is a programming technique in which a method...

22
Chapter 15 Recursive Algorithms

Post on 21-Dec-2015

263 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Chapter 15

Recursive Algorithms

Page 2: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

2

Recursion

• Recursion is a programming technique in which a method can call itself to solve a problem

• A recursive definition is one which uses the word or concept being defined in the definition itself; when defining an English word, a recursive definition usually is not helpful

Page 3: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Basic Elements of Recursion

• A recursive method is a method that contains a statement that makes a call to itself.

• Any recursive method will include the following three basic elements:– A test to stop or continue the recursion.

– An end case that terminates the recursion.

– A recursive call that continues the recursion.

Page 4: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

4

Recursive Definitions

• Mathematical formulas often are expressed recursively

• N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive

• This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)!• The concept of the factorial is defined in terms of

another factorial until the base case of 1! is reached

Page 5: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

5

Recursive Definitions

5!

5 * 4!

4 * 3!

3 * 2!

2 * 1!

1

2

6

24

120

Page 6: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

6

Infinite Recursion

• All recursive definitions must have a non-recursive part– If they don't, there is no way to terminate the recursive

path– A definition without a non-recursive part causes infinite

recursion

• This problem is similar to an infinite loop with the definition itself causing the infinite "loop"

• The non-recursive part often is called the base case

Page 7: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Recursive Factorial Method

• Factorial: n! = n*(n-1)*(n-2)* … * 2 * 1

public int factorial(int N){if (N == 1){

return 1;}else {

return N * factorial(N-1);}

}

Page 8: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Recursive Directory Listing

• Recursive algorithms may be used for nonnumerical applications.

• This example of a recursive algorithm will list the file names of all files in a given directory of a hard disk and its subdirectories.

Page 9: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Directory Listing

• We create a new File object by passing the name of a file or a directory:

• File file = new File(“D:/Java/Projects”);

• To get an array of names of files and subdirectories, we use the list method.

• String[] fileList = file.list();

Page 10: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Anagram

• 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 11: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Anagram Algorithm

• How to generate all the anagrams of a word by using recursion.

Page 12: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Finding Anagrams

• 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 13: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Anagrams

• 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 14: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

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 15: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Towers of Hanoi with N=4 disks

Page 16: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

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 17: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Recursive Sorting Algorithms

• There are two commonly used sorting methods that use recursion– Merge sort: sort each half of the array and

merge the sorted halves together– Quicksort: use a pivot value to separate the

array into two parts

• Both these methods are more efficient that the sorts we’ve seen before– N log N instead of N2

Page 18: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

When Not to Use Recursion

• Recursion does not always provide an efficient or natural way to express the solution to a problem.public int fibonacci(int N){if (N == 0 )|| N ==1){

return 1; //end case}else{ //recursive case

return fibonacci(N-1) + fibonacci(N-2);

}}

Page 19: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Fibonacci

• Recursive calls to compute fibonacci(5).

Page 20: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Recursive Fibonacci

• This method is succinct and easy to understand, but it is very inefficient. A nonrecursive version is just as easy to understand.

Page 21: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

Non-Recursive Fibonacci

public 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 next Fib. no. fibN1 = fibN2; fibN2 = fibN; cnt++;

} return fibN; }}

Page 22: Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition

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