0% found this document useful (0 votes)
32 views10 pages

4.1 Recursion

lecture note of CSC508 data structure

Uploaded by

2023217748
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)
32 views10 pages

4.1 Recursion

lecture note of CSC508 data structure

Uploaded by

2023217748
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/ 10

RECURSION

1
TOPIC 4

Content

Introduction to recursion concept


Tracing recursion methods
Recursive Type
Designing recursive method

2
What is Recursion?

• An alternative in writing iterative method

public static int sum(int n){


int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
return sum;
}

public static int sum(int n){


if(n == 0)
return 0;
else
return sum(n – 1) + n;
}

Recursion Or Iteration?

▪ Two ways to solve repetitive problems


▪ 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

4
Recursive Definition

Solution to a problem is expressed in terms of a


smaller version of the problem itself.

Recursive methods is a method that calls itself

{
0 n=0
Sum(n) =
Sum(n-1) + n n>0

Recursive Definition

▪A recursive method
contains: public static int sum(int n){
i) Base case if(n == 0)
return 0;
▪ Case in recursive
else
definition in which the
return sum(n – 1) + n;
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.

6
Tracing A Recursive Method

• Recursive method After completing recursive


▪ Has unlimited copies of call:
itself ▪ Control goes back to calling
▪ Every recursive call has environment.
▪ its own code ▪ Recursive call must
▪ own set of parameters execute completely
▪ own set of local variables before control goes back to
previous call.
▪ Execution in previous call
begins from point
immediately following
recursive call.

Tracing Example

sum(5) = sum(4) + 5 = 10 + 5 = 15
sum(4) = sum(3) + 4 = 6 + 4 = 10
sum(3) = sum(2) + 3 = 3 + 3= 6
sum(2) = sum(1) + 2 = 1 + 2 = 3
sum(1) = sum(0) + 1 = 0 + 1 = 1
sum(0 = return 0 Exercise:

Trace the following recursion

factorial(n) = factorial(n-1) * n
factorial(1) = 1

when n = 4.

8
Recursive Type

▪ Directly recursive : a method that calls itself

public static int xyz(int n){


...
xyz(n – 1);
}

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

public static int abc(int m){ public static int xyz(int n){
... ...
xyz(m – 2); abc(n – 1);
} }

Recursive Type

▪ Tail recursive method: recursive method in which the last


statement executed is the recursive call.

public static int xyz(int n){


...
return xyz(n – 1);
}

▪ Infinite recursion: the case where every recursive call results in


another recursive call.

public static int xyz(int n){


...
m = m + xyz(n + 1);
System.out.println(m);
}

10
Designing Recursive Methods

1. Understand problem requirements


2. Determine limiting conditions
3. Identify base cases
4. Provide direct solution to each base case
5. Identify general case(s)
6. Provide solutions to general cases in terms of smaller
versions of itself

12

Example: Factorial Computation

1. Understand problem requirements

“ The factorial of a positive integer is the number multiplied by every


positive integer less than itself”

2. Determine limiting conditions

3! = 3 * 2! = 6
2! = 2 * 1! = 2
1! = 1 * 0! = 1
0! = 1

13
Example: Factorial Computation
0! = 1
3. Identify base cases 1! = 1

4. Provide direct solution to each base case


if( no == 0 || no ==1)
return 1;

5. Identify general case(s) 4!= 4 * 3! = 24


3! = 3 * 2! = 6
2! = 2 * 1! = 2

6. Provide solutions to general cases in terms of smaller versions


of itself
else
return no * factorial(no -1);

14

Factorial Recursive Method

public static int fact(int num){


if(num == 0 || num == 1)//base case
return 1;
else// general or recursive case
return num * fact(num – 1);
}

15
Example: Fibonacci Computation

1. Understand problem requirements

▪ Fibonacci sequence:
▪ 0,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.
2. Determine limiting conditions
first Fibonacci number is 0 and second Fibonacci number is 1

16

Example: Fibonacci Computation

3. Identify base cases


0 == 1 , 1 == 1
if( no == 0)
4. Provide direct solution to each base case
return 0;
else if(no == 1)
5. Identify general case(s) return 1;

0,1, 1, 2, 3, 5, 8, 13, 21, 34,…

6. Provide solutions to general cases in terms of smaller versions


of itself

else
return fibonacci(no - 1) + fibonacci(no - 2);

17
Fibonacci Recursive Method

public static int fibonacci(int n){


if(n == 0)//base case
return 0;
else if(n == 1) //base case
return 1;
else// general or recursive case
return fibonacci(n – 1) + fibonacci(n – 2);
}

18

Recursive Fibonacci Trace

Write the tracing for Fibonacci recursive method when Fibonacci(4).

Fibonacci(4) = Fibonacci(3) + Fibonacci(2) = 2 + 1 = 3


Fibonacci(3) = Fibonacci(2) + Fibonacci(1) = 1 + 1 = 2
Fibonacci(2) = Fibonacci(1) + Fibonacci(0) = 1 + 0 = 1
Fibonacci(1) = return 1
Fibonacci(0) = return 0

19
Other Examples
public static int largest(int list[], int lowerIndex, int upperIndex){
int max;
if(lowerIndex == upperIndex)
return list[lowerIndex];
else {
max = largest(list, lowerIndex + 1,upperIndex);
if(list[lowerIndex] >= max)
return list[lowerIndex];
else
return max;
} Largest Number in
} Array

public static void decToBin(int num){


if(num > 0){
decToBin(num/2);
System.out.println(num %2); Decimal to Binary
} Algorithm
}

20

Conclusion

Introduction to recursion concept


Tracing recursion methods
Recursive Type
Designing recursive method

22

You might also like