On Recurrence Relation
On Recurrence Relation
~ Rezaur Rahman
S.No.: 47
Class: IT-2, 5th Sem
Recurrences and Examples
• An equation that defines a sequence based on a rule that
gives the next term as a function of the previous term(s).
T(n) = T(n-1) + n
• Recurrences arise when an algorithm contains recursive
calls to itself
• Example: Fibonacci series where succeeding terms are
f(n)=f(n-1)+f(n-2)
3
Recurrent Algorithms
BINARY-SEARCH
• for an ordered array A, finds if x is in the array A[lo…hi]
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x)
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x)
4
Analysis of BINARY-SEARCH
Alg.: BINARY-SEARCH (A, lo, hi, x)
if (lo > hi) constant time: c1
return FALSE
mid (lo+hi)/2 constant time: c2
if x = A[mid]
constant time: c3
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x)same problem of size n/2
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x)same problem of size n/2
• T(n) = c + T(n/2)
– T(n) – running time for an array of size n
5
Methods for Solving Recurrences
• Substitution method(iteration)
• Master method
6
The Substitution Method
• Convert the recurrence into a summation and try
to bound it using known series
– Use back-substitution to express the recurrence in
terms of n and the initial (boundary) condition.
– Iterate the recurrence until the initial condition is
reached.
7
Substitution Method - Examples
T(n) = 1 + T(n/2)
T(n) = 1 + T(n/2) T(n/2) = 1 + T(n/4)
= 1 + 1 + T(n/4) T(n/4) = 1 + T(n/8)
= 1 + 1 + 1 + T(n/8)
After substituting for k times, we get
T(n) = 1 + 1 + … + 1 + T(n/ 2k)
k times
= k + T(n/ 2k)
Now T(n/ 2k) = T(1)
8
Substitution Method - Examples
=> n/ 2k = 1
=> 2k = n
=> k = log(n)
Time complexity= Θ(log n)
9
Substitution Method – Examples
T(n) = n + 2T(n/2)
T(n) = n + 2T(n/2) T(n/2) = n/2 + 2T(n/4)
= n + 2(n/2 + 2T(n/4))
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
…
After k times, we get
T(n) = kn + 2kT(n/2k) …(i)
10
Substitution Method - Examples
Assume T(n/2k) = T(1)
n/2k = 1
2k = n …(ii)
k= log n …(iii)
Put (ii) & (iii) in (i), we get
T(n) = n T(1) + k n
T(n) = n x 1 + n log (n)
T(n) = n + n log (n)
12
Example 1
W(n) = 2W(n/2) + n2
3 2 3 1
T ( n) cn n
16
log4 3
cn 2 n log4 3
i 0 16
3
cn 2 n log4 3 O(n 2 )
i 0
1
16 14
T(n) = O(n2)
Example 2 – Solving by substitution
method
T(n) = 3T(n/4) + cn2 …(i)
15
Solving by substitution method
T(n) = 3kT(n/4k) + cn2 + 32c(n/42)2 + 33c(n/43)2 + ……… + 3k-1c(n/4k-1)2
= cn 2(1 + 3/42 + (3/42)2 + (3/42)3 + ……)
T(n) = cn2[
T(n) = d n2 where d is some constant
Θ(n2)
16
Example 3 (simpler proof)
W(n) = W(n/3) + W(2n/3) + n
=> k =
Now from equation (iv), we have
W(n) = W(n(2/3)k) + k n …(iv)
19
Master’s method
• For solving recurrences of the form:
n
T ( n) aT f ( n)
b
where, a ≥ 1, b > 1, and f(n) = Θ(nk logp n)
Case1: if logab > k , then Θ(nlogab)
Case2: if logab = k
if p > -1, Θ(nk logp+1 n)
if p = -1, Θ(nk log log n)
if p > -1, Θ(nk)
Case2: if logab = k
if p > -1, Θ(nk logp+1 n)
if p = -1, Θ(n log log n)
if p > -1, Θ(nk)
Case2: if logab = k
if p > -1, Θ(nk logp+1 n)
21
Time Complexity = Θ(n log n)
Examples
T(n) = 2T(n/2) + n2
n
T (
Sol: Compare it with n ) aT f ( n)
b
Where f(n) = Θ(nk logp n)
a = 2, b = 2, k=2, p = 0
=> log22 = 1 k (Case 3)
Θ(n2)
22
Examples
T(n) = 2T(n/2) + n
23
Examples
In above example, p = 1 0
=> a = 2, b = 2, k = 1, p = -1
Case2: if logab = k
if p > -1, Θ(nk logp+1 n)
if p = -1, Θ(nk log log n)
if p > -1, Θ(nk)
We have p = -1
Case2: if logab = k
if p = -1, Θ(nk log log n)