0% found this document useful (0 votes)
12 views

Chapter 3 - Recursive Algorithms

good for revision

Uploaded by

mungai.allan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chapter 3 - Recursive Algorithms

good for revision

Uploaded by

mungai.allan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1

SCJ2013 Data Structure & Algorithms

Recursive

Nor Bahiah Hj Ahmad & Dayang


Norhayati A. Jawawi
Objectives
At the end of the class students should be able
to:

• Identify problem solving characterestics using


recursive.
• Trace the implementation of recursive
function.
• Write recursive function in solving a problem
12/8/2011 2
Introduction
• Repetitive algorithm is a process wherby a
sequence of operations is executed repeatedly
until certain condition is achieved.
• Repetition can be implemented using loop :
while, for or do..while.
• Besides repetition using loop, C++ allow
programmers to implement recursive to
replace loops.
• Not all programming language allow recursive
implement, e.g. Basic language. 3
Introduction
• Recursive is a repetitive process in which an
algorithm calls itself.
• Recursively defined data structures (like lists)
are very well-suited to be processed using
recursive procedure.
• A recursive procedure is mathematically more
elegant than one using loops. Sometimes
procedures can become straightforward and
simple using recursion as compared to loop
solution procedure. 4
Introduction
• Advantage : Recursive is a powerful problem
solving approach, since problem solving can
be expressed in an easier and neat approach.

• Drawback : Execution running time for


recursive function is not efficient compared to
loop, since every time a recursive function
calls itself, it requires multiple memory to
store the internal address of the function.
5
Recursive solution

• Not all problem can be solved using recursive.

• Recursive solve problem by:


1. breaking the problem into the same smaller
instances of problem,
2. solve each smallest problem and
3. combine back the solutions.

12/8/2011 6
Understanding recursion
Every recursive definition has 2 parts:

• BASE CASE(S): case(s) so simple that they can


be solved directly
• RECURSIVE CASE(S): more complex and make
use of recursion to:
– break the problem to smaller sub-problems and
– combine into a solution to the larger problem

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

int Multiply(int M,int N)


{ for (int i=1,i<=N,i++)
result += M;
return result;
}//end Multiply()

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

• It checks whether N is equal 0. If so, the


function just return 1.
• Otherwise, it computes the factorial of (N – 1)
and multiplies it by N.
12/8/2011 20
Execution of Factorial(3)

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

int Fibonacci (int N )


{ /* start Fibonacci*/
if (N<=0)
return 0;
else if (N==1)
return 1;
else
return Fibonacci(N-1) + Fibonacci (N-2);
}

12/8/2011 26
Implementation of
Fibonacci()
• Passing and returning value from function.

27
Infinite Recursive
• Impossible termination condition

• How to avoid infinite recursion:


– must have at least 1 base case (to terminate the
recursive sequence)
– each recursive call must get closer to a base case

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

You might also like