0% found this document useful (0 votes)
12 views10 pages

Ada 3

Uploaded by

Sunshine Ratrey
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)
12 views10 pages

Ada 3

Uploaded by

Sunshine Ratrey
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/ 10

8. What is Recurrence Relation?

Answer-

 A recurrence is an equation or inequality that describes a function in terms of its values on


smaller inputs.
 It helps in finding the subsequent term (next term) dependent upon the preceding term
(previous term).
 If we know the previous term in a given series, then we can easily determine the next term.

Example-

 Fibonacci numbers is defined by the recurrence relation

Fn = Fn-1 + Fn-2

There are four methods for solving Recurrence:

1. Iteration Method (Back Substitution Method)


2. Master Method
3. Recursion Tree Method
4. Substitution Method
---------------------------------------------------------------------------------------------------------------------------

Iteration Method (Back Substitution Method):-


 The iteration method is a "brute force" method of solving a recurrence relation.
 The general idea is to iteratively substitute the value of the recurrent part of the equation until
a pattern (usually a summation) is noticed, at which point the summation can be used to
evaluate the recurrence.

Example-1-

1 𝑖𝑓 𝑛 = 1
𝑇(𝑛) =
𝑇(𝑛 − 1) + 𝑛 𝑖𝑓 𝑛 > 1

Solution-

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

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

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


Substituting equation (3) in equation (2)

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

Substituting equation (2) in equation (1)

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

Repeating k times

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

Putting n-k = 1

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

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

Given that, when n=1 then T(n) = 1

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

Hence 1 + 2 + 3 + …………… + (n-2) + (n-1) + n = n(n+1)/2

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

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

T(n) = O(n2)

Example-2-

2𝑇(𝑛 − 1) − 1 𝑖𝑓 𝑛 > 0
𝑇(𝑛) =
1 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Solution-

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

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

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


Substituting equation (3) in equation (2)

T (n-1) = 2[2T(n-3)-1]-1

Substituting equation (2) in equation (1)

T(n) = 2[2{2T(n-3)-1}-1]-1

T(n) = 22{2T(n-3)-1}-2-1

T(n) = 23T(n-3)-22-2-1

Repeating k times

T(n) = 2kT(n-k)- 2k-1 - 2k-2 - ……………………… -22-2-1

Putting n = k

T(n) = 2n T(0)- 2n-1 – 2n-2 - ……………………… -22-2-1

Given that, other than n > 0 gives T(n) = 1

T(n) = 2n - 2n-1 – 2n-2 - ……………………… -22-2-1

T(n) = 2n – (2n-1 + 2n-2 + ……………………… +22+2+1)

Hence 2n-1 + 2n-2 + ……………………… +22+2+1 = (2n - 1)/ 2-1 = (2n - 1)

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

T(n) = 1

T(n) = O(1)

Example-3-

3𝑇(𝑛 − 1) 𝑖𝑓 𝑛 > 0
𝑇(𝑛) =
1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Solution-

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


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

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

Substituting equation (3) in equation (2)

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

Substituting equation (2) in equation (1)

T(n) = 3(3(3T(n-3)))

T(n) = 33 T(n-3)

Repeating k times

T(n) = 3k T(n-k)

Putting n = k

T(n) = 3n T(n-n)

T(n) = 3n T(0)

Given that, other than n > 0 gives T(n) = 1

T(n) = 3n

T(n) = O(3n)

Example-4-

𝑇(𝑛 − 1) ∗ 𝑛 𝑖𝑓 𝑛 > 1
𝑇(𝑛) =
1 𝑛=1

Solution-

T(n) = T(n-1) * n -----------------(1)

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

T(n-2) = T(n-3) * (n-2) -----------------(3)


Substituting equation (3) in equation (2)

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

Substituting equation (2) in equation (1)

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

Repeating k times

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

Putting n – k = 1

T(n) = T(1) * (1+1) * (1+2)……………. (n-2) * (n-1) * n

T(n) = T(1) * 2 * 3……………. (n-2) * (n-1) * n

Given that, when n=1 then T(n) = 1

T(n) = 1 * 2 * 3……………. (n-2) * (n-1) * n

T(n) = n!

Hence n! = nn

T(n) = O(nn)

Example-5-

𝑇(𝑛/2) + 𝑐 𝑖𝑓 𝑛 > 1
𝑇(𝑛) =
1 𝑛=1

Solution-

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

T(n/2) = T(n/4) + c -----------------(2)

T(n/4) = T(n/8) + c -----------------(3)

Substituting equation (3) in equation (2)


T(n/2) = T(n/8) + c + c

Substituting equation (2) in equation (1)

T(n) = T(n/8) + c + c + c

T(n) = T(n/23) + 3c

Repeating k times

T(n) = T(n/2k) + kc -----------------(4)

Let us assume n = 2k

Taking log2 both side

log2 n = log22k

log2 n = klog22

log2 n = k [log22 =1]

putting value of k in equation (4)

T(n) = T(n / n) + clog2 n

T(n) = T(1) + clog2 n

Given that, when n=1 then T(n) = 1

T(n) = 1 + clog2 n

T(n) = O(log2 n )

Example-6-

1 𝑖𝑓 𝑛 = 1
𝑇(𝑛) = 𝑛
2𝑇 +𝑛 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
2

Solution-

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


T(n/2) = 2T(n/4) + (n/2) -----------------(2)

T(n/4) = 2T(n/8)+(n/4) -----------------(3)

Substituting equation (3) in equation (2)

T(n/2) = 2[2T(n/8)+(n/4)] + (n/2)

Substituting equation (2) in equation (1)

T(n) = 2[2[2T(n/8)+(n/4)] + (n/2)] + n

T(n) = 22[2T(n/8)+(n/4)] + n + n

T(n) = 23 T(n/8)+ n + n + n

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

Repeating k times

T(n) = 2k T(n/2k) + kn -----------------(4)

Let us assume 2k = n

Taking log2 both side

log2 n = log22k

log2 n = klog22

log2 n = k [log22 =1]

putting value of k in equation (4)

T(n) = n T(n/n) + n log2 n

T(n) = n T(1) + n log2 n

Given that, when n=1 then T(n) = 1

T(n) = n + n log2 n
T(n) = O(nlog2 n )

Example-6-

0 𝑖𝑓 𝑛 = 0
𝑇(𝑛) =
𝑇(𝑛 − 1) + log 𝑛 𝑖𝑓 𝑛 > 1

Solution-

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

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

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

Substituting equation (3) in equation (2)

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

Substituting equation (2) in equation (1)

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

Repeating k times

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

Let us assume n=k

T(n) = T(0) + log (1) + log (2) +…………….. + log (n-1) + log n

Given that, when n=0 then T(n) = 0

T(n) = 0 + log (1) + log (2) +…………….. + log (n-1) + log n

T(n) = log (1) + log (2) +…………….. + log (n-1) + log n

T(n) = log (1*2*3*……………..*(n-1)* n)

Hence 1*2*3*……………..*(n-1)* n = n!

T(n) = log (n!)


Hence n! <= nn

T(n) = log nn

T(n) = nlog n

T(n) = O(nlog n )

---------------------------------------------------------------------------------------------------------------------------

Master Method:-

 The master method provides a “cookbook” method for solving recurrences of the form:

𝑛
𝑇(𝑛) = 𝑎𝑇 + 𝑓(𝑛)
𝑏

 Where n is the size of the problem.


 a ≥ 1 is a constant which is the number of sub-problems in the recursion.
 b > 1 is constant.
 n/b is the size of each sub-problem.
 f(n) is an asymptotically positive function which is the sum of the work done outside the
recursive calls.

T(n) has the following asymptotic bounds:-


1. If f(n) = O(nlogba- ε ) for some constant ε > 0, then T(n) = Θ(nlogba)
2. If f(n) = Θ (nlogba ), then T(n) = Θ(nlogba log n)
3. If f(n) = Ω (nlogba + ε ) for some constant ε > 0, and if af(n/b) ≤ cf(n) for some constant c < 1,
then T(n) = Θ(f(n))
Example-1-
𝑛
𝑇(𝑛) = 8𝑇 + 1000𝑛
2
apply master theorem on it.

Solution:

 Compare 𝑇(𝑛) = 8𝑇 + 1000𝑛 with 𝑇(𝑛) = 𝑎𝑇 + 𝑓(𝑛) 𝑤ℎ𝑒𝑟𝑒 𝑎 ≥ 1 𝑎𝑛𝑑 𝑏 > 1

 We get a = 8, b=2, f (n) = 1000 n2


 Therefore logba = log28 = 3
 Put all the values in: f (n) = O(nlogba- ε )
1000 n2 = O (n3-ε )
 If we choose ε=1, we get: 1000 n2 = O (n3-1) = O (n2)
 Since this equation holds, the first case of the master theorem applies to the given recurrence
relation, thus resulting in the conclusion:

T(n) = Θ(nlogba)
Therefore: T (n) = Θ (n3)
Example-2-
𝑛
𝑇(𝑛) = 9𝑇 +𝑛
3
apply master theorem on it.

Solution:

 Compare 𝑇(𝑛) = 9𝑇 + 𝑛 with 𝑇(𝑛) = 𝑎𝑇 + 𝑓(𝑛) 𝑤ℎ𝑒𝑟𝑒 𝑎 ≥ 1 𝑎𝑛𝑑 𝑏 > 1

 We get a = 9, b=3, f (n) = n


 Therefore logba = log39 = 2
 Put all the values in: f (n) = O(nlogba- ε )
n = O (n2-ε )
 If we choose ε=1, we get: n = O (n2-1) = O (n)
 Since this equation holds, the first case of the master theorem applies to the given recurrence
relation, thus resulting in the conclusion:
T(n) = Θ(nlogba)
Therefore: T (n) = Θ (n2)

You might also like