Вы находитесь на странице: 1из 24

Time

Complexity
Analysis of
Iterative
Algorithm &
Time Complexity Analysis of Iterative Algorithm
Recursive
Algorithm & Recursive Algorithm
Dr. Munesh
Singh

Dr. Munesh Singh

Indian Institute of Information Technology


Design and Manufacturing,
Kancheepuram
Chennai-600127

December 26, 2018


Types of Algorithm

Time
Complexity Algorithm generally classified into two categories:
Analysis of
Iterative Iterative algorithm
Algorithm &
Recursive Recursive algorithm
Algorithm

Dr. Munesh
Singh
Rules for Analysis

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
Algorithm Any program that could be written using iteration can also
Dr. Munesh
Singh
be written using recursion and vice versa
Analysis of iterative algorithm depends on number of
times loops gets executed
Analysis of recursion algorithm depends on the number of
times the function calls itself
If no iteration given in program, then the running time of
the program is constant O(1)
Rules for Analysis

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
Algorithm If loops in a program is independent, then the time
Dr. Munesh complexity will be the multiplication of total run time
Singh
if loops in a program is dependent, then unroll the loop
and consider the runtime of the last loop for time
complexity estimation
if dependent last loop increment is not sequential, then we
consider the k times iteration
if single loop in a program increment is not sequential,
then also we consider the k times iteration
Time complexity of the Algorithms

Time
Complexity
Analysis of
Iterative
Algorithm & Growth rate of function
Recursive
Algorithm

Dr. Munesh
Singh
Mathematical Series

Time
Complexity
Analysis of
Iterative Linear
Algorithm &
Recursive
Algorithm

Dr. Munesh
Singh

Quadratic Series

Cubic Series
Mathematical Series

Time
Complexity Geoemtric Series for x!=1
Analysis of
Iterative
Algorithm &
Recursive
Algorithm

Dr. Munesh
Singh Geoemtric Series for |x|

Linear Geometric Series for n>0, c!=1

Harmonic Series
Some Examples of Iterative Algorithm

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
Algorithm Algorithm
Algorithm
Dr. Munesh 1 A()
Singh 1 A()
2 {
2 {
3 int i,j;
3 int i;
4 for(i=1 to n)
4 for(i=1 to n)
5 for (j=1 to n)
5 pf(”Ram”);
6 pf(”Ram”);
6 }
7 }
Some Examples of Iterative Algorithm

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive Algorithm
Algorithm
1 A() Algorithm
Dr. Munesh
Singh 2 { 1 A()
3 int i=1,s=1; 2 {
4 while(s<=n) 3 int i;
5 i++; 4 for(i=1;i2 <=n;i++)
6 s=s+i; 5 pf(”Ram”);
7 pf(”ram”); 6 }
8 }
Some Examples of Iterative Algorithm

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive Algorithm Algorithm
Algorithm

Dr. Munesh
1 A() 1 A()
Singh 2 { 2 {
3 int i,j,k; 3 int i,j,k;
4 for(i=1;i<=n; i++) { 4 for(i=1;i<=n; i++) {
5 for(j=1;j<=i; j++){ 5 for(j=1;j<=i2 ; j++){
6 for(k=1;k<=100; k++){ 6 for(k=1;k<=n/2; k++){
7 pf(”Ram”); 7 pf(”Ram”);
8 }}} 8 }}}
Some Examples of Iterative Algorithm

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive Algorithm
Algorithm
Algorithm 1 A()
Dr. Munesh
Singh 1 A() 2 {
2 { 3 int i,j,k;
3 int i 4 for(i=n/2;i<=n; i++) {
4 for(i=1;i<=n; i=i*2) 5 for(j=1;j<=n/2; j++){
5 pf(”Ram”); 6 for(k=1;k<=n; k=k*2){
6 } 7 pf(”Ram”);
8 }}}
Some Examples of Iterative Algorithm

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive Algorithm
Algorithm
Algorithm 1 A()
Dr. Munesh
Singh 1 A() 2 {
2 { 3 int i,j,k;
3 int i 4 for(i=n/2;i<=n; i++) {
4 for(i=1;i<=n; i=i*2) 5 for(j=1;j<=n/2; j++){
5 pf(”Ram”); 6 for(k=1;k<=n; k=k*2){
6 } 7 pf(”Ram”);
8 }}}
Some Examples of Iterative Algorithm

Time
Complexity
Analysis of
Iterative
Algorithm & Algorithm
Recursive
Algorithm 1 A() Algorithm
Dr. Munesh
Singh
2 { 1 A()
3 int i 2 {
4 for(i=1;i<=n; i++) { 3 int i,j;
5 for(j=1;j<=n; j=j*2) { 4 for(i=1;i<=n; i++) {
6 for(k=1;k<=n; k=k*2) { 5 for(j=1;j<=n; j=j+i){
7 pf(”Ram”); 6 pf(”Ram”);
8 break; 7 }}
9 }}}
Recursion

Time
Complexity
Analysis of
Iterative Recursion is a process in which a function call itself
Algorithm &
Recursive directly or indirectly
Algorithm 1 A(n)
Dr. Munesh
Singh
2 {
3 if()
4 return (A(n/2)+A(n/2))
5 }
The above function has two recursive call in return
statement of the program and a one conditional check if
statement.
T(n)=C+2T(n/2)
C represent the constant time taken to check the if
condition, and 2T(n/2) is the two recursive calls
Types of Solution for Recursion

Time
Complexity
Analysis of
Iterative To solve the recursive relation of the given algorithm, we
Algorithm &
Recursive have three methods
Algorithm
1 Back Substitution
Dr. Munesh
Singh
2 Tree Method
3 Masters Method
An Example
1 A(n)
2 {
3 if(n>1)
4 return (A(n/2)+A(n/2))
5 }
T(n)=1+2T(n/2), n>1
T(1)=1, n=1 (Base condition)
Some Recursive Examples

Time
Complexity
Analysis of
Iterative
Algorithm &
Ex: 1 Ex:4
Recursive
Algorithm T(n)=c+T(n-1), n>1 T(n)=2T(n-1)-1, n>0
Dr. Munesh T(1)=1, n=1 T(1)=1 , n=1
Singh

Ex:2 Ex:5
T(n)=n+T(n-1), n>1 T(n)=T(n/2)+1, n>0
T(1)=1, n=1 T(1)=1 , n=1

Ex:3 Ex:6
T(n)=3T(n-1), n>0 T(n)=2T(n/2)+n, n>0
T(1)=1, n=1 T(1)=1 , n=1
Recursive Tree Method

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
Algorithm
Step 1: Cost of each level
Dr. Munesh Step 2: Find the Depth of tree
Singh
Step 3: Find the number of leaves
It has 3 cases
1 Cost of root node is Max
2 Cost of leaf node in Max
3 Cost of each node is same
An example
T(n)=3T(n/4)+n2
Recursive Analysis using Tree Method

Time
Complexity An Example root node max
Analysis of
Iterative Cost at each level= (3/16)i cn2
Algorithm &
Recursive Depth of the tree =n/4i
Algorithm
cn2 cn2
i=log4n
Dr. Munesh
Singh

No of Leaves:= 3i = 3log4n
c(n/4)2 c(n/4)2 c(n/4)2

2 2 2 2 2
c(n/16)2 c(n/16) c(n/16) c(n/16) c(n/16) c(n/16)
2 2
c(n/16)2 c(n/16) c(n/16)

c(1) c(1) c(1)

Total cost= cn2+(3/16)2n2 +----------+c3log4n


Some Example

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
Algorithm
An Example of Max Leave node
Dr. Munesh 4T(n/2)+n cn cn Depth = (n/2i)=1
Singh i=log2n
cn cn/2 cn/2 Level =Depth+1;
log2n+1
An Example of same root and
cn T(n/4) T(n/4) T(n/4) T(n/4)
leaves
2T(n/2)+n
1 1 1
Level=depth+1 1
Total cost =Cost at each level +No of levels
Total cost=cost of each cn*(log2n+1)
level*No of level
Master Method

Time
Complexity Provides Solution to be recurrences of the form:
Analysis of
Iterative
Algorithm &
T(n)=aT(n/b)+f(n)
Recursive
Algorithm

Dr. Munesh
Case: 1
Singh 1 Root level cost greater than f(n)

2 f(n)=O(nlogb a−∈ ) when ∈> 0 ∼ = T(n)=Θ(nlogb a )

Case: 2
1 Root level cost equal to f(n)

2 f(n)=Θ(nlogb a ) ∼
= T(n)=Θ(nlogb a logn)

Case: 3
1 Root level cost less than f(n)

2 f(n)=Ω(nlogb a+∈ ) ∼
= T(n)=f(n)
An Example

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
For Case: 1
Algorithm
T(n)=16T(n/4)+n
Dr. Munesh
Singh Compair with
T(n)=aT(n/b)+f(n)
a=16, b=4 , f(n)=n
Then nlogb a = nlog4 16 = n2
Root n2 is greater than f(n) so Case 1
f(n)=O(nlogb a−∈ ) when ∈> 0 ∼ = T(n)=Θ(nlog4 16 )
T(n)=Θ(n2 )
An Example

Time
Complexity
Analysis of
Iterative
Algorithm &
Recursive
For Case: 2
Algorithm
T(n)=2T(n/2)+n
Dr. Munesh
Singh Compair with
T(n)=aT(n/b)+f(n)
a=2, b=2 , f(n)=n
Then nlogb a = nlog2 2 = n
Root n is equal to f(n) so Case 2
f(n)=O(nlogb a ) when ∼
= T(n)=Θ(nlog2 2 logn)
T(n)=Θ(nlogn)
An Example

Time
Complexity
Analysis of
Iterative
Algorithm & For Case: 3
Recursive
Algorithm T(n)=T(9n/10)+n
Dr. Munesh
Singh
T(n)=T(n/10/9)+n
Compare with
T(n)=aT(n/b)+f(n)
a=1, b=10/9 , f(n)=n
Then nlogb a = nlog10/9 1 = n0
Root n0 is less to f(n) so Case 3
∼ T(n)=f(n)
f(n)=Ω(nlogb a+∈ ) =
T(n)=Θ(n)
Some Examples

Time
Complexity
Analysis of
Problem 1 Case 3 Θ(n2 ) Problem 6 Case 3 Θ(n0 .51)
Iterative
Algorithm & T(n)=3T(n/2)+n2 T(n)=2T(n/4)+n0.51
Recursive
Algorithm

Dr. Munesh
Singh
Problem 2 Case 2 Θ(n2 logn) Problem 7 Case 1 Θ(nlg 3 )
T(n)=4T(n/2)+n2 T(n)=3T(n/2)+n

Problem 3 Case 3 Θ(2n ) Problem 8 Case 1 Θ(n)



T(n)=T(n/2)+2n T(n)=3T(n/3)+ n

Problem 4 case 1 Θ(n2 ) Problem 9 Case 1 Θ(n2 )


T(n)=16T(n/4)+n T(n)=4T(n/2)+cn

Problem 5 case 2 Θ(nlog 2 n) Problem 10 Case 3 Θ(nlogn)

Вам также может понравиться