Unit 4
Unit 4
4.0 INTRODUCTION
4.1 OBJECTIVES
Hence a recurrence has one or more initial conditions and a recursive formula,
known as recurrence relation.
For example: A Fibonacci sequence can be defined by the
recurrencerelation
Solving Recurrences
1. (BasicStep)Thegivenrecurrencesaysthatifn=0then and if n=1 then
. These two conditions (or values) where recursion does not call
itself is called ainitial conditions (or Baseconditions).
2. (Recursive step): This step is used to findnewterms from the
existing (preceding) terms, by using theformula 𝑓𝑛 = 𝑓𝑛−1 + 𝑓𝑛−2 for n≥ 2
This formula says that “by adding two previous sequence (or term) we can get
the next term”.
Forexample
Let us consider some recursive algorithms and try to write their recurrence relation.
Then later we will learn some method to solve these recurrence relations to analyze
the running time of these algorithms.
Algorithm: fact(n)
1: 𝑖𝑓 𝑛 = 0 𝑡ℎ𝑒𝑛
2: 𝑟𝑒𝑡𝑢𝑟𝑛 1
3: 𝑒𝑙𝑠𝑒
4: 𝑟𝑒𝑡𝑢𝑟𝑛 𝑛 ∗ 𝑓𝑎𝑐𝑡(𝑛 − 1)
5: 𝑒𝑛𝑑 𝑖𝑓
Let us try to understand the efficiency of the algorithm in terms of the number of
multiplication operations required for each value of n
Let 𝑇(𝑛) denoted the number of multiplication required to execute the n!.,
that is 𝑇(𝑛) denotes the number of times the line 4 is executed in factorial
algorithm.
We have the initial condition T(0) = 1;when n = 0, the fact simply returns the
number of multiplication is1.
Thus we can write a recurrence relation for the algorithm fact as:
(We can also write some constant value instead of writing 0 and 1, that is
Introduction to Algorithm
b if n = 1 (𝐛𝐚𝐬𝐞 𝐜𝐚𝐬𝐞)
T(n) = {
c + T(n − 1)(𝐑𝐞𝐜𝐮𝐫𝐬𝐢𝐯𝐞 𝐬𝐭𝐞𝐩)
Algorithm3:𝑷𝒐𝒘𝒆𝒓 (𝒙, 𝒏)
1: if(𝑛 == 0)
2: 𝑟𝑒𝑡𝑢𝑟𝑛 1
3: 𝒆𝒍𝒔𝒆
5: 𝑒𝑛𝑑𝑖𝑓
At the end of n-1 years, the amount is 𝑇𝑛−1 . After one more year , the amount will be
𝑇𝑛−1 + the interest amount.
To find out the recurrence relation when n=1 ( base value) we have to find the value
of 𝑇0 . Since 𝑇0 refers to the initial amount, i.e.,5000 .With the above definitions we
can calculate the value of 𝑇𝑛 for any value of n. For example: 𝑇3=
(1.15)𝑇2 =(1.15)(1.15)𝑇1 = (1.15)(1.15)(1.15)𝑇0 = (1.15)3 (5000) The above
computation can be extended to any arbitrary value of n.
𝑇𝑛 = (1.15)𝑇𝑛−1
.. ..
= ((1.15)𝑛 (5000))
Solving Recurrences
4.3.1 SUBSTITUTIONMETHOD
A substitution method is one, in which we guess a bound and then use mathematical
induction to prove our guess correct. It is basically two step process:
Solution: step1: The given recurrence is quite similar with that of MERGE- SORT,
you guess the solutionis
Or
w So is True forn=2
We known that
𝑛
≤c nlog ⌊ ⌋ + n ≤ c nlogn +cnlog2 + n
2
Thus
This recurrence (1) describe the running time of any divide-and-conquer algorithm.
Method (steps) for solving a recurrence 𝑻(𝒏) = 𝒂𝑻( 𝒏𝒃 ) + 𝒇(𝒏) using recursion tree:
We make a
recursion
tree for a
given
recurrence as
follows: a)
To make a
recursion
tree of a
given
recurrence
(1), First put
the value of
𝒇(𝒏) at root
node of a
tree and make a number of child nodes of this root value f(n). Now the tree will look
like as:
Solving Recurrences
[ copy from the pdf file all sections from pdf file before summary. Use summary and
solutions sections from this word fille which are modified]
4.4 SUMMARY
When an algorithm contains a recursive call to itself, its running time can often be
described by a recurrence equation which describes a function in terms of its value
on smaller inputs.
1. The SubstitutionMethod
2. The Recursion-treeMethod
3. The MasterTheorem
4.5 SOLUTIONS/ANSWERS