0% found this document useful (0 votes)
60 views20 pages

Chapter 4 - Recursion

The document discusses recursion including recursive definitions, algorithms, and methods. It explains the concepts of base cases and general cases for recursive definitions and algorithms. Examples of recursive algorithms like factorials and Fibonacci numbers are provided. The differences between iterative and recursive solutions are also highlighted.

Uploaded by

2022470942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views20 pages

Chapter 4 - Recursion

The document discusses recursion including recursive definitions, algorithms, and methods. It explains the concepts of base cases and general cases for recursive definitions and algorithms. Examples of recursive algorithms like factorials and Fibonacci numbers are provided. The differences between iterative and recursive solutions are also highlighted.

Uploaded by

2022470942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1

TOPIC 3
Recursion

Zulaile Mabni
CHAPTER OBJECTIVES
▪ Learn about recursive definitions
▪ Explore the base case and the general case
of a recursive definition
▪ Discover what a recursive algorithm is
▪ Learn about recursive methods
▪ Explore how to use recursive methods to
implement recursive algorithms

2
RECURSIVE DEFINITIONS
▪ Recursion
▪ Process of solving a problem by reducing it to
smaller versions of itself.
▪ Recursive definition
▪ Definition in which a problem is expressed in
terms of a smaller version of itself.
▪ Has one or more base cases.

3
RECURSIVE DEFINITIONS
▪ Recursive algorithm
▪ Algorithm that finds the solution to a given
problem by reducing the problem to smaller
versions of itself.
▪ Has one or more base cases.
▪ Implemented using recursive methods.

▪ Recursive method
▪ Method that calls itself

4
RECURSIVE DEFINITIONS
▪ A recursive method contains:
▪ i) Base case
▪ Case in recursive definition in which the solution is
obtained directly.
▪ Stops the recursion.

▪ ii) General case


▪ Breaks problem into smaller versions of itself.
▪ Case in recursive definition in which a smaller
version of itself is called.
▪ Must eventually be reduced to a base case.

5
Recursive method
▪ Has unlimited copies of itself
▪ Every recursive call has
▪ its own code
▪ own set of parameters
▪ own set of local variables

6
After completing recursive call:
▪ Control goes back to calling environment.
▪ Recursive call must execute completely
before control goes back to previous call.
▪ Execution in previous call begins from point
immediately following recursive call.

7
RECURSIVE DEFINITIONS
▪ Directly recursive: a method that calls itself

▪ Indirectly
recursive: a method that calls another
method and eventually results in the original method
call.

▪ Tail recursive method: recursive method in which the


last statement executed is the recursive call.

▪ Infinite recursion: the case where every recursive call


results in another recursive call.

8
▪ Understand problem requirements
▪ Determine limiting conditions
▪ Identify base cases

9
▪ Provide direct solution to each base case
▪ Identify general case(s)
▪ Provide solutions to general cases in terms of smaller versions
of itself

10
▪ Examples:
▪ i) Factorial method
▪ The factorial of a positive integer is the number
multiplied by every positive integer less than itself
▪ Example:
▪ 3! = 3 * 2! = 6
▪ 2! = 2 * 1! = 2
▪ 1! = 1 * 0! = 1
▪ 0! = 1

11
ii) Fibonacci Number
▪ Fibonacci sequence:
▪ 1, 1, 2, 3, 5, 8, 13, 21, 34, ……
▪ Note:
▪ The third Fibonacci number is the sum of the first
two Fibonacci numbers.
▪ The fourth Fibonacci number in a sequence is
the sum of the second and third Fibonacci
numbers.

12
public static int fact(int num)
{
if(num == 0)//base case
return 1;
else
// general or recursive case
return num * fact(num – 1);

13
14
public static int rFibNum(int a, int b, int n)
{
if(n == 1)
return a;
else if(n == 2)
return b;
else
return rFibNum(a, b, n - 1) + rFibNum(a, b, n - 2);
}
Or
public static int fib(int n)
{
if(n == 1) // base case
return 0;
else if(n == 2) // base case
return 1;
else
return fib(n - 1) + fib(n - 2); // general case
}
15
16
▪ Two ways to solve particular problem
▪ Iteration
▪ Recursion
▪ Iteration:
▪ uses looping to repeat a set of statements
▪ Uses less memory
▪ Recursion
▪ Sometimes recursive solution is easier & simpler
▪ Recursive solution is often slower
▪ Uses up too much time and memory

17
public static int factorial(int n)
{
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}

18
CHAPTER SUMMARY
▪ Recursive Definitions
▪ Recursive Algorithms
▪ Recursive methods
▪ Base cases
▪ General cases

19
▪ Malik D.S, Nair P.S., Data Structures Using Java, Course
Technology, 2003.

▪ Koffman E., Wolfgang P., Objects, Abstraction, Data Structures


And Design Using Java, John Wiley & Sons, 2005.

20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy