Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cis304 RECURSION. Recursion A recursive computation solves a problem by using the solution of the same problem with simpler input For recursion to terminate,

Similar presentations


Presentation on theme: "Cis304 RECURSION. Recursion A recursive computation solves a problem by using the solution of the same problem with simpler input For recursion to terminate,"— Presentation transcript:

1 cis304 RECURSION

2 Recursion A recursive computation solves a problem by using the solution of the same problem with simpler input For recursion to terminate, there must be special cases for the simplest inputs.

3 Permutations of a String Design a class that will list all permutations of a string A permutation is a rearrangement of the letters The string "eat" has six permutations "eat" "eta" "aet" "tea" "tae"

4 Public Interface of PermutationGenerator class PermutationGenerator { public PermutationGenerator(String s) {... } public String nextPermutation() {... } public boolean hasMorePermutations() {... } }

5 File PermutationGeneratorTest.java 01: /** 02: This program tests the permutation generator. 03: */ 04: public class PermutationGeneratorTest 05: { 06: public static void main(String[] args) 07: { 08: PermutationGenerator generator 09: = new PermutationGenerator("eat"); 10: while (generator.hasMorePermutations()) 11: System.out.println(generator.nextPermutation()); 12: } 13: }

6 To Generate All Permutations Generate all permutations that start with 'e', then 'a' then 't' To generate permutations starting with 'e', we need to find all permutations of "at" This is the same problem with simpler inputs. Use recursion

7 To Generate All Permutations nextPermutaion method returns one permutation at a time PermutationGenerator remembers its state oThe string we are permuting (word) oPosition of the current character (current) oPermutationGenerator of the substring (tailGenerator) nextPermutation asks tailGenerator for its next permutation and returns word.charAt(current) + tailGenerator.nextPermutation();

8 Handling the Special Case When the tail generator runs out of permutations, we need to: o Increment the current position o Compute the tail string that contains all letters except for the current one o Make a new permutation generator for the tail string

9 File PermutationGenerator.java 01: /** 02: This class generates permutations of a word. 03: */ 04: class PermutationGenerator 05: { 06: /** 07: Constructs a permutation generator. 08: @param aWord the word to permute 09: */ 10: public PermutationGenerator(String aWord) 11: { 12: word = aWord; 13: current = 0; 14: if (word.length() > 1) 15: tailGenerator = new PermutationGenerator(word.substring(1)); 16: System.out.println("Generating " + word ); 17: }

10 18: 19: /** 20: Computes the next permutation of the word. 21: @return the next permutation 22: */ 23: public String nextPermutation() 24: { 25: if (word.length() == 1) 26: { 27: current++; 28: return word; 29: } 30: 31: String r = word.charAt(current) + tailGenerator.nextPermutation(); 32: 33: if (!tailGenerator.hasMorePermutations()) 34: { 35: current++; 36: if (current < word.length()) 37: { 38: String tailString = word.substring(0, current)

11 39: + word.substring(current + 1); 40: tailGenerator = new PermutationGenerator(tailString); 41: } 42: } 43: 44: return r; 45: } 46: 47: /** 48: Tests whether there are more permutations. 49: @return true if more permutations are available 50: */ 51: public boolean hasMorePermutations() 52: { 53: return current < word.length(); 54: } 55: 56: private String word; 57: private int current; 58: private PermutationGenerator tailGenerator; 59: }


Download ppt "Cis304 RECURSION. Recursion A recursive computation solves a problem by using the solution of the same problem with simpler input For recursion to terminate,"

Similar presentations


Ads by Google