0% found this document useful (0 votes)
19 views64 pages

Chapter 3

The document discusses solving recurrence equations, which are recursive descriptions of functions, particularly in the context of algorithms. It outlines methods for solving these equations, including the Substitution Method (both Forward and Backward), Master's Method, and Recurrence Tree Method, providing examples for clarity. Additionally, it explains how to derive time complexity using these methods.

Uploaded by

nishitgamer0
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)
19 views64 pages

Chapter 3

The document discusses solving recurrence equations, which are recursive descriptions of functions, particularly in the context of algorithms. It outlines methods for solving these equations, including the Substitution Method (both Forward and Backward), Master's Method, and Recurrence Tree Method, providing examples for clarity. Additionally, it explains how to derive time complexity using these methods.

Uploaded by

nishitgamer0
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/ 64

UNIT-3: SOLVING RECURRENCE

Assistant Professor
IT-ICT Department, LJIET
Mr. Prayag Patel [email protected]
Recurrence Equation
▪ A Recurrence is a recursive description of a function, or
a description of a function in terms of itself.
▪ Many algorithms (Divide & conquer) are recursive in
nature.
▪ Recurrence equation is an equation that defines a
sequence recursively.
▪ Recurrence equation is normally in following form:
1) T(n) = T(n-1) + n for n > 0.
Recurrence Relation
2) T(0) = 0 Initial condition

Mr. Prayag Patel Department: IT/ICT-LJIET


Methods to Solve Recurrence
1) Substitution Method.
a) Forward Substitution Method
b) Backward Substitution Method
2) Master’s Method.
3) Recurrence tree Method.

Mr. Prayag Patel Department: IT/ICT-LJIET


Methods to Solve Recurrence
❖Summation formula and rules used in Efficiency analysis

1) 1 = 1 + 1 + 1 +……+ 1 = n

2) i = 1 + 2 + 3 +……+ n = n(n+1) /2

3) (ai + bi ) = ai + bi

4) c ai = c ai

5) 1 =n-k+1

Mr. Prayag Patel Department: IT/ICT-LJIET


Methods to Solve Recurrence
1) Substitution Method.
a) Forward Substitution Method
b) Backward Substitution Method
2) Master’s Method.
3) Recurrence tree Method.

Mr. Prayag Patel Department: IT/ICT-LJIET


Substitution Method
•In substitution method we make a guess for
the solution and then we use mathematical
induction to prove the guess is correct or
incorrect.

•Two types of Substitution Method


1) Forward Substitution Method
2) Backward Substitution Method

Mr. Prayag Patel Department: IT/ICT-LJIET


Forward Substitution Method
• One of the simplest methods for solving simple recurrence
relations is using forward substitution Substitution Method.
• Steps of Forward substitution method:
1) Take the Recurrence equation and Initial Condition.
2) Put initial condition in equation and look for the pattern.
(We solve the recurrence relation for n=0,1,2,…n until we
see a pattern. )
3) Guess the pattern.
4) Prove that Guess pattern is correct by using the induction.

Mr. Prayag Patel Department: IT/ICT-LJIET


Forward Substitution Method
Ex-1 : Consider a recurrence relation
T(n) = T(n−1) + n Recurrence Relation
T(0) = 0
Initial condition
Step-1: Take the Recurrence equation and Initial Condition.
T(n) = T(n−1) + n
T(0) = 0

Step-2 n T(n)
n=1 T(1) = T(1-1) + 1 = T(0) +1 = 0+1 = 1
Put initial condition
in equation and look n=2 T(2) = T(2-1) + 2 = T(1) +2 = 1 + 2 = 3
for the pattern. n=3 T(3) = T(3-1) + 3 = T(2) +3 = 3 + 3 = 6
n=4 T(4) = T(4-1) + 4 = T(3) +4 = 6 + 4 = 10

Mr. Prayag Patel Department: IT/ICT-LJIET


Forward Substitution Method
Step-3 Guess the pattern.
By observing above generated equations we can derive a formula
T(n) = n (n+1) / 2 = n2 + n / 2 = n2 /2 + n/2

Step- 4 Prove that Guess pattern is correct.


n T(n) = n(n+1) / 2
n=1 T(1) = 1(1+1) / 2 = 2/2 = 1
n=2 T(2) = 2(2+1) / 2 = 6/2 = 3
n=3 T(3) = 3(3+1) / 2 = 12/2 = 6
n=4 T(4) = 4(4+1) / 2 = 20/2 = 10
We can denote T(n) in terms of big oh notation like T(n) = O (n2)
Mr. Prayag Patel Department: IT/ICT-LJIET
Forward Substitution Method
Ex-2 : Consider a recurrence relation
T(n) = T(n−1) + 1 Recurrence Relation
T(0) = 0
Initial condition
Step-1: Take the Recurrence equation and Initial Condition.
T(n) = T(n−1) + 1
T(0) = 0

Step-2 n T(n)
n=1 T(1) = T(1-1) + 1 = T(0) +1 = 0+1 = 1
Put initial condition
in equation and look n=2 T(2) = T(2-1) + 1 = T(1) + 1 = 1 + 1 = 2
for the pattern. n=3 T(3) = T(3-1) + 1 = T(2) + 1 = 2 + 1 = 3
n=4 T(4) = T(4-1) + 1 = T(3) + 1 = 3 + 1 = 4

Mr. Prayag Patel Department: IT/ICT-LJIET


Forward Substitution Method
Step-3 Guess the pattern.
By observing above generated equations we can derive a formula
T(n) = n

Step- 4 Prove that Guess pattern is correct.


n T(n) = n
n=1 T(1) = 1
n=2 T(2) = 2
n=3 T(3) = 3
n=4 T(4) = 4
We can denote T(n) in terms of big oh notation like T(n) = O (n)

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
• In forward substitution method, we put n = 0,1,2,…n in the
recurrence relation until we see a pattern.
• In backward substitution, we do the opposite i.e. we put n= n,
n−1, n−2, … or n = n, n/2,n/4,… until we see the pattern.
• Steps of Backward substitution method:
1) Take the Recurrence equation and Initial Condition.
2) We solve the recurrence relation for n = n, n−1, n−2, … or
n=n, n/2,n/4,… until we see the pattern.
3) Guess the pattern.
4) Prove that Guess pattern is correct by using the induction.

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
Ex-1 : Consider a recurrence relation
T(n) = T(n−1) + n Recurrence Relation
T(0) = 0 Initial condition

Step-1: Take the Recurrence equation and Initial Condition.

T(n) = T(n−1) + n
T(0) = 0

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
Step-2 : We solve the recurrence relation for n = n, n−1, n−2, …
T(n) = T(n-1) + n

First find T(n-1) means put n = n-1

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


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

Now find T(n-2) means put n = n-2


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

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
Now find T(n-3) means put n = n-3 in T(n) = T(n-1) + n

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


= T(n-4) + (n-3) = T(n-4)2+ (n-3) + (n-2) + (n-1) + n

If we repeat this process for k times then


T(n) = T(n-4) + (n-3) + (n-2) + (n-1) + n
……
T(n) = T(n-k) + (n-k+1) + (n-k+2) + (n-k+3) +……+ n
If n = k (means put k=n) then
T(n) = T(n-n) + (n-n+1) + (n-n+2) + (n-n+3) +……+ n
= T(0) + 1 + 2 + 3 + 4 +……….+ n
= 0 + 1 + 2 + 3 + 4 +……….+ n
Mr. Prayag Patel Department: IT/ICT-LJIET
Backward Substitution Method
Step-3 : Guess the pattern
T(n) = 0 + 1 + 2 + 3 + 4 +……….+ n
= n (n+1) / 2 = (n2 + n) / 2 = n2 /2 + n/2

Step-4 : Prove that Guess pattern is correct.


T(n) = T(n-1)+n
T(n) = n (n+1) / 2
= (n-1)[(n-1)+1]/2 + n
= n (n-1)/2 + n
= n2 /2 - n/2 + n
= n2 /2 + n/2
= n (n+1) / 2
We can denote T(n) in terms of big oh notation like T(n) = O (n2)

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
Ex-2 : Consider a recurrence relation
T(n) = T(n−1) + 1 Recurrence Relation
T(0) = 0 Initial condition

Step-1: Take the Recurrence equation and Initial Condition.

T(n) = T(n−1) + 1
T(0) = 0

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
Step-2 : We solve the recurrence relation for n = n, n−1, n−2, …
T(n) = T(n-1) + 1

First find T(n-1) means put n = n-1

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


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

Now find T(n-2) means put n = n-2 in T(n) = T(n-1) + 1


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

Mr. Prayag Patel Department: IT/ICT-LJIET


Backward Substitution Method
Now find T(n-3) means put n = n-3 in T(n) = T(n-1) + 1

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


= T(n-4) + 1 2 +1+3
= T(n-4)
= T (n-4) + 4

If we repeat this process for k times then


T(n) = T(n-4) + 4
……
T(n) = T(n-k) + k
If n = k (means put k = n) then
T(n) = T(n-n) + n
= T(0) + n
=0+n =n
Mr. Prayag Patel Department: IT/ICT-LJIET
Backward Substitution Method
Step-3 : Guess the pattern
T(n) = 0 + 1 + 1 + 1 + 1 +……….+ 1
= n

Step-4 : Prove that Guess pattern is correct.


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

We can denote T(n) in terms of big oh notation like T(n) = O(n)

Mr. Prayag Patel Department: IT/ICT-LJIET


Methods to Solve Recurrence
1) Substitution Method.
a) Forward Substitution Method
b) Backward Substitution Method
2) Master’s Method.
3) Recurrence tree Method.

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method
Time to divide &
recombine

Number of sub- Time required to


problems solve a sub-problem

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method
• Consider the following recurrence relation

• If f(n) is Θ (nd) where d ≥ 0, a ≥ 1, b ≥ 2 in the recurrence relation then

Case - 1

Case - 2

Case - 3

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method
Case - 1
Case - 2
Case - 3

Ex-1 : T(n)= 16T(n/4)+ 5n3


a=16, b=4, d=3 Consider f(n) is Θ (nd)
16 < 43 means a < bd
so consider Case -1
Thus, T(n)=Θ(nd)
= Θ(n3) So Time complexity is Θ(n3)

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method
Case - 1
Case - 2
Case - 3

Ex-2 : T(n)= 2T(n/2)+ n


a=2, b=2, d=1 Consider f(n) is Θ (nd)
2 = 21 means a = bd
so consider Case -2
Thus, T(n)=Θ(nd log n)
= Θ(n1 log n) So Time complexity is Θ(n log n)

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method
Case - 1
Case - 2
Case - 3

Ex-3 : T(n)= 8T(n/2)+ n


a=8, b=2, d=1
Consider f(n) is Θ (nd)
8 > 21 means a > bd
so consider Case-3
Thus, T(n) =
=Θ(n log28)
= Θ(n3) So Time complexity is Θ(n3)

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method
Variation of Master theorem.

p+1

Here we compare Case - 1


Case - 2
Case - 3
Mr. Prayag Patel Department: IT/ICT-LJIET
Master’s Method
Variation of Master theorem.

p+1
Case-2(a): T(n)= Θ(nlogba log n) for p > -1 p+1
Case-2(b): T(n)= Θ(nlogba log log n) for p = -1
Case-2 (c): T(n)= Θ(nlogba) if p < -1

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method

p+1

Θ (nlog22 log n)
= Θ (n log n)

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method

p+1

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method

p+1

Mr. Prayag Patel Department: IT/ICT-LJIET


Master’s Method

p+1
Case-2(a): T(n)= Θ(nlogba log n) for p > -1 p+1

Case-2(b): T(n)= Θ(nlogba log log n) for p = -1


Case-2 (c): T(n)= Θ(nlogba) if p < -1

Ex-4 : T(n)= 2T(n/2)+ n log n


Here a = 2, b = 2, p=1. So, logba = 1 and nlogba = n
f(n)= n log n
so consider case-2 (a)
p+1
T(n) = Θ(nlogba log n)
1+1 2
log 2
T(n) = Θ(n 2 log n) = = Θ(n log n)
Mr. Prayag Patel Department: IT/ICT-LJIET
Master’s Method

p+1

Ex-5 : T(n)= 3T(n/4)+ n log n


Here a=3, b=4. So, logba = 0.793 and nlogba = n0.793
f(n)= n log n
so consider case-3
T(n) = Θ(f(n))
= Θ(n log n)
Mr. Prayag Patel Department: IT/ICT-LJIET
Methods to Solve Recurrence
1) Substitution Method.
a) Forward Substitution Method
b) Backward Substitution Method
2) Master’s Method.
3) Recurrence tree Method.

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
▪ In recurrence tree, each node represents the cost of a
single sub-problem in the set of recursive function
invocations.
▪ We sum the costs within each level of the tree to
obtain a set of per level costs.
▪ Then we sum the all the per level costs to determine
the total cost of all levels of the recursion.

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

Ex-1 :

(1) if n = 1;
T(n) =
2T(n/2) + (n) if n > 1.

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
T(n)
Level 0 20=1

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

T(n/2) T(n/2) Level 1 21=2

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

T(n/4) T(n/4) T(n/4) T(n/4) Level 2 22=4

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

T(n/4) T(n/4) T(n/4) T(n/4) Level 2 22=4

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

cn/4 cn/4 cn/4 cn/4 Level 2 22=4

T(n/ 2 i)

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

cn/4 cn/4 cn/4 cn/4 Level 2 22=4

T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) Level i 2i

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

cn/4 cn/4 cn/4 cn/4 Level 2 22=4

T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) T(n/ 2 i) Level i 2i

 (1)

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

cn/4 cn/4 cn/4 cn/4 Level 2 22=4

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i Level i 2i

 (1)

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

cn/4 cn/4 cn/4 cn/4 Level 2 22=4

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i Level i 2i

 (1) 1 = n/ 2 i 2i=n i = log2n

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
Level Node
cn Level 0 20=1

cn/2 cn/2 Level 1 21=2

h = lg n cn/4 cn/4 cn/4 cn/4 Level 2 22=4

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i Level i 2i

 (1) i = log2n

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn

cn/2 cn/2

h = lg n cn/4 cn/4 cn/4 cn/4

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i

 (1)

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn

cn/2 cn/2 cn

h = lg n cn/4 cn/4 cn/4 cn/4

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i

 (1)

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn

cn/2 cn/2 cn

h = lg n cn/4 cn/4 cn/4 cn/4 cn

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i

 (1)

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn

cn/2 cn/2 cn

h = lg n cn/4 cn/4 cn/4 cn/4 cn

cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i cn/ 2 i

 (1) = n(lg n – 0 + 1) = n( lg n + 1) = n lg n + n

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

=n(lg n – 0 + 1)
=n( lg n + 1)
= nlg n + n
T(n)=O(n lg n)
Mr. Prayag Patel Department: IT/ICT-LJIET
Recurrence Tree Method

n2

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

n2 n2

(n/2) 2 (n/2) 2

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

n2 n2

n2/2
(n/2) 2 (n/2) 2

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

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

n2 n2

n2/2
(n/2) 2 (n/2) 2

T (n/4) T (n/4) T (n/4) T (n/4) n2/4

= n2/ 2i

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

n2 n2

n2/2
h = lg n (n/2) 2 (n/2) 2

2 2 (n/4) 2 (n/4) 2 n2/4


(n/4) (n/4)

= n2/ 2i

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method

n2/ 2i

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
n

T (n) =
r
a
m

Mr. Prayag Patel Department: IT/ICT-LJIET


Recurrence Tree Method
T (n)

T (n) = O(n2)
Mr. Prayag Patel Department: IT/ICT-LJIET
THANK YOU FOR
WATCHING!

Mr. Prayag Patel [email protected]


* Veaigtion ot Muches's method
Tn) aT(b) t fa
Whese
fn)=
Ccsei: if fn) O(n then

Cuse 2:
tor P 1

Jog dog n ) foa Pa-1


Tn)= (
hen fon)>n
Tn) (fn))

Ton) - 2T(2) + ndogn

fim)

Pt1

n)

You might also like