RECURRENCE
EQUATIONS &
ANALYZING
THEM
By:
ALPANA A. INGALE
ROLL NO. 8108
RECURRENCES
 A recurrence is an equation or inequality that describes a function in
terms of its value on small inputs.
 The running time of the recursive algorithm can be obtained by a
recurrence.
 To solve the recurrence relation mean to obtain a function defined on
the natural numbers that satisfies the recurrence.
2
Recurrence Equation Methods –
cont’d
 We solve the recurrence equation by the following methods. That is:
i. Substitution method
ii. Iterative method
iii. Master method
iv. Recurrence or Recursion Tree
 Substitution Method:
1. In this method, first we guess a solution and use mathematical induction to find
the constant and show that the solution works.
2. The substitution method can be used to establish either upper or lower bounds
on a recurrence.
3
Recurrence Equation Methods –
cont’d
 E.g.: T(n) = 2T(n/2) + n --- (1) is a recurrence relation.
 We guess the solution T(n) = O(nlgn), where g(n) = nlgn is the solution.
 That is: f(n) = T(n) ≤ cnlgn --- (2)
 Then we have to prove that, this solution is true, by using mathematical induction.
 From equation-1, T(n) = 2T(n/2) + n
 T(n) ≤ 2c(n/2) lg(n/2) + n
 T(n) ≤ cn.lg(n/2) + n = cn(lgn – lg2) + n
 T(n) ≤ cnlgn – cn + n.
 So, T(n) ≤ cnlgn – n(c – 1)
 T(n) ≤ cnlgn for c >1
 Now, using mathematical induction, T(n) = T(n-1) + 1.
4
Recurrence Equation Methods –
cont’d
 For n = 1, if T(1) = 1, then c ≥ 1
 lg1 = 0 => T(1) is not true for n = 1.
 For n = 2, if T(2) = 2T(1) + 2 = 4, then c ≥ 4.
 2lg2 => c ≥ 2.
 Hence, T(n) ≤ cnlgn, c > 1 is true
 For n = 2 => T(2) is true.
 Similarly, T(3), T(4) … is true
 T(k) is true.
 T(k + 1) is true by using Tk.
 T(n ) = T(n – 1) + 1, g(n) = n = O(n)
5
Recurrence Equation Methods –
cont’d
 T(n) = T(n – 1) + 1
 T(n) = T(n – 2) + 1 + 1
 T(n) = T(n + 3) + 1 + 1 + 1
 Let k = T(n – k) + k.
 Let n – k = 0 (base value).
 n = k
 T₀ + n = n + 1
 Let T₀ = 1 (base case) T(1) = 1, k = n – 1
 T(n) = 1 + n – 1 = n
 So, it is true for n/2 and it is true for n.
6
Recurrence Equation Methods -
cont’d
 If it is true for n, then it is true for 3.
 If it is true for 3, then it is true for 6 and 7 and if it is true for 6, then it is true for
12 and 13.
 Hence, we conclude that for n ≥ c, where c ≥ 2.
 So, T(n) = O(nlgn) is a solution of T(n) = 2T(n/2) + n.
2. Iterative Method: An iterative method is the method, where the recurrence
relation is solved by considering 3 steps. That is:
1. Step 1: expand the recurrence
2. Step 2: express is as a summation (∑) of terms, dependent only on „n‟ and the initial
condition.
3. Step 3: evaluate the summation (∑).
7
Recurrence Equation Methods –
cont’d
 T(n) = 3T(n/4) + n
 Expanding the above terms, we get
T(n) = 3(3T(n/16)) + (n/4) + n
T(n) = 3(3(3T(n/164))) + (n/16) + (n/4) + n
T(n) = n + 3(n/4) + 9(n/16) + 2T(T(n/164))
 The recursion stops, when n/4ⁱ ≤ 1, which implies n ≤ 4ⁱ => log₄n = i
 Thus, T(n) = n + 3n/4+ … + 3ⁱ(n/4ⁱ)+ 3log₄n.θ(1)
 T(n) ≤ (n + 3n/4 + 9n/16 + … + 3 log₄n)θ(1)
 T(n) ≤ n + θ(n log₄3) {as 3log₄n = n log₄3}
 T(n) ≤ n(1/(1 – ¾)) + O(n)
 T(n) = 4n + O(n) {as log₄3 < 1} = O(n)
 Hence, T(n) = O(n) (proved).
8
k
k


0
)4/3(
Recurrence Equation Methods –
cont’d
3. Master Method: The master method is used for solving the following types of
recurrences, T(n) = aT(n/b) + f(n),
where ‘a’ and ‘b’ are constants and a ≥ 1, b > 1.
 In the above recurrence, the problem of size ‘n’ is divided into ‘a’ sub-
problems each of size ‘n/b’.
 Each sub-problem of size ‘n/b’ can be solved recursively in time T(n/b).
 The cost of dividing or splitting the problem and combine the solutions or
result is described by the function f(n).
 Here the size is interpreted as ‘n/b’.
9
Recurrence Equation Methods –
cont’d
 The T(n) can be bounded asymptotically by the following 3 cases.
10
Recurrence Equation Methods –
cont’d
4. Recursion Tree Method: Recursion Tree Method is pictorial representation of
an iteration method, which is in the form of a tree, where at each levels,
nodes are expanded.
 It is used to keep track of the size of the remaining arguments in the
recurrence and the non-recursive costs.
 In a recursion tree, each node represents the cost of a single sub-problem.
 We add the cost within each level of the tree to obtain a set of pre-level cost
and then we add all the levels of costs to determine the total cost of all levels
of recursion.
 In general, T(n) = aT(n/b) + f(n)
11
Recurrence Equation Methods –
cont’d
12
Recurrence Equation Methods –
cont’d
13
14

RECURRENCE EQUATIONS & ANALYZING THEM

  • 1.
  • 2.
    RECURRENCES  A recurrenceis an equation or inequality that describes a function in terms of its value on small inputs.  The running time of the recursive algorithm can be obtained by a recurrence.  To solve the recurrence relation mean to obtain a function defined on the natural numbers that satisfies the recurrence. 2
  • 3.
    Recurrence Equation Methods– cont’d  We solve the recurrence equation by the following methods. That is: i. Substitution method ii. Iterative method iii. Master method iv. Recurrence or Recursion Tree  Substitution Method: 1. In this method, first we guess a solution and use mathematical induction to find the constant and show that the solution works. 2. The substitution method can be used to establish either upper or lower bounds on a recurrence. 3
  • 4.
    Recurrence Equation Methods– cont’d  E.g.: T(n) = 2T(n/2) + n --- (1) is a recurrence relation.  We guess the solution T(n) = O(nlgn), where g(n) = nlgn is the solution.  That is: f(n) = T(n) ≤ cnlgn --- (2)  Then we have to prove that, this solution is true, by using mathematical induction.  From equation-1, T(n) = 2T(n/2) + n  T(n) ≤ 2c(n/2) lg(n/2) + n  T(n) ≤ cn.lg(n/2) + n = cn(lgn – lg2) + n  T(n) ≤ cnlgn – cn + n.  So, T(n) ≤ cnlgn – n(c – 1)  T(n) ≤ cnlgn for c >1  Now, using mathematical induction, T(n) = T(n-1) + 1. 4
  • 5.
    Recurrence Equation Methods– cont’d  For n = 1, if T(1) = 1, then c ≥ 1  lg1 = 0 => T(1) is not true for n = 1.  For n = 2, if T(2) = 2T(1) + 2 = 4, then c ≥ 4.  2lg2 => c ≥ 2.  Hence, T(n) ≤ cnlgn, c > 1 is true  For n = 2 => T(2) is true.  Similarly, T(3), T(4) … is true  T(k) is true.  T(k + 1) is true by using Tk.  T(n ) = T(n – 1) + 1, g(n) = n = O(n) 5
  • 6.
    Recurrence Equation Methods– cont’d  T(n) = T(n – 1) + 1  T(n) = T(n – 2) + 1 + 1  T(n) = T(n + 3) + 1 + 1 + 1  Let k = T(n – k) + k.  Let n – k = 0 (base value).  n = k  T₀ + n = n + 1  Let T₀ = 1 (base case) T(1) = 1, k = n – 1  T(n) = 1 + n – 1 = n  So, it is true for n/2 and it is true for n. 6
  • 7.
    Recurrence Equation Methods- cont’d  If it is true for n, then it is true for 3.  If it is true for 3, then it is true for 6 and 7 and if it is true for 6, then it is true for 12 and 13.  Hence, we conclude that for n ≥ c, where c ≥ 2.  So, T(n) = O(nlgn) is a solution of T(n) = 2T(n/2) + n. 2. Iterative Method: An iterative method is the method, where the recurrence relation is solved by considering 3 steps. That is: 1. Step 1: expand the recurrence 2. Step 2: express is as a summation (∑) of terms, dependent only on „n‟ and the initial condition. 3. Step 3: evaluate the summation (∑). 7
  • 8.
    Recurrence Equation Methods– cont’d  T(n) = 3T(n/4) + n  Expanding the above terms, we get T(n) = 3(3T(n/16)) + (n/4) + n T(n) = 3(3(3T(n/164))) + (n/16) + (n/4) + n T(n) = n + 3(n/4) + 9(n/16) + 2T(T(n/164))  The recursion stops, when n/4ⁱ ≤ 1, which implies n ≤ 4ⁱ => log₄n = i  Thus, T(n) = n + 3n/4+ … + 3ⁱ(n/4ⁱ)+ 3log₄n.θ(1)  T(n) ≤ (n + 3n/4 + 9n/16 + … + 3 log₄n)θ(1)  T(n) ≤ n + θ(n log₄3) {as 3log₄n = n log₄3}  T(n) ≤ n(1/(1 – ¾)) + O(n)  T(n) = 4n + O(n) {as log₄3 < 1} = O(n)  Hence, T(n) = O(n) (proved). 8 k k   0 )4/3(
  • 9.
    Recurrence Equation Methods– cont’d 3. Master Method: The master method is used for solving the following types of recurrences, T(n) = aT(n/b) + f(n), where ‘a’ and ‘b’ are constants and a ≥ 1, b > 1.  In the above recurrence, the problem of size ‘n’ is divided into ‘a’ sub- problems each of size ‘n/b’.  Each sub-problem of size ‘n/b’ can be solved recursively in time T(n/b).  The cost of dividing or splitting the problem and combine the solutions or result is described by the function f(n).  Here the size is interpreted as ‘n/b’. 9
  • 10.
    Recurrence Equation Methods– cont’d  The T(n) can be bounded asymptotically by the following 3 cases. 10
  • 11.
    Recurrence Equation Methods– cont’d 4. Recursion Tree Method: Recursion Tree Method is pictorial representation of an iteration method, which is in the form of a tree, where at each levels, nodes are expanded.  It is used to keep track of the size of the remaining arguments in the recurrence and the non-recursive costs.  In a recursion tree, each node represents the cost of a single sub-problem.  We add the cost within each level of the tree to obtain a set of pre-level cost and then we add all the levels of costs to determine the total cost of all levels of recursion.  In general, T(n) = aT(n/b) + f(n) 11
  • 12.
  • 13.
  • 14.