230 likes | 487 Views
Recursive. Recursive Definitions. In a recursive definition , an object is defined in terms of itself. We can recursively define sequences , functions and sets. Recursively Defined Sequences. Example: The sequence {a n } of powers of 2 is given by a n = 2 n for n = 0, 1, 2, … .
E N D
Recursive Definitions • In a recursive definition, an object is defined in terms of itself. • We can recursively define sequences, functions and sets.
Recursively Defined Sequences • Example: The sequence {an} of powers of 2 is given by an= 2n for n = 0, 1, 2, … . • The same sequence can also be defined recursively: a0= 1 an+1= 2an for n = 0, 1, 2, …
Recursively Defined Functions We can use the following method to define a function with the natural numbers as its domain: • Specify the value of the function at zero. • Give a rule for finding its value at any integer from its values at smaller integers.
Constructing Recursion • To construct a recursive algorithm you have to find out: • Recursive step • Base step
Recursively Defined Functions • Example: f(0) = 3 f(n + 1) = 2f(n) + 3 • f(0) = 3 • f(1) = 2f(0) + 3 = 23 + 3 = 9 • f(2) = 2f(1) + 3 = 29 + 3 = 21 • f(3) = 2f(2) + 3 = 221 + 3 = 45 • f(4) = 2f(3) + 3 = 245 + 3 = 93
Example : Give an inductive definition of the factorial • function F(n) = n!, Then compute F(5). • Solution: • Specify the initial value of factorial function: F(0)=1. • 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
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
Recursive Algorithms Example II: Recursive Fibonacci Algorithm procedurefibo(n: nonnegative integer) ifn = 0 then fibo(0) := 0 else if n = 1 then fibo(1) := 1 elsefibo(n) := fibo(n – 1) + fibo(n – 2)
f(4) f(3) f(2) f(2) f(0) f(1) f(1) f(1) f(0) Recursive Algorithms • Recursive Fibonacci Evaluation:
General Algorithm • if (stopping condition) then • solve simple problem (base) • else • use recursion to solve smaller problem • combine solutions from smaller problem
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
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
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
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 1 0 1
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){ inti=10; • dec2bin(i); • } • } Output: 1010
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)
Recursive Definition of sum(n) intsum(int n) { if (n == 1) return 1; else return n +sum(n-1); } n = 3 sum(n-1) = ? return ?
Basic Recursions n = 2 A: sum(n-1)=? return ? n = 1 return 1 3 1 6 3 Box trace of sum(3) cout << sum(3); n = 3 A: sum(n-1)=? return ? 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.
Home Work Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b.
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 } 21
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
Trace of recursive method: gcd Caller int x = gcd(33,55); return 11 gcd(33,55) call return 11 call gcd(55,33) call return 11 gcd(33,22) call return 11 gcd(22,11) call return 11 gcd(11,0)