0% found this document useful (0 votes)
48 views

Unit 4

The document introduces techniques for solving recurrence relations that define recursive algorithms. It discusses three methods: 1) the substitution method, which guesses a bound and uses induction to prove it correct; 2) the recursion tree method, which converts a recurrence into a tree showing how it is divided at each level; and 3) the master method, which provides cases for determining asymptotic bounds of simple recurrences through memorization. The document aims to explain these methods to enable analyzing the time complexity of recursive algorithms defined by recurrence relations.

Uploaded by

Sagnik Ganguly
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)
48 views

Unit 4

The document introduces techniques for solving recurrence relations that define recursive algorithms. It discusses three methods: 1) the substitution method, which guesses a bound and uses induction to prove it correct; 2) the recursion tree method, which converts a recurrence into a tree showing how it is divided at each level; and 3) the master method, which provides cases for determining asymptotic bounds of simple recurrences through memorization. The document aims to explain these methods to enable analyzing the time complexity of recursive algorithms defined by recurrence relations.

Uploaded by

Sagnik Ganguly
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/ 6

Introduction to Algorithm

UNIT 4 SOLVING RECURRENCES

Structure Page Nos.


4.0 Introduction
4.1 Objectives
4.2 Recurrence Programme& Recurrence Relation
4.3 Methods for Solving Recurrence Relation
4.3.1 Substitution method
4.3.2 Recursion Tree Method
4.3.3 Master Method
4.4 Summary
4.5 Solution to check your progress
4.6 Further Reading

4.0 INTRODUCTION

Complexity analysis of iteration algorithms is much easier as compared to recursive


algorithm but once the recurrence relation/equation is defined for recursive algorithm,
which is not difficult task it become easier task to obtain for asymptotic bounds (,
O(big oh) ) for the recursive solution. In this unit we discuss three techniques for
solving recurrence form methods three form techniques for solving recurrence
relation. These are: (i) Substitution method (ii) Recursion Tree Method and (iii)
Master Method. In the substitution method, we first guess an asymptotic bound and
then we prove our guess is correct or not. In the recursion tree method a recurrences
equation is converted into a recursion tree comprising several levels. Calculating time
complexity requires taking a total sum of cost of all the levels. The master method
requires memorization of three different types of cases which help to obtain
asymptotic bounds of many simple recurrence relations.
The structure of the unit is done as follows.

4.1 OBJECTIVES

After going through this unit you will be able to:

 Define recurrence relation


 Construct recurrence relations of simple recursive algorithms
 List the techniques used to solve recurrence relation
Solve the recurrence relation through Substitution, Recurrence tree & Master methods

4.2 RECURRENCE RELATION

Like all recursive functions, a recurrence also consists of two steps:

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.

Example 1: Consider a Factorial function, defined as:

/* Algorithm for computing n!


Input:𝑛 ∈ 𝑁
Output:𝑛!

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.

When 𝑛 > 1, the line 4 performs 1 multiplication plus fact is recursively


called with input (n -1). It means, by the definitionof 𝑇(𝑛), additional 𝑇(𝑛 − 1)
number of multiplications arerequired.

Thus we can write a recurrence relation for the algorithm fact as:

T(1) = 0 (𝐛𝐚𝐬𝐞 𝐜𝐚𝐬𝐞)

T(n) = 1 + T(n − 1)(𝐑𝐞𝐜𝐮𝐫𝐬𝐢𝐯𝐞 𝐬𝐭𝐞𝐩)

(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)(𝐑𝐞𝐜𝐮𝐫𝐬𝐢𝐯𝐞 𝐬𝐭𝐞𝐩)

Where b and c are constants

The following algorithm calculates

Algorithm3:𝑷𝒐𝒘𝒆𝒓 (𝒙, 𝒏)
1: if(𝑛 == 0)

2: 𝑟𝑒𝑡𝑢𝑟𝑛 1

3: 𝒆𝒍𝒔𝒆

4: 𝑟𝑒𝑡𝑢𝑟𝑛 𝑥 ∗ 𝑃𝑜𝑤𝑒𝑟(𝑥, 𝑛 − 1);

5: 𝑒𝑛𝑑𝑖𝑓

The base case isreachedwhenn= = 0


The algorithm 3 performs one comparison and one returnstatement. Therefore,
When n > 1; the algorithm 3 performs one recursive call with inputparameter(n – 1)
at line 4, and some constantnumber of basic operations. Thus we obtain the recurrence
relationas:

Example 4: A customer makes an investment of Rs. 5000 at 15% annual compound


interest. If 𝑇𝑛 denotes the amount earned at the end of n years, define a recurrent
relation and initial conditions.

At the end of n-1 years, the amount is 𝑇𝑛−1 . After one more year , the amount will be
𝑇𝑛−1 + the interest amount.

Therefore 𝑇𝑛 = 𝑇𝑛−1 + (15%)𝑇𝑛−1 =(0.15)𝑇𝑛−1 = (1.15)𝑇𝑛−1 ; n≥ 1

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 METHODS FOR SOLVING RECURRENCE


RELTIONS

Three methods are discussed here to solve recurrence relations:

1. The Substitution Method


The Recursion-tree method
2. Master method

We start with substitution method

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:

Step1: Guess the form of the Solution.

Step2: Prove your guess is correct by using Mathematical Induction.

Example 1. Solve the following recurrence by using substitution method.

Solution: step1: The given recurrence is quite similar with that of MERGE- SORT,
you guess the solutionis

Or

Step2: Now we use mathematical Induction.

Here our guess does not hold for n=1because

i.e., T(1) <= 0 which is in contradiction with T(1) =1


The reason is that O(n log n) means the upper bound holds for n >= 𝑛0 and 𝑛0 could
be 2

Now for n=2

2 ≤ c.2 which is true for c=1

w So is True forn=2

(i) Induction step: Now assume it is true forn=n/2


Introduction to Algorithm

Now we have to show that it is true for


⌈⌉. ⌊⌋

We known that

𝑛
≤c nlog ⌊ ⌋ + n ≤ c nlogn +cnlog2 + n
2

Thus

Remark: Making a good guess, which can be a solution of a given recurrence,


requires experiences. So, in general, we are often not using this method to get a
solution of the given recurrence.

4.3.2 RECURSION TREEMETHOD


A recursion tree is a convenient way to visualize what happens when a recurrence is
iterated. It is a pictorial representation of a given recurrence relation, which shows
how Recurrence is divided till Boundaryconditions.

Recursion tree method is especially used to solve a recurrence of theform:

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.

There are three basic methods of solving the recurrence relation:

1. The SubstitutionMethod

2. The Recursion-treeMethod

3. The MasterTheorem

Master method provides a “cookbook” method for solving recurrences of the


form: are
constants

In master method you have to always compare the value of with


to decide which case is applicable.

4.5 SOLUTIONS/ANSWERS

Copy from pdf file. No change was suggested by the editor

You might also like