0% found this document useful (0 votes)
7 views33 pages

4-Analysis of Recursive and Iterative Algorithm-27!05!2024

Uploaded by

Gaming world
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views33 pages

4-Analysis of Recursive and Iterative Algorithm-27!05!2024

Uploaded by

Gaming world
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Analysis of Recursive

algorithms
Backward Substitution & Recursion Tree for solving
Recurrence Relations
Recursion Vs Iteration
Function(n){
if(n>1)
return(function(n-1))
}
Function(n){
if(n>1) T(1)
return(function(n-1))
}
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1); n>1 eq(1)
T(1) = 1+T(1-1); n=1
T(1) = 1+T(0);
T(1) =1

T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Substitute eq(3) in 2 T(n)=2+(1+T(n-3)) = 3+T(n-3)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Substitute eq(3) in 2 T(n)=2+(1+T(n-3)) = 3+T(n-3)
K+T(n-k) eq(4)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Substitute eq(3) in 2 T(n)=2+(1+T(n-3)) = 3+T(n-3)
K+T(n-k) eq(4)
When n-k=1 loop stop so k=n-1 substitute in eq(4)
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Substitute eq(3) in 2 T(n)=2+(1+T(n-3)) = 3+T(n-3)
K+T(n-k)
When n-k=1 loop stop so k=n-1 substitute in eq(4)
n-1+T(n-(n-1)))
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Substitute eq(3) in 2 T(n)=2+(1+T(n-3)) = 3+T(n-3)
K+T(n-k)
When n-k=1 loop stop so k=n-1 substitute in eq(4)
n-1+T(n-(n-1)))
n
Function(n){
if(n>1) T(1)
return(function(n-1)) T(n-1)
}
T(n)=1+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=1+T(n-3) eq(3)
Backward substitution
Substitute eq(2) in 1 T(n) = I+(1+T(n-2)) = 2+T(n-2)
Substitute eq(3) in 2 T(n)=2+(1+T(n-3)) = 3+T(n-3)
K+T(n-k)
When n-k=1 loop stop so k=n-1 substitute in eq(4)
n-1+T(n-(n-1)))
n
O(n)
T(n)=n+T(n-1) n>1
T(1)=1+T(1-1) n=1

T(n)=n+T(n-1) eq(1)
To find T(n-1) substitute n-1 to all n in eq(1)
T(n-1)=n-1+T(n-2) eq(2)
To find T(n-2) substitute n-2 to all n in eq(1)
T(n-2)=n-2+T(n-3) eq(3)
Backward substitution
T(n) = n+T(n-1)
Substitute eq(2) in 1 T(n) = n+(n-1)+T(n-2)
Substitute eq(3) in 2 T(n)=n+(n-1)+(n-2)+T(n-3)) ….+(n-k)+T(n-(k+1)) eq(4)
1

When n-(k+1)=1 loop stop so k=n-2 substitute in eq(4)


n-(K+1)) =1
n-k-1=1
K=n-2
T(1)
T(n)= n+(n-1)+(n-2)+T(n-3)) ….+(n- (n-2))+T(n-((n-2)+1)
= n+(n-1)+(n-2)+T(n-3)) ….+2 +1
= n(n+1)/2
Recursion Tree Method

• T(n) = 2T(n/2)+C ; n>1


• =C ; n=1
• if I do C amount of work, I could produce 2
problems with smaller sets of inputs and each input
size is reduced to n/2.
• T(n) ------- C

• T(n/2) T(n/2)
• T(n) = 2T(n/2)+C ; n>1 t(n/2)=2t(n/4)+c
• =C ; n=1t(n/4)=2t(n/8)+c
• T(n) ------- C

• T(n/2) T(n/2)

• C

• C T(n/2) similarly

• T(n/4) T(n/4)
• C
• 2C
• 4C
• 8C

• T(n/n)……….. T(n/n) nC
• T(1) = T(n/n)
• C+2C+4C+…NC
• C(1+2+4+…..n) assume n=2k (for simiplication)
• C(1+2+4+….. 2k) => c(1(2k+1 -1)) / (2-1)
• => C(2k+1 -1) => C(2n-1)

• T(n) = 2T(n/2)+n ; n>1
• =1 ; n=1
• T(n) ------- n
• T(n/2) = 2T(n/4)+n/2
• T(n/2) T(n/2) T(n/4) = 2T(n/8)+n/4

• n

• n/2 (n/2) similarly

• T(n/4) T(n/4) T(n/4) T(n/4)




• C
• 2C
• 4C
• 8C

• T(n/n)……….. T(n/n) nC
• T(1) = T(n/n)
• C+2C+4C+…NC
• C(1+2+4+…..n) assume n=2k (for simiplication)
• C(1+2+4+….. 2k) => c(1(2k+1 -1)) / (2-1)
• => C(2k+1 -1) => C(2n-1)

You might also like