Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.

Similar presentations


Presentation on theme: "1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working."— Presentation transcript:

1 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 2 Recursion: Compute using self-reference Iteration: Compute using loops (for, while, etc.)

3 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 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. 51-52

5 5 In Java public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 53

6 6 Tracing Recursive Programs Recursion for void methods The Towers of Hanoi

7 7 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 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 8 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 n = 3 A: fact(n-1)= ? return= ? A

9 9 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A

10 10 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 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 11 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 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 12 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 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 13 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 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 14 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 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 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. 751-5

16 16 Tracing Recursive Programs Recursion for void methods The Towers of Hanoi

17 17 Reversing a String Textbook, p. 59ff god “ ”

18 18 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing

19 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 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 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 22 Invariants public static int fact( int n ) { if ( n == 0 ) { return 1; } else { // Invariant: return n * fact( n-1 ); } Textbook, p. 56

23 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 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++; }


Download ppt "1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working."

Similar presentations


Ads by Google