Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

1.6 Mathematical analysis for Recursive algorithms (1)

The document outlines methods for analyzing recursive algorithms in the context of the Design and Analysis of Algorithms course (CS8451). It discusses various techniques such as the Substitution Method, Master Theorem, and Recursion Tree Method, providing examples like computing factorials and the Towers of Hanoi problem. Additionally, it covers the mathematical analysis of Fibonacci numbers and binary digit counting algorithms.

Uploaded by

gopi ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

1.6 Mathematical analysis for Recursive algorithms (1)

The document outlines methods for analyzing recursive algorithms in the context of the Design and Analysis of Algorithms course (CS8451). It discusses various techniques such as the Substitution Method, Master Theorem, and Recursion Tree Method, providing examples like computing factorials and the Towers of Hanoi problem. Additionally, it covers the mathematical analysis of Fibonacci numbers and binary digit counting algorithms.

Uploaded by

gopi ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SUBJECT CODE

TYPE THE SUBJECT NAME HERE

UNIT NO 1
INTRODUCTION

● Mathematical analysis for Recursive


algorithms
II IV

CS8451
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to CSE & IT)
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

1. Substitution Method - This method repeatedly makes


substitution for each occurrence of the function T in the right-hand side
until all such occurrences disappear.

2. Master Theorem - The efficiency analysis of many divide-and-


conquer algorithms is greatly simplified by the master theorem. It states
that, in recurrence equation T(n) = aT(n/b) + f (n), If f (n)∈ Θ (nd) where d
≥ 0 then
.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Mathematical analysis for Recursive algorithms


Solving recurrence relations
1.Substitution Method - This method repeatedly makes substitution for
each occurrence of the function T in the right-hand side until all such
occurrences disappear.

2.Master Theorem - The efficiency analysis of many divide-and-conquer


algorithms is greatly simplified by the master theorem.It states that, in
recurrence equation T(n) = aT(n/b) + f (n), If f (n)∈ Θ (nd) where d ≥ 0
then
.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

.
3 Recursion Tree Method

● Recursion Tree Method is a popular technique for solving


such recurrence relations, in particular for solving
unbalanced recurrence relations

● For example, in case of modified merge Sort, to solve a


problem of size n (to sort an array of size n), the problem is
divided into two problems of size n/3 and 2n/3 each

● This is done recursively until the problem size becomes 1.


CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Master’s Method

● The Master Method is used for solving the following types of recurrence.

● T(n) = a T(n)+ f (n/b) with a≥1 and b≥1 be constant & f(n) be a function and n/b can
be interpreted as, Let T (n) is defined on non-negative integers by the recurrence.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

.
General Plan

● Decide the input size based on the parameter n

● Identify algorithm’s basic operation.

● Check how many times the basic operation is executed.


Then find whether the execution of basic operation depends
upon the input size n. Determine worst, best and average
case for input of size n. If the basic operation depends upon
those three cases then that has to be analyzed separately.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

● Setup the recurrence relation with some initial condition and


expressing the basic operation.

● Solve the recurrence or at least determine the order of growth.


While solving the recurrence we will use the forward and backward
substitution method. And then correctness of formula can be proved
with the help of mathematical induction method.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Example 1:
Computing factorial of some n numbers

If (n=0)
Return 1
Else
Return Factorial (n-1)*n

Mathematical Analysis:
1. The factorial algorithm works for input size n.
2. The basic operation in computing factorial is multiplication.
3. The recursive function can be formulated as F(n) = F(n-1) * n where n >0.
Then basic operation multiplication is given as M(n).
M(n) = M(n-1) + 1
M(n-1) → these multiplications are required to compute factorial (n-1)
+1 → to multiply factorial (n-1) by n.
4. In step 3 the recurrence relation is obtained. M(n) = M(n-1) + 1.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

• Thus, the recurrence relation and initial condition for the algorithm’s number of
multiplications .

M(n):
M(n) = M(n − 1) + 1 for n > 0,

M(0) = 0 for n = 0.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Method of backward substitutions


M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1
= [M(n − 2) + 1]+ 1
= M(n − 2) + 2 substitute M(n − 2) = M(n − 3) + 1
= [M(n − 3) + 1]+ 2
= M(n − 3) + 3

= M(n − i) + i

= M(n − n) + n
= n

Therefore M(n)=n
Thus the time complexity of factorial function is Θ(n).
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Example 2:
Towers of Hanoi
• Classic example of recursive function.
• There are 3 pegs named A, B and C. 5 disks of different sizes are placed on
peg A.
• The arrangements of the disks is such that every smaller disk is placed on
the larger disk.
• The Problem of “Towers of Hanoi” states that move the 5 disks from peg A to
peg C using peg B as an auxiliary.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Mathematical Analysis:

Step 1: The input size is n.

Step 2: The basic operation in this problem is moving the disk from one peg to
another.

• When n>1, then to move these disks from peg A to peg C using
auxiliary peg B, we first move recursively n-1 disks from peg A to peg B using
auxiliary peg C.
• Then we move the largest disk directly from peg A to peg C and
finally move n-1 disks from peg B to peg C.
• If n=1, then we simply move the disk from peg A to peg C.

Step 3: The move of disks are denoted by M(n). M(n) depends on the number of
disks n. The recursive relation can then setup as M(1)=1.
• If n>1 then we need two recursive calls plus one move.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Hence
M(n) = M(n-1) + 1 + M(n-1)
Here M(n-1) → to move (n-1) disks from peg A to B.
1 → To move largest disk from peg A to C.
M(n-1) → to move (n-1) disks from peg B to C.

Therefore M(n) = 2M(n-1) + 1 →(equ 1)

Step 4: solving recurrence M(n) = 2M(n-1) + 1 using substitution methods

M(n) = 2M(n-1) + 1
Put M(n-1) = 2M(n-2)+1
=2[2M(n-2)+1]+1
=4M(n-2)+3
Put M(n-2) = 2M(n-3)+1
=4[2M(n-3)+1]+3
=8M(n-3)+7
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

• From this we can establish a general formula as:


M(n) = 2i M(n-i) + 2i-1 + 2i-2 +…… + 2 +1
This can also be written as:
M(n) = 2i M(n-i) + 2i– 1 → (equ 2)

• As in equation 2 we can obtain M(n) by substituting n = n – i , assume initially


n=1 then, n – i = 1
i = n-1
M(n) = 2i M(n-i) + 2i-1 with i=n-1 can become
= 2n-1 M(n-(n-1))+ 2n-1– 1
= 2n-1 M(1) + 2n-1– 1 put M(1)=1
= 2n-1 + 2n-1-1
M(n) = 2n– 1

• From this we can conclude the time complexity of the Towers of Hanoi
problem as Θ(2n- 1) = Θ(2n).
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

EXAMPLE 3: An investigation of a recursive version of the algorithm which


finds the number of binary digits in the binary representation of a positive
decimal integer.

ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec( n 2 )+ 1

Algorithm Analysis

Since the recursive calls end when n is equal to 1 and there are no additions
made then, the initial condition is A(1) = 0.
The standard approach to solving such a recurrence is to solve it only for n
= 2k A(2k) = A(2k−1) + 1 for k > 0,
A(20) = 0.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Backward substitutions

A(2k) = A(2k−1) + 1
substitute A(2k−1) = A(2k−2) + 1
= [A(2k−2) + 1]+ 1
= A(2k−2) + 2
substitute A(2k−2) = A(2k−3) + 1
= [A(2k−3) + 1]+ 2
= A(2k−3) + 3
..…
= A(2k−i) + i
...
= A(2k−k) + k.
Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original
variable n = 2k and hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Example 4 : Fibonacci Numbers Sequence

• A classic example for more elaborate recursion relations.


Fibonacci Sequence: 0, 1, 1 2, 3, 5, 8, 13, 21, 34, ...
Can be defined by the simple recurrence
F(n) = F(n-1) + F(n-2) for n > 1
IC: F(0) = 0 and F(1) = 1
• Homogenous second-order linear recurrence with constant coefficients
ax(n) + bx(n-1) + cx(n-2) = 0
• Homogenous because the recurrence equals zero.
second-order linear. Substitute the proposed solution x(n) = rn
a rn + b rn-1 + c rn-2 = 0
divide by rn
a + b r + c r2 = 0, characteristic equation is a second order polynomial.

The real roots are solutions


x(n) = αr1n + βr2n α and β are determined from the
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Apply to Fibonacci Recursion

F(n) - F(n-1) - F(n-2) = 0, homogenous second order with constant coefficients


r2 - r - 1 = 0, characteristic equation
r1,2 = (1 ± √5)/2 = φ or φ' { φ = (1+√5)/2 and φ' = (1-√5)/2}

The general form of the solution


F(n) = αφn + βφ' n where α and β are unknowns
Using the Initial Condition,
α + β =0
φα + φ'β = 1
Solve by substituting the first equation into the second, get α = 1/√5 and β = -
1/√5 So ,

F(n) = 1/√5 (φn - φ' n) = φn/√5 rounded to the nearest integer


CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)

Video Link

https://www.youtube.com/watch?v=XTs1H2LfY9g

You might also like