Lecture 8.
How to Form Recursive
relations
Recap
●
Asymptotic analysis helps to highlight the
order of growth of functions to compare
algorithms
●
Common asymptotic notation are of O, o,
Ω, ω and Θ.
●
Definitions of each notation uses the two
constants k (or n0 ) and c.
●
Constant k refer to the point where two
function shows same value or fro where
growth of one function start.
●
While constant c refer to the value of
multiple time growth of f(n) with respect to
g(n).
Steps to solve and analysed problem (Without using Recursive approach)
Step-1:- Take and understand problem.
– Such as find the sum of elements of an array of size N
Step-2:- Write algorithm or Pseudo code
– Use C/C++ syntax to write a pseudo code (already
discused)
Step-3:- Analyse algorithm or pseudo code (Three ways)
– Consider execution time (based on computer, not
preferred)
– Consider total steps (based on programmer style, not
preferred)
– Make a function f(n) with respect to input size n
(Preferred, for this type , follow step 4 , 5 and 6)
Step-4:- Assign run time cost to each step of algorithm/pseudo code
– c1, c2, ... Cn to all steps
Steps to solve and analysed problem (Without using Recursive approach)
Step-5:- Make a general function f(n) by
considering input size n and run time cost of algorithm.
– Such as f(n) = c1+c2*N + c3 etc
Step-6:- Calculate rate/order of growth of f(n) (i.e. Use Big O because it refer to worst case)
– Such as O(f(n)) = O(c1 +c2*N + c3 ) = O(N) – Apply rules of order of growth which are
● Remove constant, low order terms and coefficient with high
order term)
Step-7:- if you have two algorithms or pseudo
codes then use asymptotic definition O, Ω ,Θ for worst, best and average case analysis.
– Asymptotic definition will help you show the rate of
growth of a function (f(n))with respect to another function g(n)
5
Recursive Algorithms
● Recursive algorithms solve the problem by
solving smaller versions of the problem
– If the smaller versions are only a little smaller,
the algorithm can be called a reduce and
conquer algorithm
– If the smaller versions are about half the size of
the original, the algorithm can be called a divide
Analyzing Recursive Algorithms
Remember the difference between recursive algorithms and iterative algorithms
Iterative – uses a loop to do the work
Recursive – a function (method) calls itself - usually involves a base case
- recursive call is on a “simpler” set of data
Analyzing Recursive Algorithms
Iterative:
Int power (int a, int p) {result = 1;
For(i=1;i<=p;i++)
result = result * a; Return result}
Analyzing Recursive Algorithms
Recursive:
int power( int a, int p) calling point:- Res=power(3,2)
{
If(p == 1) return a Else
return a*power(a, p-1); }
Analyzing Recursive Algorithms
1. Check to see if the problem is small enough
to solve directly
2. If not, then divide the data into smaller
problems
3. Call the function on the smaller sets of data
and form partial solutions
4. Combine the partial solutions to form the
final solution
Analyzing Recursive Algorithms
A recursive function to find the factorial of a number
void Factorial(N) calling point:- Factorial(4)
if N == 1 then return 1; else
{smaller = N-1;
answer = Factorial(smaller) return (N * answer)
}
The Recursion Pattern
● Recursion: when a method calls itself
● Classic example--the factorial function: – n! = 1· 2· 3· ··· · (n-1)· n
● Recursive definition:
● As a C/C++ method:
// recursive factorial function
int recursiveFactorial(int n) {
if (n == 0) return 1; // basis case
else return n * recursiveFactorial(n- 1); // recursive case
}
Visualizing Recursion
● Recursion trace
● A box for each
recursive call
● An arrow from each
calling point to called point
● An arrow from each
called point to calling point showing return value Example recursion trace: recursiveFactorial(4) recursiveFactorial(3) recursiveFactorial(2) recursiveFactorial(1) recursiveFactorial(0) return 1 call call call call
return 1*1 = 1 return 2*1 = 2 return 3*2 = 6
return 4*6 = 24 final answer
call
Recurrence Relations
Two types:
1. Only a few simple cases:
T(1) = 40 T(2) = 40
T(n) = 2T(n-2)-15 2. Several simple cases:
T(n) = 4 if n<=4
T(n) = 4T(n/2) – 1 if n>4
14
Recursive Algorithm Analysis
●
The analysis depends on
– the preparation work to divide the input
– the size of the smaller pieces
– the number of recursive calls
– the concluding work to combine the results
Content of a Recursive
function/Method
●
Base case(s).
– Values of the input variables for which we
perform no recursive calls are called base cases (there should be at least one base case).
– Every possible chain of recursive calls must eventually reach a base case.
●
Recursive calls.
– Calls to the current method.
– Each recursive call should be defined so
that it makes progress towards a base case.
What is a recurrence relation?
●
A recurrence relation, T(n), is a recursive
function of an
integer variable n.
●
Like all recursive functions, it has one or more
recursive cases and one or more base cases.
●
Example:
●
The portion of the definition that does not
contain T is called
the base case of the recurrence relation; the
portion that
contains T is called the recurrent or recursive
case.
What is a recurrence relation? (Cont
!!!)
●
Recurrence relations are useful for expressing
the
running times (i.e., the umber of basic
operations
executed) of recursive algorithms
●
The specific values of the constants such as
a
,
b
, and
c
(in the previous slide) are important in
determining
the exact solution to the recurrence. Often
however we
are only concerned with finding an asymptotic
upper
bound on the solution. We call such a bound
an
asymptotic solution
to the recurrence
.Forming Recurrence Relations
● For a given recursive method, the base case and the
recursive case of its recurrence relation correspond directly to the base case and the recursive case of the method.
● Example 1: Write the recurrence relation for the following
method:
● The base case is reached when n = = 0. The method
performs one comparison. Thus, the number of operations when n = = 0, T(0), is some constant a.
public void f (int n) { if (n > 0) {
cout<<n; f(n-1);
} }
Forming Recurrence Relations
● When n > 0, the method performs two basic operations and
then calls itself, using ONE recursive call, with a parameter n – 1.
● Therefore the recurrence relation is:
T(0) = a for some constant a
T(n) = b + T(n – 1) for some constant b
In General, T(n) is usually a sum of various choices of T(m ), the cost of the recursive subproblems, plus the cost of the
work done outside the recursive calls:
T(n ) = aT(f(n)) + bT(g(n)) + . . . + c(n)
where a and b are the number of subproblems, f(n) and g(n) are subproblem sizes, and c(n) is the cost of the work
done outside the recursive calls [Note: c(n) may be a constant]
● Example 2: Write the recurrence relation for the
following method:
● The base case is reached when n == 1. The
method performs one comparison and one return statement. Therefore, T(1), is some constant c.
public int g(int n) { if (n == 1)
return 2; else
return 3 * g(n / 2) + g( n / 2) + 5;
}
Forming Recurrence Relations
(Cont !!!)
Forming Recurrence Relations (Cont !!!)
● When n > 1, the method performs TWO recursive
calls, each with the parameter n / 2, and some constant # of basic operations.
● Hence, the recurrence relation is:
T(1) = c for some constant c
T(n) = b + 2T(n / 2) for some constant b
● Example 3: Write the recurrence relation for the following method:
● The base case is reached when n == 1 or n == 2. The
method performs one comparison and one return statement. Therefore each of T(1) and T(2) is some constant c.
long fibonacci (int n) { // Recursively calculates Fibonacci
number
if( n == 1 || n == 2)
return 1; else
return fibonacci(n – 1) + fibonacci(n – 2);
}
Forming Recurrence Relations (Cont !!!)
● When n > 2, the method performs TWO recursive calls, one
with the parameter n - 1 , another with parameter n – 2, and some constant # of basic operations.
● Hence, the recurrence relation is:
T(n) = c if n = 1 or n = 2
T(n) = T(n – 1) + T(n – 2) + b if n > 2
Forming Recurrence Relations
(Cont !!!)
● Example 4: Write the recurrence relation for the following method:
● The base case is reached when n == 0 or n == 1.
The method performs one comparison and one return statement. ThereforeT(0) and T(1) is some constant c.
long power (long x, long n) { if(n == 0)
return 1;
else if(n == 1)
return x; else if ((n % 2) == 0)
return power (x, n/2) * power (x, n/2); else
return x * power (x, n/2) * power (x, n/2); }
Forming Recurrence Relations (Cont !!!)
● At every step the problem size reduces to half the
size. When the power is an odd number, an
additional multiplication is involved. To work out time complexity, let us consider the worst case,
that is we assume that at every step an additional multiplication is needed. Thus total number of
operations T(n) will reduce to number of
operations for n/2, that is T(n/2) with seven
additional basic operations (the odd power case)
● Hence, the recurrence relation is:
T(n) = c if n = 0 or n = 1
T(n) = 2T(n /2) + b if n > 2
Forming Recurrence Relations (Cont !!!)
Summary
●
We have to use seven steps from taking a
problem scenario and to analyzing its run
time complexity. These steps are for those
problems which are attempted without
recursive approach.
●
A recursive function or method s that
method which called itself from within body
until its base condition fulfill.
●
There are two components of a recursive
method i.e. base and recursive part.
●
A recursive relation is form by considering
base condition and total recursive calls with
some constants
Summary
●
Constants for recursive relation are a, b
and c or others.
– One constant refer to cost of base condition (i.e a) – One constant refer to total recursive calling (i.e. b) – One constant refer to cost of those steps which are
performed outside the recursive calls.
● Always consider worst case analysis for recursive
relations.
In Next Lecturer
●