0% found this document useful (0 votes)
11 views23 pages

3 Recurrence Relation

A recurrence relation defines a sequence where each term is a function of previous terms, exemplified by the Fibonacci series. The document discusses various methods for solving recurrence relations, including back substitution and the Master theorem, providing examples and time complexity analyses for different cases. It highlights the impact of function types on time complexity, ranging from O(n) to O(n^2) and O(n log n).

Uploaded by

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

3 Recurrence Relation

A recurrence relation defines a sequence where each term is a function of previous terms, exemplified by the Fibonacci series. The document discusses various methods for solving recurrence relations, including back substitution and the Master theorem, providing examples and time complexity analyses for different cases. It highlights the impact of function types on time complexity, ranging from O(n) to O(n^2) and O(n log n).

Uploaded by

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

Recurrence Relation

Recurrence Relation
A recurrence relation is an equation that recursively defines a
sequence where the next term is a function of the previous terms.
For example, Fibonacci series
Fn = Fn-1+Fn-2
Recurrence Relation
void fun(int n) fun (3)
{
if (n > 0) {
cout << n << " "; 3 fun(2)
fun(n - 1);
}
} 2 fun(1)
int main()
{
fun(3); 1 fun(0)
return 0;
} stop

Recursion Tree
Time=?
It can be observed from the recursion tree that there are 3 self call, and, in each call, it
takes 1 unit of time for printing. So, time taken for n=3 will 3 and for n=n, it will be n,
i.e., O(n).
Recurrence Relation
void fun(int n) T(n)
{
if (n > 0) {
cout << n << " "; 1
fun(n - 1); T(n-1)
}
}
int main()
{
fun(3);
return 0;
}

Recurrence relation

T(n)= 1+ T(n-1) if n>0


1 n=0
Solving Recurrence Relation
Recurrence relation T(n-1) = 1+T(n-2)----(i)
1+ T(n-1) if n>0 T(n-2) = 1+T(n-3)----(ii)
T(n)= 1 n=0

Use Back substitution method T(n) = k + T(n-k) --------------- (iii)

Substitute T(n-1) in main equation As we are subtracting 1 from n every


time, so at one point it will reach 0.
T(n)= [1+T(n-2)]+1 Assume n-k=0
= 2+T(n-2) So, n=k
Replace k by n in equation (iii)
If we substitute T(n-2) in main equation, then
T(n) = n + T(n-n)
T(n)= [1+T(n-3)]+2 T(n) = n+ T (0)
= 3+T(n-3) T(n) = n+1
.
. T(n) = O(n)
T(n) = k + T(n-k)
Recurrence Relation
void fun(int n) T(n)
{
if (n > 0) { 1
for(i=0; i<n; i++) n+1
cout << n << " "; n
fun(n - 1); T(n-1)
}
}

Recurrence relation

n+ T(n-1) if n>0
T(n)= 1 n=0
Solving Recurrence Relation
Recurrence relation

n+ T(n-1) if n>0
T(n)= 1 n=0
Recursion tree method

T(n) n
n T(n-1) n-1
n-1 T(n-2) n-3
n-2 T(n-3)
.
T(2) 2
Time taken 2 T(1) 1
1 T(0)
0+1+2+3+…+n-1+n
= n(n+1)/2 stop
= O(n2)
Solving Recurrence Relation
Recurrence relation
Use back substitution method
n+ T(n-1) if n>0 T(n-1) = T(n-2) + n-1 -(ii)
T(n)= 1 n=0 T(n-2) = T(n-3)+ n-2 -(iii)

T(n) = T(n-1) + n -(i)


Replace value of T(n-1) in eq. (i)

T(n)= [T(n-2)+n-1]+n
= T(n-2)+n-1+n
Replace value of T(n-2) in eq. (i)

T(n) = T(n-3)+n-2+n-1+n
.
T(n) = T(n-k)+(n-(k-1))+ (n-(k-2))+ … + (n-1) + n

At one time n-k will reach zero


n-k=0 => n=k
T(n)= T(n-n) + (n-(n-1)) + (n-(n-2)) + …+n-1 +n
= T(0)+1+2+3+…+n-1+n
= n(n+1)/2
= O(n2)
Recurrence Relation
void fun(int n) T(n)
{
if (n > 0) {
for(i=0; i<n; i=i*2) log n
cout << n << " "; log n
fun(n - 1); T(n-1)
}
}

Recurrence relation

log n+ T(n-1) if n>0


T(n)= 1 n=0
Recurrence Relation
Recurrence relation
T(n)
log n+ T(n-1) if n>0
T(n)= 1 n=0
log n T(n-1)

Time taken
log n-1 T(n-2)
0+log1+log2+log3+…+logn-1+logn
= log[1x2x3x…xn-1xn]
= log (n!) log n-2 T(n-3)
The upper bound for n! is nn .
= log (nn) T(2)
= nlog n
log 2 T(1)

Use back substitution method log 1 T(0)


and find the time complexity
Stop
Recurrence Relation
Observation about recurrence relation for decreasing function

T(n) = T (n-1) +1 O(n)


T(n) = T(n-1) + n O(n2)
T(n) = T(n-1) + log n O(n log n)

It can be observed from the above that first recurrence is repeated n times
so, time will be O(n). Similarly, for the third one log n is repeated n times
so, time taken will be n log n.
T(n) = T (n-1) + n2 ?
T(n) = T (n-2) + 1 ?
T(n) = T (n-100) + n ?

What will be time if some constant is being multiplied to decreasing function?


T(n) = 2T (n-1) + 1 ?
Recurrence Relation
Recurrence relation

2T(n-1)+1 if n>0
T(n)= 1 n=0

Use Back substitution method

T(n)= 2T(n-1)+1 (i)


T(n)= 2[2T(n-2)+1]+1
= 22 T(n-2)+2+1 (ii)

T(n) = 22 [2T(n-3)+1]+2+1
= 23 T(n-3)+ 22+2+1 (iii)

T(n) = 2k T(n-k)+ 2k-1+…+2+1 (iv)

Assume n-k=0 => n=k


T(n) = 2n T(0)+ 2n-1+ 2n-2…+2+1
= 2n + 2n-1+ … +22+2+1
= 2n+1
T(n)= O(2n)
Recurrence Relation
Observation about recurrence relation for decreasing function
T(n) = T (n-1) +1 O(n)
T(n) = T(n-1) + n O(n2)
T(n) = T(n-1) + log n O(n log n)
T(n) = 2T(n-1) +1 O(2n )
T(n) = 3T(n-1) +1 O(3n )
T(n) = 2T(n-1) +n O(n2n )
Master theorem for decreasing function
From the above analysis, we can write-
T(n) = aT(n-b) +f(n), where a>0, b>0, and f(n)= O(nk ), where k>=0
If a=1, then
T(n) = O(nk+1) or O(n*f(n))
If a>1, then
T(n) = O(an/b nk) or O(an/b *f(n))
If a<1, then
T(n) = O(nk) or O(f(n))
Recurrence Relation
Recurrence relation for dividing functions
T(n/2)+1 if n>0
T(n)= 1 n=1

Use Back substitution method


T(n)= T(n/2)+1 (i)
T(n)= [T(n/2*2)+1]+1 T(n)= O(log n)
= T(n/22 )+2 (ii)
T(n) = [T(n/ 23)+1]+2
= T(n/ 23)+3 (iii)
T(n) = T(n/ 2k)+k (iv)
Assume n/ 2k =1 => 2k =n T(n/2)+n if n>0
T(n) = T(1) +k (v) T(n)= 1 n=1
Take log
k log22 =log n
K = log n (vi) T(n)=?
Put value of k in equation (v)
Recurrence Relation
Recurrence relation for dividing functions
T(n/2)+n if n>0
T(n)= 1 n=1
Use Back substitution method
T(n)= T(n/2)+n (i)
T(n)= [T(n/2*2)+n/2]+n
= T(n/22 )+n/2+n (ii)
T(n) = [T(n/ 23)+n/4]+n/2+n
= T(n/ 23)+n/4+n/2+n (iii)
T(n) = T(n/ 2k)+ n/ 2k-1 +n/ 2k -2+…+n/2+n (iv)
Assume n/ 2k =1 => K = log n
T(n) = T(1)+ n[1/ 2k-1 +1/ 2k -2+…+1/2+1] (v)
T(n) = T(1)+ n[1+1]
T(n) = 1+2n
T(n)= O(n)
Recurrence Relation
Recurrence relation for dividing functions
2T(n/2)+n if n>0
T(n)= 1 n=1
Use Back substitution method
T(n)= 2T(n/2)+n (i)
T(n)= 2[2T(n/2*2)+n/2]+n
= 22 T(n/22 )+n + n (ii)
T(n) = 22[2T(n/ 23)+n/4]+2n
= 23T(n/ 23)+n+2n (iii)
T(n) = 2kT(n/ 2k)+ kn (iv)

Assume n/ 2k =1 => n= 2k and k = log n


T(n) = n*T(1)+ nlogn (v)
T(n) = n+ nlogn

T(n)= O(nlog n)
Recurrence Relation
Masters Theorem for Dividing Functions
T(n) = aT(n/b) +f(n), where a>=1, b>1, and f(n)= O(nk logpn), where k>=0

Determine values of logba and k. Based on the values of k and logba, there
will be three cases.

Case 1: if logba > k, then T(n)=O(nlogba)

Case 2: if logba = k, and


(i) if p>-1, then T(n)=O(nk logp+1n)
(ii) if p=-1, then T(n)=O(nk loglogn)
(iii) if p<-1, then T(n)=O(nk)

Case 3: if logba < k, and


(i) if p>=0, then T(n)=O(nk logpn)
(ii) if p<0, then T(n)=O(nk)
Recurrence Relation
Use Masters Theorem and find time complexity
1. T(n)= 2T(n/2)+1 T(n) = aT(n/b) +f(n), where a>=1, b>1, and
f(n)= O(nk logpn), where k>=0
2. T(n)= 4T(n/2)+n
Case 1: if logba > k, then O(nlogba)
3. T(n)= 8T(n/2)+n2
Case 2: if logba = k, and
4. T(n)= 8T(n/2)+ n logn
(i) if p>-1, then O(nk logp+1n)
(ii) if p=-1, then O(nk loglogn)
5. T(n)= 2T(n/2)+n
(iii) if p<-1, then O(nk)
6. T(n)= 4T(n/2)+n2logn
Case 3: if logba < k, and
(i) if p>=0, then O(nk logpn)
7. T(n)= 2T(n/2)+ n/logn
(ii) if p<0, then O(nk)
8. T(n)= 2T(n/2)+ n/log2n

9. T(n)= T(n/2)+n2
Recurrence Relation
Use Masters Theorem and find time complexity
1. T(n)= 2T(n/2)+1 T(n) = aT(n/b) +f(n), where a>=1, b>1, and
Here a=2, b=2, f(n)= O(n0 log0n) f(n)= O(nk logpn), where k>=0
So, k=0, p=0
Case 1: if logba > k, then O(nlogba)
Log22=1 and k=0, case 1 is satisfied
T(n)= O(nlog22)= O(n1) Case 2: if logba = k, and
(i) if p>-1, then O(nk logp+1n)
2. T(n)= 4T(n/2)+n
(ii) if p=-1, then O(nk loglogn)
Here a=4, b=2, f(n)= O(n1 log0n)
So, k=1, p=0
(iii) if p<-1, then O(nk)

Log24=2 and k=1, case 1 is satisfied Case 3: if logba < k, and


T(n)= O(nlog24)= O(n2) (i) if p>=0, then O(nk logpn)
(ii) if p<0, then O(nk)
3. T(n)= 8T(n/2)+n2
Here a=8, b=2, f(n)= O(n2 log0n)
So, k=2, p=0, Log28=3 and k=2, case 1 is satisfied
T(n)= O(nlog28)= O(n3)
Recurrence Relation
Use Masters Theorem and find time complexity
4. T(n)= 8T(n/2)+ n logn
T(n) = aT(n/b) +f(n), where a>=1, b>1, and f(n)= O(nk
Here a=8, b=2, f(n)= O(n logn)
logpn), where k>=0
So, k=1, p=1
log
Case 1: if logba > k, then O(n ba)
Log28=3 and k=1, case 1 is satisfied,
T(n)= O(nlog28)= O(n3) Case 2: if logba = k, and
(i) if p>-1, then O(nk logp+1n)
5. T(n)= 2T(n/2)+n (ii) if p=-1, then O(nk loglogn)
Here a=2, b=2, f(n)= O(n1 log0n) (iii) if p<-1, then O(nk)
So, k=1, p=0
Case 3: if logba < k, and
Log22=1 and k=1, case 2(i) is satisfied (i) if p>=0, then O(nk logpn)
T(n)= O(n1 log0+1n)= O(nlogn) (ii) if p<0, then O(nk)
6. T(n)= 4T(n/2)+n2logn
Here a=4, b=2, f(n)= O(n2 log0n)
So, k=2, p=0
Log24=2 and k=2, case 2(i) is satisfied
T(n)= O(n2 log1+1n)= O(n2 log2 n)
Recurrence Relation
Use Masters Theorem and find time complexity
7. T(n)= 2T(n/2)+ n/logn
T(n) = aT(n/b) +f(n), where a>=1, b>1, and f(n)= O(nk
Here a=2, b=2, f(n)= O(n/logn)
logpn), where k>=0
So, k=1, p=-1
log
Case 1: if logba > k, then O(n ba)
Log22=1 and k=1, case 2(ii) is satisfied,
T(n)= O(n1loglogn)= O(nloglogn) Case 2: if logba = k, and
(i) if p>-1, then O(nk logp+1n)
8. T(n)= 2T(n/2)+ n/log2n (ii) if p=-1, then O(nk loglogn)
Here a=2, b=2, f(n)= O(n/log2n) (iii) if p<-1, then O(nk)
So, k=1, p=-2
Case 3: if logba < k, and
Log22=1 and k=1, case 2(iii) is satisfied (i) if p>=0, then O(nk logpn)
T(n)= O(n1)= O(n) (ii) if p<0, then O(nk)
9. T(n)= T(n/2)+n2
Here a=1, b=2, f(n)= O(n2 log0n )
So, k=2, p=0
Log21=0 and k=2, case 3(i) is satisfied
T(n)= O(n2 log0n)= O(n2 )
Recurrence Relation
Find the time complexity for the root functions

T(√n)+1 if n>2
T(n)= 1 n=2
Recurrence Relation
T(√n)+1 if n>2
T(n)= 1 n=2

T(n) = T(n1/2 )+1 (i)


= [T(n1/2*2 )+1]+1
T(n) = T(n1/2^2)+2 (ii)
T(n) = T(n1/2^3 )+3 (iii)
.
.
T(n) = T(n1/2^k )+k (iv)
Assume n=2m

T(2m) = T(nm/2^k )+k


Assume T(nm/2^k )=T(2)
m/2^k =1, k= log2m
As n=2m
m= log2n
k= loglog2n
O(loglog2n)

You might also like