Download presentation
Presentation is loading. Please wait.
1
Chapter 15 Recursion
2
Contents Introduction to Recursion Solving Problems with Recursion
Examples of Recursive Methods A Recursive Binary Search Method The Towers of Hanoi
3
I. Introduction to Recursion
A recursive method is a method that calls itself.
4
I. Introduction to Recursion
The message method displays the string “This is a recursive method.” and then calls itself. Each time it calls itself, the cycle is repeated. There is a problem with this method. There is no way to stop the recursive calls. This method is like an infinite loop because there is no code to stop it from repeating.
5
I. Introduction to Recursion
Like a loop, a recursive method must have some way to control the number of times it repeats.
6
I. Introduction to Recursion
Recursive.message(4); First call of the method Value of n: 4 Second call of the method Value of n: 3 Third call of the method Value of n: 2 Fourth call of the method Value of n: 1 Fifth call of the method Value of n: 0
7
I. Introduction to Recursion
The numbers of times that a method calls itself is known as the depth of recursion.
8
II. Solving Problems with Recursion
A problem can be solved with recursion if it can be broken down into successive smaller problems that are identical to the overall problem. Recursion can be powerful tool for solving repetitive problems and is an important topic in upper-level computer science course. Any problem that can be solved recursively can also be solved iteratively, with a loop.
9
II. Solving Problems with Recursion
In fact, recursive problems are usually less efficient than iterative algorithms. This is because a method call requires several actions to be performed by the JVM. Allocating memory for parameters, variables, … These actions are called overhead. Some repetitive problems are more easily solved with recursion that with iteration. Where an iterative algorithm might result in faster execution time, the programmer might be able to design a recursive algorithm faster.
10
II. Solving Problems with Recursion
A recursive method works like this: If the problem can be solved now, without recursion, then the method solved it and returns. If the problem cannot be solved now, then the method reduces it to a smaller but similar problem and calls itself to solve the smaller problem.
11
II. Solving Problems with Recursion
First, we identify at least one case in which the problem can be solved without recursion. The base case Second, we determine a way to solve the problem in all other circumstances using recursion. The recursive case.
12
II. Solving Problems with Recursion
In the recursive case, we must always reduce the problem to a smaller version of the original problem. By reducing the problem with each recursive call, the base will eventually be reached and the recursion will stop. For example, the notation n! Represents the factorial of the number n. The factorial of a nonnegative number can be defined by If n=0 then n! = 1; If n>0 then n! = 1 x 2 x 3 x x n
13
II. Solving Problems with Recursion
Let's replace the notation n! with factorial(n): If n=0 then factorial(n) = 1 If n>0 then factorial(n) = 1 x 2 x 3 x x n The base case The recursive case If n>0 then factorial(n) = factorial(n - 1) x n
14
II. Solving Problems with Recursion
This might be implemented in a Java method: public static int factorial(int n) { if (n == 0) return 1; //Base case else return n * factorial(n – 1); }
15
II. Solving Problems with Recursion
16
II. Solving Problems with Recursion
Direct and Indirect Recursion Direct recursion Recursive methods directly call themselves. Indirect recursion Method A calls method B, which in turn calls method A. There can even be several methods involved in the recursion. Method A calls method B, which calls method C, which calls method C.
17
III. Examples of Recursion Methods
Summing a Range of Array Elements with Recursion rangeSum uses recursion to sum a range of array elements. The method takes the following arguments: An int array that contains the range of elements to be summed. An int specifying the starting element of the range An int specifying the ending element of the range
18
III. Examples of Recursion Methods
19
III. Examples of Recursion Methods
Drawing Concentric Circles Concentric circles are circles of different size, on inside another, all with a common center point. The drawCircles method uses recursion to draw the concentric circles. The n parameter holds the number of circles to draw.
21
III. Examples of Recursion Methods
Fibonacci Series The Fibonacci numbers are the following sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … Notice that after the second number, each number in the series is the sum of the two previous numbers. If n = 0 then Fib(n) = 0 If n = 1 then Fib(n) = 1 If n >= 2 then Fib(n) = Fib(n – 2) + Fib(n - 1)
22
III. Examples of Recursion Methods
23
III. Examples of Recursion Methods
Finding the Greatest Common Divisor The GCD of two positive integer numbers, x and y, is as follows: If x divides y evenly, then gcd(x, y) = y Otherwise gcd(x, y) = gcd(y, remander of x/y)
25
IV. A Recursive Binary Search Method
The values in the array are sorted in ascending order. The binary search algorithm can also be implemented recursively as follows: If the array is empty, the value is not found. Else If array[middle] equals the search value, the the value is found. Else if array[middle] is less than the search value, perform a binary search on the upper half of the array. Else if array[middle] is greater than the search value, perform a binary search on the lower half of the array.
26
IV. A Recursive Binary Search Method
28
V. The Towers of Hanoi The Towers of Hanoi is a mathematical game. The game uses three pegs and a set of discs with holes through their centers. The job is to move the discs from the first peg to the third peg. The middle peg can be used as a temporary holes. Only one disk may be move at a time. A disk cannot be placed on top of a smaller disc. All discs must be stored on a peg except while being moved.
29
V. The Towers of Hanoi The overall solution to the problem:
Move n discs from peg 1 to peg 3 using peg 2 as a temporary peg. To move n discs from peg 1 to peg 3, using peg 2 as a temporary peg: If n > 0 then Move n – 1 discs from peg 1 to peg 2, using peg 3 as a temporary peg. Move the remaining disc from peg 1 to peg 3. Move n – 1 disc from peg 2 to peg 3, using peg 1 as a temporary peg. End if
30
V. The Towers of Hanoi
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.