Download presentation
Presentation is loading. Please wait.
Published byWillis Woods Modified over 9 years ago
1
RECURSION Go back, Jack, and do it again.
2
Recursion 2 Recursion occurs whenever "something" is defined in terms of itself. Structural recursion: when the data itself is self- referential. A class contains an instance of that class. Functional recursion: when a procedure is self- referential. A method calls itself.
3
Example 3 Problem: Write a method to compute the sum of the numbers 1 through N where N is a non-negative integer parameter. Iterative Solution 1 int sum(int n) { int sum = 0; for(int i=1; i<=n; i++) { sum = sum + i; } return sum; } Iterative Solution 2 int sum(int n) { int sum = 0; while(n > 0) { sum += n; n--; } return sum; }
4
Recursive Solution The goal is to compute: 0 + 1 + 2 +... + (N-1) + N We can define sum(n) as Recursive Solution int sum(int n) { if(n == 0) { return 0; } else { return sum(n-1)+n; } Base Case Reduction step (makes the input smaller)
5
Variable Scope and Lifetime 5 Class variables belong to Classes Always exist Instance variables belong to Objects Are “born” when the object is created and “die” when the object is destroyed by the garbage collector. Local variables belong to methods Are “born” when the method is called and “die” when the method ends. The time between the “birth” and “death” of a variable is called the “lifetime” of a variable
6
Basic Approach Establish a base case for which the solution is trivial. Execute a reduction step which makes the solution converge towards the base case. The reduction step reduces the input size (makes the problem simpler) The reduced problem is identical in nature to the larger problem. The results of solving this reduced problem can then be used to solve the larger problem
7
Greatest Common Divisor 7 Goal: Find the largest number D that evenly divides M and N. otherwise if n = 0 gcd(n, m%n) m gcd(m,n) = Base Case Reduction step (makes the input smaller) gcd(1440, 408) = gcd(408, 216) gcd(408, 216) = gcd(216, 192) gcd(216,192) = gcd(192, 24) gcd(192, 24) = gcd(24, 0) gcd(24, 0) = 24 Example Mr. Euclid of Alexandria 325 BC to 265 BC
8
Greatest Common Divisor 8 otherwise if n = 0 gcd(n, m%n) m gcd(m,n) = Base Case Reduction step (makes the input smaller) Implementation int gcd(int m, int n) { if(n == 0) { return m; } else { return gcd(n, m%n); } Mr. Euclid
9
Greatest Common Divisor 9 When a method is invoked, an activation of that method is created. Examine what occurs when executing “int z = gcd(1440, 408)” Activation 1 int gcd(int m=1440, int n=408) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 2 int gcd(int m=408, int n=216) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 3 int gcd(int m=216, int n=192) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 4 int gcd(int m=192, int n=24) { if(n == 0) { return m; } else { return gcd(n, m%n); } Activation 5 int gcd(int m=24, int n=0) { if(n == 0) { return m; } else { return gcd(n, m%n); }
10
Conversion to Binary 10 Goal: Print the binary representation of the integer N. Implementation void printBinary(int n) { if(n == 0) { return; } printBinary(n/2); System.out.print(n%2); } Algorithm to print the binary representation of N Stop if N = 0 Write a ‘1’ if N is odd and a ‘0’ if N is even Move the pen to the left one digit Print the binary representation of the integer N/2 CallOutput print(43)1 print(21)11 print(10)011 print(5)1011 print(2)01011 print(1)101011 print(0)101011
11
Watch Out! 11 Mr. Fibonacci 1170 to 1250 Goal: Compute the Nth fibonacci number if n = 11 otherwise if n = 0 fib(n-1)+fib(n-2) 0 fib(n) = Recursive Implementation int fib(int n) { if(n == 0 || n == 1) { return n; } else { return fib(n-2) + fib(n-1); }
12
Watch Out! 12 Mr. Fibonacci Goal: Compute the Nth fibonacci number Recursive Implementation int fib(int n) { if(n == 0 || n == 1) { return n; } else { return fib(n-2) + fib(n-1); } Why is this so terribly slow?
13
Reverse Printing 13 Goal: Print a string backwards! Let S be a string consisting of the characters [C 0, C 1, C 2, C 3, …, C n-2, C n-1 ] Note that Reverse(S) = [C n-1, C n-2, …, C 3, C 2, C 1, C 0 ] Reverse(C 0, C n-2 ) Reverse(S) = [C n-1, Reverse(C 0, C n-2 )] Recursive Implementation void printBackwards(String s, int k) { System.out.println(s.charAt(k)); if(k > 0) { printBackwards(s, k-1); }
14
Reversing an Array 14 Goal: Reverse the elements in an array. Let A be an array consisting of the elements [X 0, X 1, X 2, X 3, …, X n-2, X n-1 ] Note that Reverse(A) = [X n-1, X n-2, …, X 3, X 2, X 1, X 0 ] Reverse(A, X 1, X n-2 ) Reverse(A, f, l) = [X l, Reverse(A, X f+1, X l-1 ), X f ] Recursive Implementation void arrayReverse(int[] data, int first, int last) { if(first < last) { int temp = data[first]; data[first] = data[last]; data[last] = temp; arrayReverse(data, first+1, last-1); }
15
Pop Quiz! 15 Write a single recursive method to print the numbers N to 0. Make the signature be “void print(int n)”. Feeble Attempt 1 void print(int n) { System.out.println(n); print(n-1); } Feeble Attempt 2 void print(int n) { if(n > 0) return; System.out.println(n); print(n-1); } 3 rd Times the Charm void print(int n) { if(n < 0) return; System.out.println(n); print(n-1); }
16
MineSweeper Example 16 public void select(int x, int y) { if coordinate (x,y) is off of the game-board or coordinate (x,y) is already selected then return mark (x,y) as selected if coordinate (x,y) is not a bomb AND not adjacent to a bomb then select(x-1, y-1) select(x, y-1) select(x+1, y-1) select(x-1, y) select(x+1, y) select(x-1, y+1) select(x, y+1) select(x+1, y+1) } 1111B 1B2111 12B1 222 1B1 111 111 1B1 Initially – all cells are UNSELECTED Now, select cell (3,1)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.