Download presentation
Presentation is loading. Please wait.
Published byGeorgia Maxwell Modified over 9 years ago
1
1 Examples of Recursion Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
2
2 Sum of natural numbers class SumOfNaturalNumbers { public static void main (String arg[]) { int m = 3, n = 10; if (m > n) { System.out.println (“Invalid input!”); } else { System.out.println (“Sum of numbers from ” + m + “ to ” + n + “ is ” + Sum(m, n)); }
3
3 Sum of natural numbers public static int Sum (int m, int n) { int x, y; if (m==n) return m; x = Sum(m, (m+n)/2); y = Sum((m+n)/2+1, n); return (x+y); } } // end class
4
4 GCD revisited Recall that gcd (a, b) = gcd (a-b, b) assuming a > b. –Directly defines a recurrence public static int gcd (int a, int b) { if ((a==1) || (b==1)) return 1; if (a==b) return a; if (a < b) return gcd (a, b-a); if (a > b) return gcd (a-b, b); }
5
5 GCD revisited Refinement: gcd (a, b) = gcd (a-nb, b) for any positive integer n such that a >= nb, in particular n = a/b, assuming a > b public static int gcd (int a, int b) { if (0==a) return b; if (0==b) return a; if (a < b) return gcd (a, b%a); if (a > b) return gcd (a%b, b); }
6
6 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following constraints –Can move only the topmost disk from one peg to another in one step –Cannot place a smaller disk below a larger one An example where recursion is much easier to formulate than a loop-based solution
7
7 Towers of Hanoi We want to write a recursive method THanoi (n, 1, 2, 3) which moves n disks from peg 1 to peg 3 using peg 2 for intermediate transfers The first step is to formulate the algorithm –Observation: THanoi (n, 1, 2, 3) Ξ THanoi (n- 1, 1, 3, 2) followed by transferring the largest disk to peg 3 from peg 1 and calling THanoi (n-1, 2, 1, 3) –Stopping condition: n = 1
8
8 Towers of Hanoi class TowersOfHanoi { public static void main (String arg[]) { int n = 10; THanoi(n, 1, 2, 3); }
9
9 Towers of Hanoi public static void THanoi (int n, int source, int extra, int destination) { if (n > 1) { THanoi (n-1, source, destination, extra); } System.out.println (“Move disk ” + n + “ from peg ” + source + “ to peg ” + destination); if (n > 1) { THanoi (n-1, extra, source, destination); } } // How many moves needed? 2 n -1 } // end class
10
10 Towers of Hanoi Total number of method calls Let T n be the number of method calls to solve for n disks T n = 2T n-1 + 1 for n > 1; T 1 = 1 –Use generating function to solve the recurrence (worked out in class on board)
11
11 Fibonacci series Number of method calls –Refer to the program in the last lecture –Let T n be the number of method calls to find the n th Fibonacci number T n = T n-1 + T n-2 + 1 for n > 2; T 1 = T 2 = 1 –Use generating function to solve the recurrence –Observe that T n = F n – 1 where F n is the n th Fibonacci number T n = (2 1-n /√5)((1+√5) n – (1-√5) n ) – 1 –The number (1+√5)/2 is called the golden ratio, which is lim n →∞ (F n+1 /F n )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.