Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | silvester-allen |
View: | 228 times |
Download: | 2 times |
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 15
*Recursive Algorithms
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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);}
}
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example1: 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Preliminaries
* 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();
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Directory Listing*For each file in the directory
•if it is a file•print it out
•otherwise it is a subdirectory•do a directory listing
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 2: 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Anagram Algorithm
*How to generate all the anagrams of a word by using recursion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 3: 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Towers of Hanoi with N=4 disks
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recursion vs. Iteration*Loops and recursion have similar results
*Any iterative algorithm could be expressed recursively•processing all the elements of a
list is equivalent to •process the first element•process the rest of the list
* Iteration is more efficient*Recursion requires a new entry on the run-time stack for each recursive call
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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);
}}
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fibonacci
*Recursive calls to compute fibonacci(5).
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recursive Fibonacci
*This method is succinct and easy to understand, but it is very inefficient. A nonrecursive version is just as easy to understand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Non-Recursive 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 next Fib. no. fibN1 = fibN2; fibN2 = fibN; cnt++;
} return fibN; }}
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.