Recurrences 2
Recurrences 2
Recurrences 2
Recurrences /Dr.A.Sattar / 1
Recurrences
Topics
Introduction to recurrences
Applications of recurrences
Solving recurrences
-Iterations method
-Substitution method
-Characteristic Equation
- Induction method
Recurrences /Dr.A.Sattar / 2
Introduction
Recurrences /Dr.A.Sattar / 3
Recurrences
Definition
A recurrence is a relation or an equation that describes a function in terms of its lower
order arguments. It has the following characteristics
(ii) The definition includes a base value for the function, called boundary condition
Recurrences /Dr.A.Sattar / 4
Recurrences
Taxonomy
Recurrences are frequently used to specify running times (complexity) for a broad
categories of problems. Common examples are decrease-and-conquer , divide-and-conquer,
probabilistic analysis, and dynamic programming algorithms.
• In a divide-and-conquer algorithm a problem is split (divided) into subproblems , which are then
solved (conquered) and combined to obtain solution to the main problem. In general the running time is
expressed as
T(n)=aT(n/b) + f(n)
where a is the number of subproblems and b is the size of each subproblem. The function f(n)
is the cost of dividing and combing the subproblem solutions.
Recurrences /Dr.A.Sattar / 5
Recurrences
Examples
The following examples demonstrate the use of recurrence for the running times of
common algorithms. Here T(n) denotes the running time for a problem of size n.
T(n) = T(n-1 ) + cn
Example(2) This example illustrates the recurrence relation for divide-and-conquer problem.
T(n) = 8 T(n / 4 ) + c n2
Recurrences /Dr.A.Sattar / 6
Recurrences
Applications
Some typical applications of recurrences are as follows;
Linear Search: T(n)= T(n-1) + c
Recurrences /Dr.A.Sattar / 8
Solution of Recurrence
Closed Form
In some cases exact solution to a recurrence can be expressed in a closed form , or as a
formula. For example, the exact solution to the following recurrence
1, n =2
T(n) =
2T(n/2) + 2 n > 2
T(n)=(3/2)n-2.
Recurrences /Dr.A.Sattar / 9
Solution of Recurrence
Asymptotic Notation
For most problems the exact solution to a recurrence cannot be determined, or it may not exist
in a closed form (formula). Since analysis of algorithm is primarily concerned with large inputs,
for analysis purposes, it is sufficient to express the solution in terms of asymptotic notation
For example, the solution to the recurrence
c, n =1
T(n) =
3T(n/4) + cn2 n>1
T(n) = θ(n2)
Recurrences /Dr.A.Sattar / 10
Solving Recurrences
Methods
Common methods for the solution are of recurrences are:
Using Theorems
Recurrences /Dr.A.Sattar / 11
The Iteration Method
Recurrences /Dr.A.Sattar / 12
The Iteration Method
Procedure
In the iteration method the recurrence is solved by following the top-down approach.
It involves following steps:
(1) Using definition, equations are set up for arguments n, n-1, n-2…..
(4) Finally, the solution is obtained by canceling out identical terms on the left-hand
and right- hand sides of the iterated equations
Recurrences /Dr.A.Sattar / 13
The Iteration Method
Example(1)
Here is a recurrence for reduce-and-conquer algorithm :
T(0)=0
T(n)= T(n-1) +c for n>0
Iterating the recurrence:.
T(n) = T(n-1 ) + c
T(n-1) = T(n-2) + c
T(n-2) = T(n-3) + c
……………………
T(3) = T(2) + c
T(2) = T(1) + c
T(1) = T(0) + c
Adding both sides of the equations, and canceling equal terms:
T(n) = T(0) + c+ c +………+ c (n terms )
Or, T(n) = n.c
Recurrences /Dr.A.Sattar / 14
The Iteration Method
Example(2)
Here is another recurrence for reduce-and-conquer algorithm with
linear cost function
T(0)=0
T(n) = T(n-1) + c.n
Iterating the recurrence:
T(n) = T(n-1 ) + c.n
T(n-1) = T(n-2) + c.(n-1)
T(n-2) = T(n-3) + c.(c-2)
……………………
T(3) = T(2) + c.3
T(2) = T(1) + c.2
T(1) = T(0) + c.1
Adding both sides of the equations, and canceling out the equal terms:
T(n) = T(0)+ c(1 + 2+ 3 +………+n)
Using boundary condition for T(0), and summing the arithmetic series:
T(n)=c.n(n+1)/2
It follows that recurrence has the solution
∈
T(n) θ(n2)
Recurrences /Dr.A.Sattar / 15
The Substitution Method
Recurrences /Dr.A.Sattar / 16
The Substitution Method
Procedure
The substitution method provides a systematic procedure for solving recursive equation.
It is similar to the iteration method because it also follows the top-down approach.
It is, however, useful for solving recurrences for divide-and-conquer problems.
The procedure involves following steps:
(1) In the recurrence, values are plugged in repeatedly on the right-hand side
of the equation.
(4) The summation for the series is analyzed to determine the asymptotic behavior.
Recurrences /Dr.A.Sattar / 17
The Substitution Method
Example(1)
Here is recurrence for a divide-and-conquer algorithm, with fixed cost of splitting/combining.
T(1) = c
T(n)=T(n/2) + c, n > 1
Initially, T(n) = T(n/2) + c = T(n/21) + c ………..(1)
It will be seen that the base case, T(1)=c, will be reached when n / 2k =1, or n=2k .
Taking binary logarithm, we have k = lg n
Substituting for k in equation (4), we get
T(n) = T(1)+ lg n. c
= c + lg n. c
Thus, T(n) ∈θ( lg n)
Recurrences /Dr.A.Sattar / 18
The Substitution Method
Example(2)
Here is a recurrence for divide-and-conquer algorithm with fixed cost function. The problem is split into
two sub-problems of equal sizes.
T(1) = c
T(n)=2T(n/2) + c, n > 1
It will follow that the base case , T(1)=c ,will be reached when n / 2k =1, or n=2k ,
Substituting for 2k in (5) , we get
T(n) = n.T(1) +( n-1).c = n.c +n.c - c = c.(2n-1)
Ignoring constants, we have,
∈
T(n) θ(n)
Recurrences /Dr.A.Sattar / 19
The Substitution Method
Example(3)
This is recurrence for a divide-and-conquer algorithm with linear cost.
T(1)= c
T(n)= 2T(n/2) + cn, n > 1
The base case, T(1)=c, will be reached when n/2k = 1, i.e 2k =n, or k=lg(n)
T(n) = n.T(1) + cn.lg n =cn+ cn.lg n …….……………………….(5)
Recurrences /Dr.A.Sattar / 22
The Recursion Tree Method
Procedure
The recursion tree provides a visual tool for solving recursive equation. It involves
following steps
Step # 1 The recurrence is expressed in a hierarchical way using a tree structure, such that
each node contains two fields: the size field and cost field . The number of child
nodes in the tree equals the number of subproblems
size
cost
Step #2: The size field of a node is set by plugging in the the size of parent node
into the relation
Step # 3 :The cost field is set by substituting node size into cost function
of the relation
Step #4: The solution is found by summing the costs over all nodes of the tree
Recurrences /Dr.A.Sattar / 23
The Recursion Tree Method
Example(1)
T(n)= 2T(n/2) + cn, n>1, T(1)=c
Step #1: Constructing tree structure
The fully expanded recursion tree is shown below. It has 2d nodes at the bottom level (called leaves) where
d is the tree depth. Since at the bottom level T(n/2d)=T(1), it follows that n/2d=1, or 2d= n. i.e d= lg n.
Thus, tree depth = lg n. , and number of leaves is 2d=n
Nodes
20
21
23
2d
n
Recurrences /Dr.A.Sattar / 24
The Recursion Tree Method Cont’d-1
Example(1)
T(n)= 2T(n/2) + cn, n>1, T(1)=c
Step#2: Inserting costs
The root has associated size n and cost cn. Each child of root has size n/2 and associated cost cn/2
At the next level the costs are reduced by a factor of 2. This reduction is continued up to the bottom level.
Each leaf has associated cost of c.
lg(n)
n
Recurrences /Dr.A.Sattar / 25
The Recursion Tree Method Cont’d-2
Example(1)
T(n)= 2T(n/2) + cn, n>1, T(1)=c
Step #3: Summing up rows and leaves costs
Each row contributes total cost cn. Since there are lg n+1 levels, including the last level, the total cost
associated with all nodes excluding leaves is cn.lg n. There are n leaves, each having cost c. Thus, total
contribution of leaves is c.n Hence, the recurrence has the solution T(n)=cn.lg n + cn =θ(n lg n)
rows costs
cn
2xcn/2 = cn
4xcn/4 = cn
lg(n)
8xcn/8 = cn
The fully expanded binary recursion tree is shown below. Tree has depth lg n, and n leaves.
20
21
22
lg(n)
23
2d
Recurrences /Dr.A.Sattar / 27
The Recursion Tree Method Cont’d-1
Example(2)
T(n) = 2T(n/2) + cn2 , n>1, T(1)=c
Step#2: Inserting costs
The root has associated size of n and cost of cn2. Each child of root has size n/2 and associated cost cn2/4
At the next level the costs are reduced by a factor of 4. This reduction is continued up to the bottom level.
Each leaf has associated cost of c.
lg(n)
leaves= n
Recurrences /Dr.A.Sattar / 28
The Recursion Tree Method Cont’d-2
Example(2)
T(n) = 2T(n/2) + cn2 , n>1, T(1)=c
Step #3: Summing up rows and leaves costs
Summing the costs associated with the internal nodes and leaves:
T(n) = cn2. [1/20+ 1/21+ 1/23 + ……..1/2lgn-1] + cn
The asymptotic behavior of the series is determined by the largest term, which is 1.Thus,
1/20+ 1/21+ 1/23 + ……..1/2lg n-1 = θ(1).
Therefore, T(n)=cn2.θ(1)+cn= θ(n2) ( n2 being the dominant term in the sum)
rows cost
cn2.[1/20]
8 x cn2/64 = cn2.[1/23]
nodes
30
31
log4n
32
3d
Recurrences /Dr.A.Sattar / 30
The Recursion Tree Method Cont’d-1
Example(3)
T(n) = 3T(n/4) + cn2 , n>1, T(1)=c
cn2(1)
cn2(1/2)
log4n cn2(1/4)
cn2(1/8)
Example(3)
T(n) = 3T(n/4) + cn2 , n>1, T(1)=c
Step #3: Summing over the rows and leaves costs
There are log 4 n -1 rows of internal nodes and one root node, each contributing a cost of cn2. The total
contribution by all leaves is c.nlog 4 3.Summing over all nodes of the recursion tree:
T(n)= cn2. [1+(3/16)1+(3/16)2 + …….. +(3/16)log4 n-1 ]+ c.n log 4 3
The summation in brackets is geometric series with ratio 3/16 <1.Asymtotically the summation is θ(1) Thus,
1+(3/16)1+(3/16)2 + …….. +(3/16)log4 n-1 = θ(1)
c x 3 x (n/4)2=c.(3/16)1 n2
log4n
c x 9 x (n/16)2= c.(3/16)2n2
leaves= nlog 3
4
Recurrences /Dr.A.Sattar / 32
Using Theorems
Recurrences /Dr.A.Sattar / 33
Theorem #1
Statement
If a is the number of sub-problems and b the size of each sub-problem, then the solution to
the divide-and-conquer recurrence
T(1) = c
T(n) = a T(n/b) + cn, for n >1
is given by
The theorem provides solution to problems in which cost of dividing and combining sub-
problems is linear.
Recurrences /Dr.A.Sattar / 34
Theorem #1
Usage
According to Theorem#1, the solution of recurrence, T(n)= aT(n/b) + cn ,is given by
θ(n) when a<b (case 1)
T(n) = θ(n log b n) when a=b (case 2)
θ(nlogba) when a >b (case 3)
Therefore, T(n)=θ(n)
Recurrences /Dr.A.Sattar / 35
Theorem #1
Usage
According to Theorem#1, the solution of recurrence, T(n)= aT(n/b) + cn ,is given by
θ(n) when a<b (case 1)
T(n) = θ(n log b n) when a=b (case 2)
θ(nlogba) when a >b (case 3)
a=2, b=2
Therefore, T(n)=θ(n lg n)
Recurrences /Dr.A.Sattar / 36
Theorem #1
Usage
According to Theorem#1, the solution of recurrence, T(n)= aT(n/b) + cn ,is given by
θ(n) when a<b (case 1)
T(n) = θ(n log b n) when a=b (case 2)
θ(nlogba) when a >b (case 3)
a=3, b=2
=θ(n lg 3)
Recurrences /Dr.A.Sattar / 37
Theorem #1
Proof
The method of substitution can be used to obtain solution to the recurrence
T(n)=aT(n)+cn .
Substituting for T(n/b):
T(n)=a[ aT(n/b2) + c (n/b)] + cn = a2T(n/b2) + cn[ 1+(a/b)]
The term T(n/bk) would reduce to base case T(1) when n / bk =1, or bk = n.
Or, taking logarithm with base b, k = log b n.
T(1) = c
T(n) = a T(n/b) + cnx , for n >1, where x is some constant,
is given by
The theorem provides solution to problems in which cost of dividing and combining
subproblems is some polynomial term..
Recurrences /Dr.A.Sattar / 40
Theorem #2
Usage
According to Theorem#2, the solution of recurrence T(n)=aT(n/b) + cnx, where x is some
positive constant is given by
Therefore, T(n)=θ(n3)
Recurrences /Dr.A.Sattar / 41
Theorem #2
Usage
According to Theorem#2, the solution of recurrence T(n)=aT(n/b) + cnx, where x is some
positive constant is given by
Therefore, T(n)=θ(n2 lg n)
Recurrences /Dr.A.Sattar / 42
Theorem #2
Usage
According to Theorem#2, the solution of recurrence T(n)=aT(n/b) + cnx, where x is some
positive constant is given by
Recurrences /Dr.A.Sattar / 43
Theorem #2
Proof
The following is an outline of the proof of theorem #2
The recurrence
T(1) = c
T(n) = a T(n/b) + cnx , for n >1
can be solved by substitution method.
It can be shown that after k substitutions the result is given
T(n)= akT(n/bk) +cnx[1+(a/bx)+(a/bx)2+(a/bx)3+…..+(a/bx)k-1]
The term T(n/bk) would reduce to base case T(1) when n / bk =1, or bk = n.
Taking logarithm with base b, k = log b n. Substituting for k in the summation:
The geometric series has geometric ratio a/bx By considering the asymptotic behavior
of the series it can be shown that
T(n) = θ(nx), when a < bx
T(n) = θ(nx log n), when a= bx
T(n) = θ(nlog a),
b when a > bx
Recurrences /Dr.A.Sattar / 44
Master Theorem
Statement
Let a ≥ 1 and b>1 be constants, then the recurrence
T(n)= aT(n/b) +f(n)
has the solution:
(1) T(n)= θ(n log b a ) when f(n)=O( n log b a-ε) for some ε>0
The Master Theorem provides a generalized solution for the divide-and-conquer algorithms.
The cost function can be a polynomial or logarithmic function
Recurrences /Dr.A.Sattar / 45
Master Theorem
Constraints
In Master Theorem, the cost function f(n) is compared with the function nlog b a
Depending on the outcome, the larger of the two functions provides the solution, subject to
some additional constraints.
The constraint is that the function f(n) and n log b a should not be simply larger or smaller
asymptotically, but should grow faster or slower by a polynomial factor nε , where ε is some
arbitrary small positive constant. Accordingly, in using Master Theorem the following
conditions are further examined:
Case 1: If f(n)= O( nlog b a – ε) f(n) grows slower than nlog b a by a factor of nε, then
solution of the recurrence is
T(n) = θ(nlog b a)
Case 2: If f(n)= θ( nlog b a), i. e. f(n) grows as fast as nlog b a, then solution of the
recurrence is
T(n) = θ(nlog b a lg n)
Case 3: If f(n)= Ω( nlog b a + ε) , i. e. f(n) grows faster than nlog b a by a factor of nε,
and in addition f(n/b) ≤ c.f(n) for some c<1,then solution of the recurrence is
T(n)= θ( f(n) )
Recurrences /Dr.A.Sattar / 46
Master Theorem
Usage
Example(1): T(n)=4T(n/2) +n
Since f(n) = n grows slower than = nlog b a-ε =n1.5, it follows that f(n)= O(nlog b a-ε)
Recurrences /Dr.A.Sattar / 47
Master Theorem
Usage
Example(2): T(n)=T(n/2) +1
T(n)=θ(nlog b a .lg n)
= θ(lg n)
Recurrences /Dr.A.Sattar / 48
Master Theorem
Usage
Example(3): T(n)=T(n/3) +n
Take ε=0.5
Since f(n) = n grows faster than nlog b a+ε = n0.5, it follows that f(n)= Ω(nlog b a +ε )
Recurrences /Dr.A.Sattar / 49
Master Theorem
Usage
Example(4): T(n)=3T(n/4) +n lg n
Take ε=0.207
Since f(n) = n log n grows faster than nlog b a+ε = n, it follows that f(n)= Ω(nlog b a +ε )
T(n)=4T(n/4) +n lg n
Therefore, the case 3 of Master Theorem does not apply. Thus, this example cannot
be solved by applying Master Theorem
Recurrences /Dr.A.Sattar / 51
Master Theorem
Proof
Here is brief outline of the proof. For details consult the text book.
The Master theorem can be proved by substitution method. An outline of the proof is as follows
T(n)= aT(n/b) + f(n)
log b n -1
=a
log b n
T(1)+ ∑ak f(n/b )
k =1
k
log a log b n - 1
= n b T(1) +
∑ ak f(n/b )
k =1
k
Recurrences /Dr.A.Sattar / 53
TheCharacteristic Equation
Definition
The algorithms for some problems can be expressed in terms of linear homogenous
relation of the type
T(n)=an-1T(n-1)+an-2T(n-2)+……+a1T(1).
In order to solve the recurrence, the relation is usually converted into an algebraic
equation . This equation is called characteristic equation.
Recurrences /Dr.A.Sattar / 54
Characteristic Equation
Solution
The solution to the recurrence
T(n) = aT(n-1)+ bT(n-2), T(0)=0, T(1)=1
is obtained in a closed form by the following procedure:
Step #2: The quadratic equations has two roots. If x1 and x2 are the distinct roots then
x1 = ( a+√(a2 + 4b) ) / 2
x2 = ( a - √(a2 + 4b) ) / 2
Recurrences /Dr.A.Sattar / 55
Characteristic Equation
Usage
The Fibonacci Sequence is a homogenous recurrence, which can be solved by using the method of
characteristic equation. The recurrence has the form:
T(n) = T(n-1)+ T(n-2) for >1 , T(0)=0, T(1)=1
Let T(n) = xn
Recurrences /Dr.A.Sattar / 57
The Induction Method
Statement
The induction method is commonly used to establish the validity of
some known recurrence. It involves following steps
• Finally, using the basic definition and assumption made at the previous step , it is proved
that relation T(n) also holds true for k+1 or 2k
If the above conditions are satisfied then recurrence holds true for all n
Recurrences /Dr.A.Sattar / 58
Induction Method
Usage
Using induction method it can be proved that recurrence
1, n =2
T(n) =
2T(n/2) + 2 n > 2
has the solutions T(n)= (3/2)n – 2.
It follows that the relation is true for n=2k. Therefore, it is true for all n
Recurrences /Dr.A.Sattar / 59