Chapter 3 - Recursive Algorithms
Chapter 3 - Recursive Algorithms
Recursive
12/8/2011 6
Understanding recursion
Every recursive definition has 2 parts:
12/8/2011 7
Rules for Designing Recursive
Algorithm
1. Determine the base case – is terminal case,
there is one or more terminal cases whereby
the problem will be solved and stop to call
recursive function.
2. Determine the general case – recursive call
by reducing the size of the problem
3. Combine the base case and general case into
an algorithm
12/8/2011 8
Designing Recursive Algorithm
• Recursive algorithm.
if (terminal case is reached) // base case
<solve the problem> Base case
and general
else // general case case is
< reduce the size of the problem and combined
call recursive function >
12/8/2011 9
Classic examples
• Multiplying numbers
• Find Factorial value.
• Fibonacci numbers
12/8/2011 10
Multiply 2 numbers using Addition
Method
• Multiplication of 2 numbers can be achieved
by using addition method.
• Example :
To multiply 8 x 3, the result can also be achieved
by adding value 8, 3 times as follows:
8 + 8 + 8 = 24
12/8/2011 11
Implementation of Multiply()
using loop
12/8/2011 12
Solving Multiply problem recursively
Steps to solve Multiply() problem recursively:
• Problem size is represented by variable N. In this
example, problem size is 3. Recursive function will call
Multiply() repeatedly by reducing N by 1 for each
respective call.
• Terminal case is achieved when the value of N is 1 and
recursive call will stop. At this moment, the solution for
the terminal case will be computed and the result is
returned to the called function.
• The simple solution for this example is represented by
variable M. In this example, the value of M is 8.
12/8/2011 13
Implementation of recursive function:
Multiply()
int Multiply (int M,int N)
{
if (N==1)
return M;
else
return M + Multiply(M,N-1);
}//end Multiply()
12/8/2011 14
Recursive algorithm
3 important factors for recursive
implementation:
• There’s a condition where the function will
stop calling itself. (if this condition is not
fulfilled, infinite loop will occur)
• Each recursive function call, must return to
the called function.
• Variable used as condition to stop the
recursive call must change towards terminal
case. 15
Tracing Recursive Implementation
for Multiply().
12/8/2011 16
Returning the Multiply() result
to the called function
12/8/2011 17
Factorial Problem
• Problem : Get Factorial value for a positive
integer number.
• Solution : The factorial value can be achieved
as follows:
0! is equal to 1
1! is equal to 1 x 0! = 1 x 1 = 1
2! is equal to 2 x 1! = 2 x 1 x 1 = 2
3! is equal to 3 x 2! = 3 x 2 x 1 x 1 = 6
4! is equal to 4 x 3! = 4 x 3 x 2 x 1 x 1 = 24
N! is equal to N x (N-1)! For every N>0
12/8/2011 18
Solving Factorial Recursively
1. The simple solution for this example is
represented by the factorial value equal to 1.
2. N, represent the factorial size. The recursive
process will call factorial() function
recursively by reducing N by 1.
3. Terminal case for factorial problem is when
N equal to 0. The computed result is
returned to called function.
12/8/2011 19
Factorial function
int Factorial (int N )
{ /*start Factorial*/
if (N==0)
return 1;
else
return N * Factorial (N-1);
} /*end Factorial
12/8/2011 21
Terminal case for Factorial(3)
12/8/2011 22
Execution of Factorial(3)
Return value
for
Factorial(3)
12/8/2011 23
Fibonacci Problem
• Problem : Get Fibonacci series for an integer positive.
• Fibonacci Siries : 0, 1, 1, 2, 3, 5, 8, 13, 21,…..
• Start from 0 and 1
• Every Fibonacci series is the result of adding 2 previous
Fibonacci numbers.
• Solution: Fibonacci value of a number can be
computed as follows:
Fibonacci ( 0) = 0
Fibonacci ( 1) = 1
Fibonacci ( 2) = 1
Fibonacci ( 3) = 2
Fibonacci ( N) = Fibonacci (N-1) + Fibonacci (N-2)
12/8/2011 24
Solving Fibonacci Recursively
1. The simple solution for this example is
represented by the Fibonacci value equal to
1.
2. N, represent the series in the Fibonacci
number. The recursive process will integrate
the call of two Fibonacci () function.
3. Terminal case for Fibonacci problem is when
N equal to 0 or N equal to 1. The computed
result is returned to the called function.
12/8/2011 25
Fibonacci() function
12/8/2011 26
Implementation of
Fibonacci()
• Passing and returning value from function.
27
Infinite Recursive
• Impossible termination condition
12/8/2011 28
Infinite Recursive : Example
#include <stdio.h>
#include <conio.h>
void printIntegesr(int n);
main()
{ int number;
cout<<“\nEnter an integer value :”;
cin >> number;
printIntegers(number); 1. No condition
} satatement to
void printIntegers (int nom) stop the
{ cout << “\Value : “ << nom; recursive call.
printIntegers (nom); 2. Terminal case
} variable does
not change.
12/8/2011 29
Improved Recursive function
#include <stdio.h>
#include <conio.h> Exercise: Give the
output if the value
void printIntegers(int n); entered is 10 or 7.
main()
{ int number;
cout<<“\nEnter an integer value :”;
cin >> number;
printIntegers(number);
} condition satatement
void printIntegers (int nom) to stop the recursive
{ if (nom >= 1) call and the changes
cout << “\Value : “ << nom; in the terminal case
printIntegers (nom-2); variable are
} provided.
12/8/2011 30
Conclusion and Summary
• Recursive is a repetitive process in which an
algorithm calls itself.
• Problem that can be solved by breaking the
problem into smaller instances of problem, solve
and combine.
• Every recursive definition has 2 parts:
BASE CASE: case that can be solved directly
RECURSIVE CASE: use recursion to solve smaller sub-
problems & combine into a solution to the larger
problem
12/8/2011 31
References
1. Nor Bahiah et al. Struktur data & algoritma
menggunakan C++. Penerbit UTM, 2005
2. Richrd F. Gilberg and Behrouz A. Forouzan,
“Data Structures A Pseudocode Approach
With C++”, Brooks/Cole Thomson Learning,
2001.
12/8/2011 32