R ECURRSION Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter 04
R ECURSION D EFINITION Recursion is a powerful concept that helps to simplify the solution of complex problems. Recursion means defining something in terms of itself. This means that the solution of a problem is expressed in terms of a similar problem but simpler. That is, solving the simpler problem leads to the solution of the original one. Recursive solutions are shorter, easier to understand and implement. 2 Simab Shahid UOH, Girls Branch
R ECURSIVE M ETHODS o A recursive method is a method that calls itself directly or indirectly. o A recursive method has two major steps: 1. recursive step in which the method calls itself 2. base step which specifies a case with a known solution o The method should select one of two steps based on a criteria: Example: recursive step: fact(n) = n * fact(n-1) base step: fact(0) = 1 3 Simab Shahid UOH, Girls Branch
R ECURSIVE M ETHODS o The recursive step provides the repetition needed for the solution and the base step provides the termination. o Executing recursive algorithms goes through two phases: 1. Expansion in which the recursive step is applied until hitting the base step 2. “Substitution” in which the solution is constructed backwards starting with the base step 4 Simab Shahid UOH, Girls Branch
R ECURSIVE M ETHODS recursive step: fact(n) = n * fact(n-1) base step: fact(0) = 1 fact(4)= 4 * fact(3) = 4 * (3 * fact(2)) = 4 * (3 * (2 * fact(1))) = 4 * (3 * (2 * (1 * fact(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24 5 Simab Shahid UOH, Girls Branch
C ONSTRUCTING RECURSION o To construct a recursive algorithm you have to find out: Recursive step Base step o A selection structure is then used to determine which step to take. 6 Simab Shahid UOH, Girls Branch
G ENERAL A LGORITHM if (stopping condition) then solve simple problem (base) else use recursion to solve smaller problem combine solutions from smaller problem 7 Simab Shahid UOH, Girls Branch
B ENEFITS OF RECURSION o Recursive methods are clearer, simpler, shorter, and easier to understand. o Recursive programs directly reflect the abstract solution strategy (algorithm). 8 Simab Shahid UOH, Girls Branch
W HEN TO USE RECURSION o The problem definition is recursive. o The problem is simpler to solve recursively. o When the produced results are used in the reverse order of their creation. 9 Simab Shahid UOH, Girls Branch
W HEN NOT TO USE RECURSION o The recursion can be replaced with only a loop. o Run -Time or space limitation. 10 Simab Shahid UOH, Girls Branch
I NFINITE RECURSION o Recursion resembles loops in that it terminates based on the condition. o Missing the condition leads to infinite recursion. o The recursive step must introduce a simpler version of the problem leading to the base. o Infinite recursion occurs as a result of not introducing simpler problem. 11 Simab Shahid UOH, Girls Branch
R ECURSION REMOVAL o Recursion can be removed by replacing the selection structure with a loop o If some data need to be stored for processing after the end of the recursive step, a data structure is needed in addition to the loop. o The data structure vary from a simple string or an array to a stack. 12 Simab Shahid UOH, Girls Branch
E XAMPLE 01(R ECURSION ) 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 Simab Shahid UOH, Girls Branch
E XAMPLE 02(R ECURSION ) 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); } 14 Simab Shahid UOH, Girls Branch
E XAMPLE 02( I TERATIVE ) public static void dec2bin(int n){ String binary =""; while ( n >= 2 ){ binary = n%2 + binary; n= n / 2; } binary = n+binary; System.out.print(binary); } 15 Simab Shahid UOH, Girls Branch
E XAMPLE Write a recursive method that has one parameter n of type int and that returns the nth Fibonacci number. The Fibonacci numbers are F 0 is 0,F 1 is 1,F 2 is 1,F 3 is 2 and in general F n = Fn-1 +f n-2,f 0 =0, F 1 =1. Call the method in Test class that has the main method to print the Fibonacci for numbers “1 to 10” using for loop. Base Step: Fib (n)=n if n=0 or 1 Recursive Step: Fib(n)=Fib(n-2)+Fib(n-1), 16 Simab Shahid UOH, Girls Branch
class Fib { public static int Fib( int n ) { if (n<2 ) return n; else return (Fib(n-2)+Fib(n-1)); } Public static void main( String[] args){ { for ( int i = 0; i < 11; i ++) { System.out.println(i + "th Fibonacci number:of " + i + " is “+Fib( i )); } 17 Simab Shahid UOH, Girls Branch
Output: 0th Fibonacci number of 0 is 0 1th Fibonacci number of 1 is 1 2th Fibonacci number of 2 is 1 3th Fibonacci number of 3 is 2 4th Fibonacci number of 4 is 3 5th Fibonacci number of 5 is 5 6th Fibonacci number of 6 is 8 7th Fibonacci number of 7 is 13 8th Fibonacci number of 8is 21 9th Fibonacci number of 9is34 18 Simab Shahid UOH, Girls Branch