Analyzing Iterative Algorithm
Analyzing Iterative Algorithm
Module-1[Lecture-3]
Analyzing Iterative (Non-Recursive)
Algorithms
1
OBJECTIVES
After completing this section, you will be able to
Understand the concept of Step count or Operation Count for analyzing time
complexity of Iterative algorithm.
2
3
Time complexity is expressed as a function of input size, T(n) ,
where ‘n’ is the input size.
Time complexity can be estimated using step count or operation
count. Thus, T(n) can be estimated using two types of analysis :
Apriori (or Mathematical analysis) Posteriori (or Empirical
analysis)
Apriori analysis (or Mathematical analysis)
is done before the algorithm is translated posteriori analysis is done by
to a program. The step count or the
number of executed count of the dominant
executing the program using
operations is used to estimate the running standard datasets to estimate time
time. and space.
Step count: T(n) is obtained using the step count
of the instructions.
Operation Count :
Another popular school represented by Donald E
Knuth prefer the determination of running time
based on the count of basic operations. The idea
is to use dominant operator and expressing the
complexity as the number of times the basic (or
dominant) operator is executed
4
Step Count:
The idea is to count the instructions that are used by the given algorithm to
perform the given task.
The idea is to find the step count (called steps per execution s/e) of each
instruction.
Frequency is the number of times the instruction is executed. The total
count can be obtained by multiplying the frequency and steps per execution.
Time complexity=
5
Operation Count:
The idea is to count all the operations like add, sub, multiplication and
division. Some of the operations that are typically used are assignment
operations, Comparison operations, Arithmetic operations and logical
operations. The time complexity is then given as the number of repetitions of
the basic operation as a function of input size.
Time complexity=
Some Important formulae:
7
Some Important formulae…
8
Check your progress1
Example1: Example2:
A() A()
{ {
int i,j;
for(i=1; i<=n, i++)
for(i=1;i<=n,i=i*2)
for(j=1; j<=n; j++) printf("Amity");
printf("Amity"); }
}
Example3: Example4:
Assume n>=2 A()
A() {
{
While(n>1)
for(i=1; i<=n; i++)
{ for(i=1;i<=n,i=i*2)
n=n/2; {
} printf("Amity");
} }
}
9
Check your progress1
Example5: Example6:
A() A()
{
int i,j,k;
{
for(i=1; i<=n; i++) int i,j,k,n
{ for(i=1; i<=n;i++)
for(j=1; j<=i; j++) {
{ for(j=1; j<=i2;j++)
for(k=1;k<=500;k++) {
{ for(k=1;k<=n/2;k++)
printf("Amity"); {
}
} printf("Amity");
} }
}
}
}
10
Check your progress2
Find the time complexity for the following iterative algorithm:
Q.1)
Q.2)
Q.3)
Q.4)
11
Find the time complexity for the following iterative algorithm:
Q.5)
Q.6:
12
Analyse Recursive algorithm
• Idea
Solving Recurrences
using
Recursive Recurrence Substitution Method
Program Relation Recursion tree method
Master Method
Time complexity
13
Example1:
------ T(n) Amount of work done is
nothing but number of
function calls.
For ‘n’ it makes (n+1) calls
and print ‘n’ times.
----- 1 unit For Example: n=3, we have
----- T(n-1) fun(3), fun(2), fun(1) and
fun(0) calls=> n+1calls=O(n)
----------------------------
T(n)=T(n-1)+1
Recurrence relation:
𝑇 ( 𝑛) =
{ 1 𝑛= 0
𝑇 ( 𝑛 − 1 ) +1𝑛 >0
14
Using Back substitution:
……..
15
Example2:
------ T(n)
--- 1
--- n+1
--- n
----- T(n-1)
----------------------------
T(n)=T(n-1)+2n+2=T(n-1)+O(n)
Recurrence relation:
𝑇 ( 𝑛) =
{ 1 𝑛= 0
𝑇 ( 𝑛 − 1 ) +𝑛 𝑛>0 16
Using Back substitution:
……..
17
Check your progress
Ex.1: Ex.2:
18
Important results [Decreasing function]:
1.
2.
3.
4.
5. ???
19
Analysis and Design of Algorithm(ADA)
20
OBJECTIVES
After completing this section, you will be able to
Understand recursion tree method to solve Recurrence of the form:
[Decreasing function]
[Dividing Function]
21
Recursion Tree method:
A recursion tree is a convenient way to visualize what happens when a
recurrence is iterated.
It is a pictorial representation of a given recurrence relation, which shows
how Recurrence is divided till Boundary conditions.
[Decreasing function]
22
Recursion Tree method…
23
Solving Recurrences using Recursion Tree Method
For e.g., T(n) = a T(n/b) + f(n) where a > 1 ,b > 1 and f(n) is a given
function .
T n/b T n/b
1) T(n) = 2T(n/2) + n
n
n
T n/2 T n/2
n/2 n/2 n
1 1 1 1 1 1 1
When we add the values across the levels of the recursion tree, we get a
value of n for every level.
T(n) = Ɵ (n log n)
II.
Given : T(n) = 2T(n/2) + 1
Solution : The recursion tree for the above recurrence is
1 1 2
log n 4
:
:
:
Now we add up the costs over all levels of the recursion tree, to determine
the cost for the entire tree :
n
n
n/3 2n/3
n/3 2n/3 n
log3 n n 2 n 1 2n log3/2 n n .
32 3 3 3 3 (3/2)2
n
1 n/3i
:
:
:
When we add the values across the levels of the recursion tree , we get a
value of n for every level.
we have 1 when n = 1
3i
=> n = 3i
Taking log₃ on both the sides
=> log₃ n = i
Where
Case1
Case2
Case3
32
Dividing functions
Example1:
------ T(n)
----- 1 unit
----- T(n/2)
----------------------------
T(n)=T(n/2)+1
Recurrence relation:
33
Example2:
------ T(n)
--- 1
--- n+1
--- n
----- T(n/2)
----- T(n/2)
-------------------------
Recurrence relation:
34
Master Method [for Dividing function]:
Where
Method: Calculate the value of and k then compare and decide case
Case1
Case2
(i) then
(ii) then
(iii) then
Case3
(ii) then
35
Master Method [for Dividing function]:
Sr Recurrence Relation Solution
No
1.
, case1:
2.
, case1:
3. k=1, p=0)
, case2(i):
36
Master Method [for Dividing function]:
Sr Recurrence Relation Solution
No
4.
,case2(ii):
5.
case2(iii):
6. k=2, p=0)
, case3(i):
7. k=2, p=)
, case3(ii):
37
Root function
Void fun(int n)
{
if(n>2)
{
stmt;
fun(
}
}
Recurrence relation:
Hint: Using Change of variable and then master method, we can solve such
recurrences
38