Recursion: The Mirrors Recursion is an algorithm. An alogorithm is a process - a set of instructions for accomplishing a task. Recursion occurs when a method calls itself. To construct a recursive solution: 1. Define the problem in terms of a smaller problem of the same type 2. Identify how the recursive call diminishes the size of the problem 3. Identify the base case, the point at which the problem no longer diminishes 4. Make certain that the base case can be reached Factorial of n: A Factorial is computed by multiplying together all numbers from n to 1. Problem: compute n! Old Solution: int factorial(int n) { int result = 1; for ( int i=n; i>=1; i-- ) result *= i; return result; } New Solution: 1. Define the problem in terms of a smaller problem of the same type n! = n * (n-1)! 5! = 5 * 4! 2. Identify how the recursive call diminishes the size of the problem n is reduced by 1 3. Identify the base case, the point at which the problem no longer diminishes n = 1 4. Make certain that the base case can be reached for all positive n, yes int factorial(int n) { if ( n == 1 ) return 1; else return n * factorial(n-1); } Triangular Numbers: (Applet) A Triangular Number is computed by adding together all numbers from n to 1. Representing the numbers as units, one number to a row, forms a triangle If you compute the triangle value of 5, you get 15 (5 + 4 + 3 + 2 + 1) x x x x x x x x x x x x x x x Problem: compute t(n) Old Solution: int triangle(int n) { int result = 1; for ( int i=n; i>=1; i-- ) result += i; return result; } New Solution: 1. Define the problem in terms of a smaller problem of the same type t(n) = 2. Identify how the recursive call diminishes the size of the problem 3. Identify the base case, the point at which the problem no longer diminishes 4. Make certain that the base case can be reached int triangle(int n) { } Binary Search: (Applet) A binary search is accomplished by dividing a region in half and determining which half the value is in. This process is repeated for each half until there is only one value in the region. int binarySearch(const int a[]. int first, int last, int value) { if ( first > last ) return -1; else { int mid = (first + last)/2; if ( value == a[mid] ) return mid; else if ( value < a[mid] ) return binarySearch(a, first, mid-1, value); else return binarySearch(a, mid+1, last, value); } } Problem: Find a specific value in an ordered list Recursive Solution: 1. Define the problem in terms of a smaller problem of the same type 2. Identify how the recursive call diminishes the size of the problem 3. Identify the base case, the point at which the problem no longer diminishes 4. Make certain that the base case can be reached Searching a Directory Problem: List all files in a given directory, including any files in a sub-directory Pseudocode: void listFiles(dirName) { for each file in the directory print the filename for each sub-directory in the directory listFiles(sub-directory) } Recursive Solution: 1. Define the problem in terms of a smaller problem of the same type 2. Identify how the recursive call diminishes the size of the problem 3. Identify the base case, the point at which the problem no longer diminishes 4. Make certain that the base case can be reached Anagrams Problem: Find all possible arrangements of letters in a word Recursive Solution: 1. Define the problem in terms of a smaller problem of the same type 2. Identify how the recursive call diminishes the size of the problem 3. Identify the base case, the point at which the problem no longer diminishes 4. Make certain that the base case can be reached Geneology Problem: Given a person, trace the family tree of that person by identifying all ancestors Recursive Solution: 1. Define the problem in terms of a smaller problem of the same type 2. Identify how the recursive call diminishes the size of the problem 3. Identify the base case, the point at which the problem no longer diminishes 4. Make certain that the base case can be reached