You are on page 1of 38

Analysis and Design of Algorithm(ADA)

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.

 Analyse Iterative algorithms

 Check the progress for analysing Iterative algorithms

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]:

Sr No. Recurrence Solution

1.

2.

3.

4.
5. ???

19
Analysis and Design of Algorithm(ADA)

Recursion tree method

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.

Recursion tree method is especially used to solve a recurrence of the form:

 [Decreasing function]

 where [Dividing Function]

22
Recursion Tree method…

In a recursion tree ,each node represents the cost of a single sub-problem


somewhere in the set of recursive problems invocations .
 we sum the cost within each level of the tree to obtain a set of
per level cost, and
 then we sum all the per level cost to determine the total cost of all levels
of recursion. That gives us a solution of given recurrence

Let us see how to solve recurrence of the form

 where [Dividing Function]

23
Solving Recurrences using Recursion Tree Method

• Here while solving recurrences, we divide the problem into


sub-problems of equal size.

For e.g., T(n) = a T(n/b) + f(n) where a > 1 ,b > 1 and f(n) is a given
function .

F(n) is the cost of splitting or combining the sub problems.

T n/b T n/b
1) T(n) = 2T(n/2) + n

The recursion tree for this recurrence is :

n
n
T n/2 T n/2
n/2 n/2 n

log2 n n/22 n/22 n/22 n/22


:
:
:

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.

We have n + n + n + …… log n times


= n (1 + 1 + 1 + …… log n times)
= n (log2 n)
= Ɵ (n log n)

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 :

We get series like


1 + 2 + 22 + 23 + …… log n times which is a G.P.

[ So, using the formula for sum of terms in a G.P. :


a + ar + ar2 + ar3 + …… + ar n – 1 = a( r n – 1 )
r – 1 ]
= 1 (2log n – 1)
2–1
= n–1
= Ɵ (n – 1) (neglecting the lower order terms)
= Ɵ (n)
III.
Given : T(n) = T(n/3) + T(2n/3) + n
Solution : The recursion tree for the above recurrence is

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.

Since the shortest path from the root to the leaf is


n → n → n → n → …. 1
3 32 33

we have 1 when n = 1
3i
=> n = 3i
Taking log₃ on both the sides
=> log₃ n = i

Thus the height of the shorter tree is log₃ n


T(n) > n log₃ n … A
Similarly, the longest path from root to the leaf is
n → 2 n → 2 2n → …1
3 3

So rightmost will be the longest


when 2 k n =1
3
or n = 1
(3/2)k
=> k = log3/2 n

T(n) < n log3/2 n … B

Since base does not matter in asymptotic notation , we guess


from A and B
T(n) = Ɵ (n log2 n)
Master Method [Decreasing function]:

Where
Case1

Case2
Case3

Example Recurrence Relation Solution


1
2
3

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

You might also like