2. 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.
2
3. 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
3
4. 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.
4
5. 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
and conquer algorithm
6. 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
6
9. 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
9
10. 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)
10
11. 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
else
n
f
n
n
n
f
)
1
(
0
if
1
)
(
11
12. 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
12
13. 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
13
14. 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
of the recursive calls
15. 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
15
16. 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
16
17. What is a recurrence relation? (Cont
!!!)
Recurrence relations are useful for express
the
running times (i.e., the umber of ba
operations
executed) of recursive algorithms
The specific values of the constants such as
b, and c
(in the previous slide) are important
determining
the exact solution to the recurrence. Of
however we
17
18. 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
public void f (int n) {
if (n > 0) {
cout<<n;
f(n-1);
} }
18
19. 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
19
20. 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
public int g(int n) {
if (n == 1)
return 2;
else
return 3 * g(n / 2) + g( n / 2) +
5;
}
Forming Recurrence Relations
(Cont !!!)
20
21. 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
21
22. 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
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 !
22
23. 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 !!!)
23
24. 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
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 !
24
25. 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
Forming Recurrence Relations (Cont !
25
26. 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
26
27. 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.
27
28. In Next Lecturer
In next lecture, We will discuss the
arithmetic and geometric series and its sum
and also mathematical induction.
28