Algo VC Lecture8
Algo VC Lecture8
1
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).
2
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
3
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)
4
Recursive Algorithms
5
Analyzing Recursive Algorithms
6
Analyzing Recursive Algorithms
Iterative:
7
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);
}
8
Analyzing Recursive Algorithms
9
Analyzing Recursive Algorithms
1 if n 0
f ( n)
n f (n 1) else
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
11 }
Visualizing Recursion
Example recursion trace:
Recursion trace
return 4*6 = 24 final answer
A box for each recursive call call
recursiveFactorial(4)
An arrow from each calling call return 3*2 = 6
12
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
Recursive Algorithm Analysis
14
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.
15
What is a recurrence relation?
Example:
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:
public void f (int n) {
if (n > 0) {
cout<<n;
f(n-1);
}}
The base case is reached when n = = 0. The method performs one comparison.
18 Thus, the number of operations when n = = 0, T(0), is some constant a.
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
19 be a constant]
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
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
23
Forming Recurrence Relations (Cont !!!)
Example 4: Write the recurrence relation for the following method:
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); }
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
25
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
26
Summary
27
In Next Lecturer
In next lecture, We will discuss the arithmetic and geometric series and its sum and also mathematical induction.
28