1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working on it.
2 Recursion: Compute using self-reference Iteration: Compute using loops (for, while, etc.)
3 Definition: “A phone directory is a list of names and telephone numbers in alphabetical order by surname” Searching a Phone Directory “It’s easy if there’s only one name in the book!” “But it’s hard if the book is any larger.”
4 Factorial Factorial(n) = 1 x 2 x 3 x … n Factorial(0) = 1 Iterative Definition Factorial(n) = n * Factorial(n-1) Factorial(0) = 1 Recursive Definition Textbook, pp
5 In Java public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 53
6 Tracing Recursive Programs Recursion for void methods The Towers of Hanoi
7 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = ? A: fact(n-1)= ? return= ? A For each method call, make a box with space for: Values of method’s parameters and local variables Return values of all called methods Return value for this method Box Tracing
8 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 3 A: fact(n-1)= ? return= ? A
9 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A
10 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n= 1 A: fact(n-1)= ? return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A
11 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= ? n= 1 A: fact(n-1)= ? return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A A
12 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= 1 n= 1 A: fact(n-1)= 1 return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A A
13 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= 1 n= 1 A: fact(n-1)= 1 return= 1 n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= 1 return= ? A A A A
14 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= 1 n= 1 A: fact(n-1)= 1 return= 1 n = 3 A: fact(n-1)= 2 return= ? n= 2 A: fact(n-1)= 1 return= 2 A A A A
15 Proving recursive algorithms 1.Prove each base cases works 2.Prove each recursive case works* 3.Prove all recursive calls meet a base case public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Proof by induction Textbook, p
16 Tracing Recursive Programs Recursion for void methods The Towers of Hanoi
17 Reversing a String Textbook, p. 59ff god “ ”
18 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing
19 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing Otherwise, Output last character Reverse substring of first (n-1) characters
20 Reversing a String Formal Box Trace Textbook, p. 62 god reverse “ ” If string is empty do nothing Otherwise, Output last character Reverse substring of first (n-1) characters
21 More than one way …. Textbook, p. 63ff If string is empty do nothing Otherwise, Reverse substring of last (n-1) characters Output first character
22 Invariants public static int fact( int n ) { if ( n == 0 ) { return 1; } else { // Invariant: return n * fact( n-1 ); } Textbook, p. 56
23 Loop invariants Is there something that we want to be true every time the while test is executed? // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; }
24 Loop invariants sum contains the sum of the first j items j is in the range 0..n-1 Textbook, p. 12 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; }