Download presentation
Presentation is loading. Please wait.
1
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises
2
Unit 182 Definition Recursion is a powerful concept that help 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
3
Unit 183 Recursive Methods A recursive method is a method that calls itself directly or indirectly. A recursive method has two major steps: –recursive step in which the method calls itself –base step which specifies a case with a known solution The method should select one of two steps based on a criteria Let us take factorial as an example –recursive step: fact(n) = n * fact(n-1) –base step: fact(0) = 1
4
Unit 184 Recursive Methods(cont’d) The recursive step provides the repetition needed for the solution and the base step provides the termination. Executing recursive algorithms goes through two phases: –Expansion in which the recursive step is applied until hitting the base step –“Substitution” in which the solution is constructed backwards starting with the base step
5
Unit 185 Recursive Methods(cont) 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
6
Unit 186 Constructing Recursion To construct a recursive algorithm you have to find out: –Recursive step –Base step A selection structure is then used to determine which step to take.
7
Unit 187 General Algorithm if stopping condition then – solve simple problem (base) else –use recursion to solve smaller problem –combine solutions from smaller problem
8
Unit 188 Benefits of Recursion Recursive methods are clearer, simpler, shorter, and easier to understand Recursive programs directly reflect the abstract solution strategy (algorithm)
9
Unit 189 When to Use Recursion The problem definition is recursive The problem is simpler to solve recursively When the produced results are used in the reverse order of their creation
10
Unit 1810 When not to Use Recursion The recursion can be replaced with only a loop Run-Time or space limitation
11
Unit 1811 Infinite Recursion Recursion resembles loops in that it terminates based on the condition. Missing the condition leads to infinite recursion The recursive step must introduce a simpler version of the problem leading to the base. Infinite recursion occur as a result of not introducing simpler problem
12
Unit 1812 Recursion Removal Recursion can be removed by replacing the selection structure with a loop 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. The data structure vary from a simple string or an array to a stack.
13
Unit 1813 Example 1 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
14
Unit 1814 Example 1 (Recursive) public static void dec2bin(int n){ if ( n < 2 ) System.out.print(n); else{ dec2bin(n/2); System.out.print(n%2); }
15
Unit 1815 Example1 (Iterative) public static void dec2bin(int n){ String binary =“”; while ( n >= 1 ){ binary = n%2 + binary; n /= 2; } System.out.print(binary); }
16
Unit 1816 Example: What is the output? class RecursionTest{ public static void main(String [] s){ myMethod(3); } public static void myMethod(int n){ if(n>0){ System.out.println("MIS Shabab"); myMethod(n-1); System.out.println("ICS Shabab"); myMethod(n-1); }
17
Unit 1817 Exercises 1.Write a recursive method to find the greatest common divisor (GCD) of two integer n and m. 2.Write a recursive method to find X n given the double X and the integer n. 3.Consider a Boolean array b filled with Boolean values. Write a recursive method boolean allTrue() that returns true if all values are true and returns false otherwise. 4.Write a recursive method to print all permutations of a given string.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.