Download presentation
Presentation is loading. Please wait.
Published byHugo Harrell Modified over 9 years ago
1
Recursive
2
2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions and sets.
3
3 Recursively Defined Sequences Example: The sequence {a n } of powers of 2 is given by a n = 2 n for n = 0, 1, 2, …. The same sequence can also be defined recursively: a 0 = 1 a n+1 = 2a n for n = 0, 1, 2, …
4
4 Recursively Defined Functions We can use the following method to define a function with the natural numbers as its domain: 1. Specify the value of the function at zero. 2. Give a rule for finding its value at any integer from its values at smaller integers.
5
5 Constructing Recursion o To construct a recursive algorithm you have to find out: 1.Recursive step 2.Base step
6
6 Recursively Defined Functions Example: f(0) = 3 f(n + 1) = 2f(n) + 3 f(0) = 3 f(1) = 2f(0) + 3 = 2 3 + 3 = 9 f(2) = 2f(1) + 3 = 2 9 + 3 = 21 f(3) = 2f(2) + 3 = 2 21 + 3 = 45 f(4) = 2f(3) + 3 = 2 45 + 3 = 93
7
Example : Give an inductive definition of the factorial function F(n) = n!, Then compute F(5). Solution: (1)Specify the initial value of factorial function: F(0)=1. (2)Rule for finding F(n + 1) from F(n): F(n+1) = (n + 1) F(n) F(5) = 5 F(4) =5 4 F(3) =5 4 3 F(2) =5 4 3 2 F(1) =5 4 3 2 1 F(0) = 5 4 3 2 1 1=120
8
8 Recursively Defined Functions A famous example: The Fibonacci numbers f(0) = 0, f(1) = 1 f(n) = f(n – 1) + f(n - 2) f(0) = 0 f(1) = 1 f(2) = f(1) + f(0) = 1 + 0 = 1 f(3) = f(2) + f(1) = 1 + 1 = 2 f(4) = f(3) + f(2) = 2 + 1 = 3 f(5) = f(4) + f(3) = 3 + 2 = 5 f(6) = f(5) + f(4) = 5 + 3 = 8
9
9 Recursive Algorithms Example II: Recursive Fibonacci Algorithm procedure fibo(n: nonnegative integer) if n = 0 then fibo(0) := 0 else if n = 1 then fibo(1) := 1 else fibo(n) := fibo(n – 1) + fibo(n – 2) n 01234567891011 Fib(n) 01123581321345589
10
10 Recursive Algorithms Recursive Fibonacci Evaluation:f(4) f(3) f(2) f(1) f(0) f(1) f(2) f(1) f(0)
11
11 General Algorithm if (stopping condition) then solve simple problem (base) else use recursion to solve smaller problem combine solutions from smaller problem
12
12 Convert from decimal to binary This method converts an integer number to its binary equivalent. Base step: dec2bin(n) = n if n is 0 or 1 Recursive step: dec2bin(n) = dec2bin (n/2), (n mod 2) Algorithm dec2bin(n): If n < 2 Print n else dec2bin(n / 2) Print n mod 2
13
Example (Convert from decimal to binary) n=12 dec2bin(12) = dec2bin (6) print (12 mod 2 ) 0 dec2bin(6) = dec2bin (3), print (6mod 2) 0 dec2bin(3) = dec2bin (3/2), print (3 mod 2) 1 dec2bin(3/2) = 1 1 1 0 0 13
14
Example (Convert from decimal to binary) n=17 dec2bin(17) = dec2bin (17/2) print (17 mod 2 ) 1 dec2bin(8) = dec2bin (8/2), print (8 mod 2) 0 dec2bin(4) = dec2bin (4/2), print (4 mod 2) 0 dec2bin(2) = dec2bin (2/2), print (2 mod 2) 0 Dec2bin(1) = 1 1 0 0 0 1 14
15
Example (Convert from decimal to binary) n=90 Dec2bin(90) = dec2bin (90/2) print (90 mod 2 ) 0 dec2bin(45) = dec2bin (45/2) print (45 mod 2) 1 dec2bin(22) = dec2bin (22/2) print (22 mod 2) 0 dec2bin(11) = dec2bin (11/2) print (11 mod 2) 1 dec2bin(5) = dec2bin (5/2) print (5 mod 2) 1 dec2bin(2) = dec2bin (2/2), print (2 mod 2) 0 Dec2bin(1) = 1 1 0 1 15
16
16 class Method { public static void dec2bin( int n){ if ( n < 2 ) System.out.print( n ); else { dec2bin( n / 2 ); System.out.print( n % 2 ); } class Dec2Bin{ public static void main(String [] arg){ int i=10; dec2bin(i); } Output: 1010
17
17 Example : The Sum of the First N Positive Integers Definition: sum(n)= n + (n-1) + (n-2) + … + 1 for any integer n > 0 Recursive relation; sum(n)= n + [(n-1) + (n-2) + … + 1] = n + sum(n-1) Looks so nice, but how about n == 1? sum(1)= 1 + sum(0), but the argument to sum( ) must be positive Final recursive definition: sum(n) = 1 if n = 1 (Base case) = n + sum(n-1)if n > 1 (Recursive call)
18
18 Recursive Definition of sum(n) int sum(int n) { if (n == 1) return 1; else return n + sum(n-1); } n = 3 sum(n-1) = ? return ?
19
19 Box trace of sum(3) n = 3 A: sum(n-1)=? return ? n = 2 A: sum(n-1)=? return ? n = 1 return 1 n = 3 A: sum(n-1)= ? return ? n = 2 A: sum(n-1)=? return ? n = 1 return 1 Each box corresponds to a function’s activation record or stack. cout << sum(3); 1 3 3 6 Basic Recursions
20
Home Work Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b.
21
21 Computing GCD of A and B Basis for recursion GCD (a, 0) = a (base case) GCD (a, b) = GCD (b, a mod b) (recursion) Recursive method Gcd(a, b) { if (b != 0) return gcd(b, a % b); // recursion return a; // base case }
22
Computing GCD of 33 and 55 GCD (a, 0) = a (base case) GCD (a, b) = GCD (b, a mod b) (recursion) GCD (33,55) = GCD (55, 33%55) = GCD (55, 33) GCD (55,33) = GCD (33, 55%33) = GCD (33, 22) GCD (22,33) = GCD (22, 33%22) = GCD (22, 11) GCD (11,22) = GCD (11, 22%11) = GCD (11, 0) GCD (11, 0) = 11 GCD(33,55) = 11
23
Trace of recursive method: gcd gcd(33,55) gcd(55,33) gcd(33,22) gcd(22,11) gcd(11,0) return 11 Caller int x = gcd(33,55); call
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.